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