diff options
Diffstat (limited to 'searx/network/network.py')
-rw-r--r-- | searx/network/network.py | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/searx/network/network.py b/searx/network/network.py index 69af3b7c4..677a908bf 100644 --- a/searx/network/network.py +++ b/searx/network/network.py @@ -13,6 +13,7 @@ import httpx from searx import logger, searx_debug from .client import new_client, get_loop, AsyncHTTPTransportNoHttp +from .raise_for_httperror import raise_for_httperror logger = logger.getChild('network') @@ -226,6 +227,27 @@ class Network: kwargs['follow_redirects'] = kwargs.pop('allow_redirects') return kwargs_clients + @staticmethod + def extract_do_raise_for_httperror(kwargs): + do_raise_for_httperror = True + if 'raise_for_httperror' in kwargs: + do_raise_for_httperror = kwargs['raise_for_httperror'] + del kwargs['raise_for_httperror'] + return do_raise_for_httperror + + @staticmethod + def patch_response(response, do_raise_for_httperror): + if isinstance(response, httpx.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 + + # raise an exception + if do_raise_for_httperror: + raise_for_httperror(response) + + return response + def is_valid_response(self, response): # pylint: disable=too-many-boolean-expressions if ( @@ -239,6 +261,7 @@ class Network: async def call_client(self, stream, method, url, **kwargs): retries = self.retries was_disconnected = False + do_raise_for_httperror = Network.extract_do_raise_for_httperror(kwargs) kwargs_clients = Network.extract_kwargs_clients(kwargs) while retries >= 0: # pragma: no cover client = await self.get_client(**kwargs_clients) @@ -248,7 +271,7 @@ class Network: else: response = await client.request(method, url, **kwargs) if self.is_valid_response(response) or retries <= 0: - return response + return Network.patch_response(response, do_raise_for_httperror) except httpx.RemoteProtocolError as e: if not was_disconnected: # the server has closed the connection: |