Skip to content

Commit 11eecb9

Browse files
committed
refactor: Extract common HTTP argument parsing logic into a shared helper function used by REST and JSON-RPC transports.
1 parent b0f2033 commit 11eecb9

3 files changed

Lines changed: 16 additions & 24 deletions

File tree

src/a2a/client/transports/http_helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from httpx_sse import SSEError, aconnect_sse
1010

1111
from a2a.client.errors import A2AClientError, A2AClientTimeoutError
12+
from a2a.client.middleware import ClientCallContext
1213

1314

1415
@contextmanager
@@ -40,6 +41,15 @@ def handle_http_exceptions(
4041
except json.JSONDecodeError as e:
4142
raise A2AClientError(f'JSON Decode Error: {e}') from e
4243

44+
def get_http_args(
45+
context: ClientCallContext | None
46+
) -> dict[str, Any]:
47+
http_kwargs: dict[str, Any] = {}
48+
if context and context.service_parameters:
49+
http_kwargs['headers'] = context.service_parameters.copy()
50+
if context and context.timeout is not None:
51+
http_kwargs['timeout'] = httpx.Timeout(context.timeout)
52+
return http_kwargs
4353

4454
async def send_http_request(
4555
httpx_client: httpx.AsyncClient,

src/a2a/client/transports/jsonrpc.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from a2a.client.transports.http_helpers import (
1717
send_http_request,
1818
send_http_stream_request,
19+
get_http_args,
1920
)
2021
from a2a.types.a2a_pb2 import (
2122
AgentCard,
@@ -322,16 +323,6 @@ async def close(self) -> None:
322323
"""Closes the httpx client."""
323324
await self.httpx_client.aclose()
324325

325-
def _get_http_args(
326-
self, context: ClientCallContext | None
327-
) -> dict[str, Any]:
328-
http_kwargs: dict[str, Any] = {}
329-
if context and context.service_parameters:
330-
http_kwargs['headers'] = context.service_parameters.copy()
331-
if context and context.timeout is not None:
332-
http_kwargs['timeout'] = httpx.Timeout(context.timeout)
333-
return http_kwargs
334-
335326
def _create_jsonrpc_error(self, error_dict: dict[str, Any]) -> Exception:
336327
"""Creates the appropriate A2AError from a JSON-RPC error dictionary."""
337328
code = error_dict.get('code')
@@ -348,7 +339,7 @@ async def _send_request(
348339
payload: dict[str, Any],
349340
context: ClientCallContext | None = None,
350341
) -> dict[str, Any]:
351-
http_kwargs = self._get_http_args(context)
342+
http_kwargs = get_http_args(context)
352343

353344
request = self.httpx_client.build_request(
354345
'POST', self.url, json=payload, **(http_kwargs or {})
@@ -360,7 +351,7 @@ async def _send_stream_request(
360351
rpc_request_payload: dict[str, Any],
361352
context: ClientCallContext | None = None,
362353
) -> AsyncGenerator[StreamResponse]:
363-
http_kwargs = self._get_http_args(context)
354+
http_kwargs = get_http_args(context)
364355

365356
async for sse_data in send_http_stream_request(
366357
self.httpx_client,

src/a2a/client/transports/rest.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from a2a.client.transports.http_helpers import (
1616
send_http_request,
1717
send_http_stream_request,
18+
get_http_args,
1819
)
1920
from a2a.types.a2a_pb2 import (
2021
AgentCard,
@@ -295,16 +296,6 @@ def _get_path(self, base_path: str, tenant: str) -> str:
295296
"""Returns the full path, prepending the tenant if provided."""
296297
return f'/{tenant}{base_path}' if tenant else base_path
297298

298-
def _get_http_args(
299-
self, context: ClientCallContext | None
300-
) -> dict[str, Any]:
301-
http_kwargs: dict[str, Any] = {}
302-
if context and context.service_parameters:
303-
http_kwargs['headers'] = context.service_parameters.copy()
304-
if context and context.timeout is not None:
305-
http_kwargs['timeout'] = httpx.Timeout(context.timeout)
306-
return http_kwargs
307-
308299
def _handle_http_error(self, e: httpx.HTTPStatusError) -> NoReturn:
309300
"""Handles HTTP status errors and raises the appropriate A2AError."""
310301
try:
@@ -339,7 +330,7 @@ async def _send_stream_request(
339330
json: dict[str, Any] | None = None,
340331
) -> AsyncGenerator[StreamResponse]:
341332
path = self._get_path(target, tenant)
342-
http_kwargs = self._get_http_args(context)
333+
http_kwargs = get_http_args(context)
343334

344335
async for sse_data in send_http_stream_request(
345336
self.httpx_client,
@@ -368,7 +359,7 @@ async def _execute_request(
368359
params: dict[str, Any] | None = None,
369360
) -> dict[str, Any]:
370361
path = self._get_path(target, tenant)
371-
http_kwargs = self._get_http_args(context)
362+
http_kwargs = get_http_args(context)
372363

373364
request = self.httpx_client.build_request(
374365
method,

0 commit comments

Comments
 (0)