Skip to content

Commit

Permalink
Map httpcore exceptions to httpx ones
Browse files Browse the repository at this point in the history
  • Loading branch information
romis2012 committed Mar 1, 2024
1 parent 23c0050 commit 9b41f1f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 36 deletions.
21 changes: 6 additions & 15 deletions httpx_socks/_async_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
21 changes: 6 additions & 15 deletions httpx_socks/_sync_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions tests/test_transport_asyncio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ssl

import httpcore
import httpx
import pytest
from yarl import URL
Expand Down Expand Up @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_transport_sync.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ssl

import httpcore
import httpx
import pytest
from yarl import URL
Expand Down Expand Up @@ -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)


Expand Down
5 changes: 3 additions & 2 deletions tests/test_transport_trio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ssl

import httpcore

import httpx
import pytest
from yarl import URL
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 9b41f1f

Please sign in to comment.