diff options
Diffstat (limited to 'searx/network')
| -rw-r--r-- | searx/network/__init__.py | 17 | ||||
| -rw-r--r-- | searx/network/network.py | 8 |
2 files changed, 15 insertions, 10 deletions
diff --git a/searx/network/__init__.py b/searx/network/__init__.py index 8e1edb4d0..6230b9e39 100644 --- a/searx/network/__init__.py +++ b/searx/network/__init__.py @@ -13,6 +13,7 @@ from contextlib import contextmanager import httpx import anyio +from searx.extended_types import SXNG_Response from .network import get_network, initialize, check_network_configuration # pylint:disable=cyclic-import from .client import get_loop from .raise_for_httperror import raise_for_httperror @@ -85,7 +86,7 @@ def _get_timeout(start_time, kwargs): return timeout -def request(method, url, **kwargs): +def request(method, url, **kwargs) -> SXNG_Response: """same as requests/requests/api.py request(...)""" with _record_http_time() as start_time: network = get_context_network() @@ -159,34 +160,34 @@ class Request(NamedTuple): return Request('DELETE', url, kwargs) -def get(url, **kwargs): +def get(url, **kwargs) -> SXNG_Response: kwargs.setdefault('allow_redirects', True) return request('get', url, **kwargs) -def options(url, **kwargs): +def options(url, **kwargs) -> SXNG_Response: kwargs.setdefault('allow_redirects', True) return request('options', url, **kwargs) -def head(url, **kwargs): +def head(url, **kwargs) -> SXNG_Response: kwargs.setdefault('allow_redirects', False) return request('head', url, **kwargs) -def post(url, data=None, **kwargs): +def post(url, data=None, **kwargs) -> SXNG_Response: return request('post', url, data=data, **kwargs) -def put(url, data=None, **kwargs): +def put(url, data=None, **kwargs) -> SXNG_Response: return request('put', url, data=data, **kwargs) -def patch(url, data=None, **kwargs): +def patch(url, data=None, **kwargs) -> SXNG_Response: return request('patch', url, data=data, **kwargs) -def delete(url, **kwargs): +def delete(url, **kwargs) -> SXNG_Response: return request('delete', url, **kwargs) diff --git a/searx/network/network.py b/searx/network/network.py index ff02d447a..84aaebe34 100644 --- a/searx/network/network.py +++ b/searx/network/network.py @@ -1,7 +1,9 @@ # SPDX-License-Identifier: AGPL-3.0-or-later # pylint: disable=global-statement # pylint: disable=missing-module-docstring, missing-class-docstring +from __future__ import annotations +import typing import atexit import asyncio import ipaddress @@ -11,6 +13,7 @@ from typing import Dict import httpx from searx import logger, searx_debug +from searx.extended_types import SXNG_Response from .client import new_client, get_loop, AsyncHTTPTransportNoHttp from .raise_for_httperror import raise_for_httperror @@ -233,8 +236,9 @@ class Network: del kwargs['raise_for_httperror'] return do_raise_for_httperror - def patch_response(self, response, do_raise_for_httperror): + def patch_response(self, response, do_raise_for_httperror) -> SXNG_Response: if isinstance(response, httpx.Response): + response = typing.cast(SXNG_Response, response) # requests compatibility (response is not streamed) # see also https://www.python-httpx.org/compatibility/#checking-for-4xx5xx-responses response.ok = not response.is_error @@ -258,7 +262,7 @@ class Network: return False return True - async def call_client(self, stream, method, url, **kwargs): + async def call_client(self, stream, method, url, **kwargs) -> SXNG_Response: retries = self.retries was_disconnected = False do_raise_for_httperror = Network.extract_do_raise_for_httperror(kwargs) |