Skip to content

Commit 814628e

Browse files
authored
refactor: remove unnecesary httpx usage in http_helpers (a2aproject#1412)
# Description Refactored timeout and headers creation in `http_helpers.py` to use plain Python types (`float` and `dict`) instead of `httpx.Timeout` and `httpx.Headers`. Avoids coupling these helpers to `httpx` types, which prepares for the upcoming `httpx` / `httpx2` migration (we plan to allow using both httpx and httpx2 connection, thus helpers shouldn't rely on specific version) ## Tests Updated existing REST/JSON-RPC timeout assertions to expect a raw `float` (was: `httpx.Timeout`) Added unit tests in `test_http_helpers.py` for unwrapped timeout pass-through and case-insensitive SSE default headers. Refs a2aproject#1288
1 parent 11f1116 commit 814628e

4 files changed

Lines changed: 58 additions & 6 deletions

File tree

‎src/a2a/client/transports/http_helpers.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_http_args(context: ClientCallContext | None) -> dict[str, Any]:
4646
if context and context.service_parameters:
4747
http_kwargs['headers'] = context.service_parameters.copy()
4848
if context and context.timeout is not None:
49-
http_kwargs['timeout'] = httpx.Timeout(context.timeout)
49+
http_kwargs['timeout'] = context.timeout
5050
return http_kwargs
5151

5252

@@ -179,9 +179,12 @@ def __init__(
179179
url: str,
180180
**kwargs: Any,
181181
) -> None:
182-
headers = httpx.Headers(kwargs.pop('headers', None))
183-
headers.setdefault('Accept', 'text/event-stream')
184-
headers.setdefault('Cache-Control', 'no-store')
182+
headers: dict[str, str] = dict(kwargs.pop('headers', None) or {})
183+
present = {key.lower() for key in headers}
184+
if 'accept' not in present:
185+
headers['Accept'] = 'text/event-stream'
186+
if 'cache-control' not in present:
187+
headers['Cache-Control'] = 'no-store'
185188
self._request = client.build_request(
186189
method, url, headers=headers, **kwargs
187190
)

‎tests/client/transports/test_http_helpers.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import httpx
22
import pytest
33

4+
from a2a.client.client import ClientCallContext
45
from a2a.client.errors import A2AClientError
56
from a2a.client.transports.http_helpers import (
7+
_SSEEventSource,
68
_default_sse_error_handler,
9+
get_http_args,
710
parse_sse_stream,
811
send_http_stream_request,
912
)
@@ -62,3 +65,49 @@ async def test_send_http_stream_request_non_sse(mocker):
6265
c async for c in send_http_stream_request(client, 'GET', 'http://test')
6366
]
6467
assert chunks == ['plain error response']
68+
69+
70+
def test_get_http_args_passes_timeout_through_unwrapped():
71+
"""The timeout stays a bare float, for the client itself to wrap."""
72+
http_kwargs = get_http_args(ClientCallContext(timeout=5.0))
73+
74+
assert http_kwargs['timeout'] == 5.0
75+
assert not isinstance(http_kwargs['timeout'], httpx.Timeout)
76+
77+
request = httpx.AsyncClient().build_request(
78+
'GET', 'http://test', **http_kwargs
79+
)
80+
assert request.extensions['timeout'] == {
81+
'connect': 5.0,
82+
'read': 5.0,
83+
'write': 5.0,
84+
'pool': 5.0,
85+
}
86+
87+
88+
def test_sse_event_source_sets_default_stream_headers():
89+
source = _SSEEventSource(httpx.AsyncClient(), 'GET', 'http://test')
90+
91+
assert source._request.headers['Accept'] == 'text/event-stream'
92+
assert source._request.headers['Cache-Control'] == 'no-store'
93+
94+
95+
@pytest.mark.parametrize(
96+
'caller_spelling', ['accept', 'Accept', 'ACCEPT', 'AcCePt']
97+
)
98+
def test_sse_event_source_keeps_caller_accept_whatever_its_case(
99+
caller_spelling: str,
100+
):
101+
"""A caller's own Accept wins, and is never joined by a second one.
102+
103+
HTTP header names are case-insensitive, so matching the caller's
104+
spelling exactly is not enough to detect that they already set one.
105+
"""
106+
source = _SSEEventSource(
107+
httpx.AsyncClient(),
108+
'GET',
109+
'http://test',
110+
headers={caller_spelling: 'application/json'},
111+
)
112+
113+
assert source._request.headers.get_list('accept') == ['application/json']

‎tests/client/transports/test_jsonrpc_client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ async def test_send_message_with_timeout_context(
245245
mock_httpx_client.build_request.assert_called_once()
246246
_, kwargs = mock_httpx_client.build_request.call_args
247247
assert 'timeout' in kwargs
248-
assert kwargs['timeout'] == httpx.Timeout(15.0)
248+
assert kwargs['timeout'] == 15.0
249249

250250

251251
class TestGetTask:

‎tests/client/transports/test_rest_client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ async def test_send_message_with_timeout_context(
220220
mock_build_request.assert_called_once()
221221
_, kwargs = mock_build_request.call_args
222222
assert 'timeout' in kwargs
223-
assert kwargs['timeout'] == httpx.Timeout(10.0)
223+
assert kwargs['timeout'] == 10.0
224224

225225
@pytest.mark.asyncio
226226
async def test_url_serialization(

0 commit comments

Comments
 (0)