request.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import asyncio
  12. import typing as t
  13. from flask import has_request_context
  14. from flask import request as flask_request
  15. from starlette.datastructures import QueryParams
  16. from werkzeug.datastructures import ImmutableMultiDict
  17. from .fastapi.request import request as fastapi_request
  18. from .fastapi.request import sid as fastapi_sid
  19. from .server import server_type
  20. class RequestAccessor:
  21. @staticmethod
  22. def args(to_dict=False) -> t.Union[ImmutableMultiDict[t.Any, t.Any], QueryParams, t.Dict[str, str]]:
  23. if server_type.get() == "flask":
  24. return flask_request.args if to_dict is False else flask_request.args.to_dict(flat=True)
  25. elif server_type.get() == "fastapi":
  26. fastapi_r = fastapi_request.get()
  27. return (
  28. {}
  29. if fastapi_r is None
  30. else fastapi_r.query_params
  31. if to_dict is False
  32. else dict(fastapi_r.query_params)
  33. )
  34. return {}
  35. @staticmethod
  36. def arg(key, default=None):
  37. if server_type.get() == "flask":
  38. return flask_request.args.get(key, default)
  39. elif server_type.get() == "fastapi":
  40. fastapi_r = fastapi_request.get()
  41. return default if fastapi_r is None else fastapi_r.query_params.get(key, default)
  42. return default
  43. @staticmethod
  44. def form():
  45. if server_type.get() == "flask":
  46. return flask_request.form
  47. elif server_type.get() == "fastapi":
  48. fastapi_r = fastapi_request.get()
  49. return {} if fastapi_r is None else dict(asyncio.run(fastapi_r._get_form()))
  50. return {}
  51. @staticmethod
  52. def files():
  53. if server_type.get() == "flask":
  54. return flask_request.files
  55. elif server_type.get() == "fastapi":
  56. raise NotImplementedError("FastAPI does not support files handling yet")
  57. return {}
  58. @staticmethod
  59. def cookies():
  60. if server_type.get() == "flask":
  61. return flask_request.cookies
  62. elif server_type.get() == "fastapi":
  63. fastapi_r = fastapi_request.get()
  64. return {} if fastapi_r is None else fastapi_r.cookies
  65. return {}
  66. @staticmethod
  67. def sid():
  68. if server_type.get() == "flask" and has_request_context() and flask_request:
  69. return getattr(flask_request, "sid", None)
  70. elif server_type.get() == "fastapi":
  71. return fastapi_sid.get()
  72. return None
  73. @staticmethod
  74. def set_sid(sid: t.Optional[str]):
  75. if server_type.get() == "flask":
  76. flask_request.sid = sid # type: ignore[attr-defined]
  77. elif server_type.get() == "fastapi":
  78. fastapi_sid.set(sid)