1- import json
21import logging
32
43from collections .abc import AsyncGenerator , Callable
87import httpx
98
109from google .protobuf import json_format
11- from httpx_sse import SSEError , aconnect_sse
1210from jsonrpc .jsonrpc2 import JSONRPC20Request , JSONRPC20Response
1311
1412from a2a .client .errors import A2AClientError
1513from a2a .client .middleware import ClientCallContext , ClientCallInterceptor
1614from a2a .client .transports .base import ClientTransport
15+ from a2a .client .transports .http_helpers import (
16+ send_http_request ,
17+ send_http_stream_request ,
18+ )
1719from a2a .extensions .common import update_extension_header
1820from a2a .types .a2a_pb2 import (
1921 AgentCard ,
@@ -470,22 +472,10 @@ async def _send_request(
470472 rpc_request_payload : dict [str , Any ],
471473 http_kwargs : dict [str , Any ] | None = None ,
472474 ) -> dict [str , Any ]:
473- try :
474- response = await self .httpx_client .post (
475- self .url , json = rpc_request_payload , ** (http_kwargs or {})
476- )
477- response .raise_for_status ()
478- return response .json ()
479- except httpx .TimeoutException as e :
480- raise A2AClientError ('Client Request timed out' ) from e
481- except httpx .HTTPStatusError as e :
482- raise A2AClientError (
483- f'HTTP Error { e .response .status_code } : { e } '
484- ) from e
485- except json .JSONDecodeError as e :
486- raise A2AClientError (str (e )) from e
487- except httpx .RequestError as e :
488- raise A2AClientError (f'Network communication error: { e } ' ) from e
475+ request = self .httpx_client .build_request (
476+ 'POST' , self .url , json = rpc_request_payload , ** (http_kwargs or {})
477+ )
478+ return await send_http_request (self .httpx_client , request )
489479
490480 async def _send_stream_request (
491481 self ,
@@ -500,39 +490,18 @@ async def _send_stream_request(
500490 headers .update (final_kwargs .get ('headers' , {}))
501491 final_kwargs ['headers' ] = headers
502492
503- try :
504- async with aconnect_sse (
505- self .httpx_client ,
506- 'POST' ,
507- self .url ,
508- json = rpc_request_payload ,
509- ** final_kwargs ,
510- ) as event_source :
511- try :
512- event_source .response .raise_for_status ()
513- async for sse in event_source .aiter_sse ():
514- if not sse .data :
515- continue
516- json_rpc_response = JSONRPC20Response .from_json (
517- sse .data
518- )
519- if json_rpc_response .error :
520- self ._handle_jsonrpc_error (json_rpc_response .error )
521- response : StreamResponse = json_format .ParseDict (
522- json_rpc_response .result , StreamResponse ()
523- )
524- yield response
525- except httpx .HTTPStatusError as e :
526- raise A2AClientError (
527- f'HTTP Error { e .response .status_code } : { e } '
528- ) from e
529- except SSEError as e :
530- raise A2AClientError (
531- f'Invalid SSE response or protocol error: { e } '
532- ) from e
533- except httpx .TimeoutException as e :
534- raise A2AClientError ('Client Request timed out' ) from e
535- except httpx .RequestError as e :
536- raise A2AClientError (f'Network communication error: { e } ' ) from e
537- except json .JSONDecodeError as e :
538- raise A2AClientError (str (e )) from e
493+ async for sse_data in send_http_stream_request (
494+ self .httpx_client ,
495+ 'POST' ,
496+ self .url ,
497+ None ,
498+ json = rpc_request_payload ,
499+ ** final_kwargs ,
500+ ):
501+ json_rpc_response = JSONRPC20Response .from_json (sse_data )
502+ if json_rpc_response .error :
503+ self ._handle_jsonrpc_error (json_rpc_response .error )
504+ response : StreamResponse = json_format .ParseDict (
505+ json_rpc_response .result , StreamResponse ()
506+ )
507+ yield response
0 commit comments