Skip to content

Commit 337ae1f

Browse files
lesnik512claude
andcommitted
feat(client): send() + build_request(), terminal error mapping
Add AsyncClient.send() (with response_model overloads) and build_request() delegating to the wrapped httpx2.AsyncClient. Tests cover 2xx passthrough, typed 4xx/5xx subclass raises, fallback to ClientStatusError/ServerStatusError, 3xx passthrough, timeout→TimeoutError, connect→TransportError, and closed-client→TransportError. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 966da64 commit 337ae1f

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

‎src/httpware/client.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,25 @@ async def _terminal(self, request: httpx2.Request) -> httpx2.Response:
110110
)
111111
raise exc_class(response)
112112
return response
113+
114+
@typing.overload
115+
async def send(self, request: httpx2.Request, *, response_model: None = None) -> httpx2.Response: ...
116+
117+
@typing.overload
118+
async def send(self, request: httpx2.Request, *, response_model: type[T]) -> T: ...
119+
120+
async def send(
121+
self,
122+
request: httpx2.Request,
123+
*,
124+
response_model: type[T] | None = None,
125+
) -> httpx2.Response | T:
126+
"""Send `request` through the middleware chain. Decode if `response_model` is set."""
127+
response = await self._dispatch(request)
128+
if response_model is None:
129+
return response
130+
return self._decoder.decode(response.content, response_model)
131+
132+
def build_request(self, method: str, url: str, **kwargs: typing.Any) -> httpx2.Request: # noqa: ANN401 — mirrors httpx2.AsyncClient.build_request kwargs
133+
"""Delegate request construction to the wrapped httpx2.AsyncClient."""
134+
return self._httpx2_client.build_request(method, url, **kwargs)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""Tests for the AsyncClient internal terminal's exception mapping."""
2+
3+
from http import HTTPStatus
4+
5+
import httpx2
6+
import pytest
7+
8+
from httpware import (
9+
AsyncClient,
10+
BadRequestError,
11+
ClientStatusError,
12+
InternalServerError,
13+
NotFoundError,
14+
RateLimitedError,
15+
ServerStatusError,
16+
StatusError,
17+
TimeoutError, # noqa: A004
18+
TransportError,
19+
)
20+
21+
22+
def _client_with_handler(handler) -> AsyncClient: # noqa: ANN001
23+
transport = httpx2.MockTransport(handler)
24+
return AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport))
25+
26+
27+
async def test_terminal_returns_response_on_2xx() -> None:
28+
client = _client_with_handler(lambda req: httpx2.Response(HTTPStatus.OK, json={"ok": True}, request=req))
29+
response = await client.send(httpx2.Request("GET", "https://example.test/x"))
30+
assert response.status_code == HTTPStatus.OK
31+
assert response.json() == {"ok": True}
32+
33+
34+
@pytest.mark.parametrize(
35+
("status", "exc_type"),
36+
[
37+
(HTTPStatus.BAD_REQUEST, BadRequestError),
38+
(HTTPStatus.NOT_FOUND, NotFoundError),
39+
(HTTPStatus.TOO_MANY_REQUESTS, RateLimitedError),
40+
(HTTPStatus.INTERNAL_SERVER_ERROR, InternalServerError),
41+
],
42+
)
43+
async def test_known_status_codes_raise_typed_subclass(status: int, exc_type: type[StatusError]) -> None:
44+
client = _client_with_handler(lambda req: httpx2.Response(status, request=req))
45+
with pytest.raises(exc_type) as info:
46+
await client.send(httpx2.Request("GET", "https://example.test/x"))
47+
assert info.value.response.status_code == status
48+
49+
50+
async def test_unknown_4xx_falls_back_to_client_status_error() -> None:
51+
client = _client_with_handler(lambda req: httpx2.Response(HTTPStatus.IM_A_TEAPOT, request=req))
52+
with pytest.raises(ClientStatusError) as info:
53+
await client.send(httpx2.Request("GET", "https://example.test/x"))
54+
assert info.value.response.status_code == HTTPStatus.IM_A_TEAPOT
55+
assert type(info.value) is ClientStatusError
56+
57+
58+
async def test_unknown_5xx_falls_back_to_server_status_error() -> None:
59+
client = _client_with_handler(lambda req: httpx2.Response(599, request=req))
60+
with pytest.raises(ServerStatusError) as info:
61+
await client.send(httpx2.Request("GET", "https://example.test/x"))
62+
assert info.value.response.status_code == 599 # noqa: PLR2004
63+
assert type(info.value) is ServerStatusError
64+
65+
66+
async def test_3xx_does_not_raise() -> None:
67+
client = _client_with_handler(
68+
lambda req: httpx2.Response(HTTPStatus.MOVED_PERMANENTLY, request=req, headers={"location": "/y"})
69+
)
70+
response = await client.send(httpx2.Request("GET", "https://example.test/x"))
71+
assert response.status_code == HTTPStatus.MOVED_PERMANENTLY
72+
73+
74+
async def test_httpx2_timeout_maps_to_httpware_timeout() -> None:
75+
def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
76+
msg = "read timeout"
77+
raise httpx2.ReadTimeout(msg)
78+
79+
client = _client_with_handler(handler)
80+
with pytest.raises(TimeoutError, match="read timeout"):
81+
await client.send(httpx2.Request("GET", "https://example.test/x"))
82+
83+
84+
async def test_httpx2_connect_error_maps_to_transport_error() -> None:
85+
def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
86+
msg = "connect refused"
87+
raise httpx2.ConnectError(msg)
88+
89+
client = _client_with_handler(handler)
90+
with pytest.raises(TransportError, match="connect refused"):
91+
await client.send(httpx2.Request("GET", "https://example.test/x"))
92+
93+
94+
async def test_send_on_closed_client_raises_transport_error() -> None:
95+
transport = httpx2.MockTransport(lambda req: httpx2.Response(HTTPStatus.OK, request=req))
96+
underlying = httpx2.AsyncClient(transport=transport)
97+
client = AsyncClient(httpx2_client=underlying)
98+
await underlying.aclose()
99+
with pytest.raises(TransportError):
100+
await client.send(httpx2.Request("GET", "https://example.test/x"))

0 commit comments

Comments
 (0)