extended_types.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """This module implements the type extensions applied by SearXNG.
  3. - :py:obj:`flask.request` is replaced by :py:obj:`sxng_request`
  4. - :py:obj:`flask.Request` is replaced by :py:obj:`SXNG_Request`
  5. - :py:obj:`httpx.response` is replaced by :py:obj:`SXNG_Response`
  6. ----
  7. .. py:attribute:: sxng_request
  8. :type: SXNG_Request
  9. A replacement for :py:obj:`flask.request` with type cast :py:obj:`SXNG_Request`.
  10. .. autoclass:: SXNG_Request
  11. :members:
  12. .. autoclass:: SXNG_Response
  13. :members:
  14. """
  15. # pylint: disable=invalid-name
  16. from __future__ import annotations
  17. __all__ = ["SXNG_Request", "sxng_request", "SXNG_Response"]
  18. import typing
  19. import flask
  20. import httpx
  21. if typing.TYPE_CHECKING:
  22. import searx.preferences
  23. import searx.results
  24. class SXNG_Request(flask.Request):
  25. """SearXNG extends the class :py:obj:`flask.Request` with properties from
  26. *this* class definition, see type cast :py:obj:`sxng_request`.
  27. """
  28. user_plugins: list[str]
  29. """list of searx.plugins.Plugin.id (the id of the plugins)"""
  30. preferences: "searx.preferences.Preferences"
  31. """The prefernces of the request."""
  32. errors: list[str]
  33. """A list of errors (translated text) added by :py:obj:`searx.webapp` in
  34. case of errors."""
  35. # request.form is of type werkzeug.datastructures.ImmutableMultiDict
  36. # form: dict[str, str]
  37. start_time: float
  38. """Start time of the request, :py:obj:`timeit.default_timer` added by
  39. :py:obj:`searx.webapp` to calculate the total time of the request."""
  40. render_time: float
  41. """Duration of the rendering, calculated and added by
  42. :py:obj:`searx.webapp`."""
  43. timings: list["searx.results.Timing"]
  44. """A list of :py:obj:`searx.results.Timing` of the engines, calculatid in
  45. and hold by :py:obj:`searx.results.ResultContainer.timings`."""
  46. #: A replacement for :py:obj:`flask.request` with type cast :py:`SXNG_Request`.
  47. sxng_request = typing.cast(SXNG_Request, flask.request)
  48. class SXNG_Response(httpx.Response):
  49. """SearXNG extends the class :py:obj:`httpx.Response` with properties from
  50. *this* class (type cast of :py:obj:`httpx.Response`).
  51. .. code:: python
  52. response = httpx.get("https://example.org")
  53. response = typing.cast(SXNG_Response, response)
  54. if response.ok:
  55. ...
  56. """
  57. ok: bool