From 9b41f1fcbed2c4b154865ceaff8c5f9d8f6f9970 Mon Sep 17 00:00:00 2001 From: Roman Snegirev Date: Fri, 1 Mar 2024 10:13:25 +0300 Subject: [PATCH] Map httpcore exceptions to httpx ones --- httpx_socks/_async_transport.py | 21 ++++++--------------- httpx_socks/_sync_transport.py | 21 ++++++--------------- tests/test_transport_asyncio.py | 4 ++-- tests/test_transport_sync.py | 4 ++-- tests/test_transport_trio.py | 5 +++-- 5 files changed, 19 insertions(+), 36 deletions(-) diff --git a/httpx_socks/_async_transport.py b/httpx_socks/_async_transport.py index 46837aa..b15405b 100644 --- a/httpx_socks/_async_transport.py +++ b/httpx_socks/_async_transport.py @@ -6,24 +6,13 @@ # noinspection PyProtectedMember from httpx._config import DEFAULT_LIMITS, create_ssl_context +# noinspection PyProtectedMember +from httpx._transports.default import AsyncResponseStream, map_httpcore_exceptions from python_socks import ProxyType, parse_proxy_url from ._async_proxy import AsyncProxy -class AsyncResponseStream(AsyncByteStream): - def __init__(self, httpcore_stream: typing.AsyncIterable[bytes]): - self._httpcore_stream = httpcore_stream - - async def __aiter__(self) -> typing.AsyncIterator[bytes]: - async for part in self._httpcore_stream: - yield part - - async def aclose(self) -> None: - if hasattr(self._httpcore_stream, "aclose"): - await self._httpcore_stream.aclose() # type: ignore - - class AsyncProxyTransport(AsyncBaseTransport): def __init__( self, @@ -79,7 +68,8 @@ async def handle_async_request(self, request: Request) -> Response: extensions=request.extensions, ) - resp = await self._pool.handle_async_request(req) + with map_httpcore_exceptions(): + resp = await self._pool.handle_async_request(req) assert isinstance(resp.stream, typing.AsyncIterable) @@ -110,4 +100,5 @@ async def __aenter__(self): return self async def __aexit__(self, exc_type=None, exc_value=None, traceback=None): - await self._pool.__aexit__(exc_type, exc_value, traceback) + with map_httpcore_exceptions(): + await self._pool.__aexit__(exc_type, exc_value, traceback) diff --git a/httpx_socks/_sync_transport.py b/httpx_socks/_sync_transport.py index 61449b5..179f86b 100644 --- a/httpx_socks/_sync_transport.py +++ b/httpx_socks/_sync_transport.py @@ -7,24 +7,13 @@ # noinspection PyProtectedMember from httpx._config import DEFAULT_LIMITS, create_ssl_context +# noinspection PyProtectedMember +from httpx._transports.default import ResponseStream, map_httpcore_exceptions from ._sync_proxy import SyncProxy from python_socks import ProxyType, parse_proxy_url -class ResponseStream(SyncByteStream): - def __init__(self, httpcore_stream: typing.Iterable[bytes]): - self._httpcore_stream = httpcore_stream - - def __iter__(self) -> typing.Iterator[bytes]: - for part in self._httpcore_stream: - yield part - - def close(self) -> None: - if hasattr(self._httpcore_stream, "close"): - self._httpcore_stream.close() # type: ignore - - class SyncProxyTransport(BaseTransport): def __init__( self, @@ -80,7 +69,8 @@ def handle_request(self, request: Request) -> Response: extensions=request.extensions, ) - resp = self._pool.handle_request(req) + with map_httpcore_exceptions(): + resp = self._pool.handle_request(req) assert isinstance(resp.stream, typing.Iterable) @@ -111,4 +101,5 @@ def __enter__(self): return self def __exit__(self, exc_type=None, exc_value=None, traceback=None): - self._pool.__exit__(exc_type, exc_value, traceback) + with map_httpcore_exceptions(): + self._pool.__exit__(exc_type, exc_value, traceback) diff --git a/tests/test_transport_asyncio.py b/tests/test_transport_asyncio.py index 5841581..f4a5b58 100644 --- a/tests/test_transport_asyncio.py +++ b/tests/test_transport_asyncio.py @@ -1,6 +1,5 @@ import ssl -import httpcore import httpx import pytest from yarl import URL @@ -103,7 +102,8 @@ async def test_socks5_proxy_with_read_timeout(target_ssl_ca, url=TEST_URL_IPV4_D verify=create_ssl_context(url, ca=target_ssl_ca), ) timeout = httpx.Timeout(2, connect=32) - with pytest.raises(httpcore.ReadTimeout): + # with pytest.raises(httpcore.ReadTimeout): + with pytest.raises(httpx.ReadTimeout): await fetch(transport=transport, url=url, timeout=timeout) diff --git a/tests/test_transport_sync.py b/tests/test_transport_sync.py index d298d0b..5f23b81 100644 --- a/tests/test_transport_sync.py +++ b/tests/test_transport_sync.py @@ -1,6 +1,5 @@ import ssl -import httpcore import httpx import pytest from yarl import URL @@ -94,7 +93,8 @@ def test_socks5_proxy_with_read_timeout(target_ssl_ca, url=TEST_URL_IPV4_DELAY): verify=create_ssl_context(url, ca=target_ssl_ca), ) timeout = httpx.Timeout(2, connect=32) - with pytest.raises(httpcore.ReadTimeout): + # with pytest.raises(httpcore.ReadTimeout): + with pytest.raises(httpx.ReadTimeout): fetch(transport=transport, url=url, timeout=timeout) diff --git a/tests/test_transport_trio.py b/tests/test_transport_trio.py index 3bf852f..78bb3ff 100644 --- a/tests/test_transport_trio.py +++ b/tests/test_transport_trio.py @@ -1,6 +1,6 @@ import ssl -import httpcore + import httpx import pytest from yarl import URL @@ -102,7 +102,8 @@ async def test_socks5_proxy_with_read_timeout(target_ssl_ca, url=TEST_URL_IPV4_D verify=create_ssl_context(url, ca=target_ssl_ca), ) timeout = httpx.Timeout(2, connect=32) - with pytest.raises(httpcore.ReadTimeout): + # with pytest.raises(httpcore.ReadTimeout): + with pytest.raises(httpx.ReadTimeout): await fetch(transport=transport, url=url, timeout=timeout)