11import json
22import logging
3+
34from collections .abc import AsyncGenerator
45from typing import Any
56from uuid import uuid4
67
78import httpx
9+
810from httpx_sse import SSEError , aconnect_sse
911from pydantic import ValidationError
1012
1113from a2a .client .errors import A2AClientHTTPError , A2AClientJSONError
12- from a2a .types import (AgentCard , CancelTaskRequest , CancelTaskResponse ,
13- GetTaskPushNotificationConfigRequest ,
14- GetTaskPushNotificationConfigResponse , GetTaskRequest ,
15- GetTaskResponse , SendMessageRequest ,
16- SendMessageResponse , SendStreamingMessageRequest ,
17- SendStreamingMessageResponse ,
18- SetTaskPushNotificationConfigRequest ,
19- SetTaskPushNotificationConfigResponse )
14+ from a2a .types import (
15+ AgentCard ,
16+ CancelTaskRequest ,
17+ CancelTaskResponse ,
18+ GetTaskPushNotificationConfigRequest ,
19+ GetTaskPushNotificationConfigResponse ,
20+ GetTaskRequest ,
21+ GetTaskResponse ,
22+ SendMessageRequest ,
23+ SendMessageResponse ,
24+ SendStreamingMessageRequest ,
25+ SendStreamingMessageResponse ,
26+ SetTaskPushNotificationConfigRequest ,
27+ SetTaskPushNotificationConfigResponse ,
28+ )
2029from a2a .utils .telemetry import SpanKind , trace_class
2130
22- logger = logging .getLogger (__name__ )
2331
24- async def _make_httpx_request (
25- client : httpx .AsyncClient ,
26- method : str ,
27- url : str ,
28- json_payload : dict [str , Any ] | None = None ,
29- http_kwargs : dict [str , Any ] | None = None ,
30- ) -> dict [str , Any ]:
31- """Makes an HTTP request and handles common errors, returning parsed JSON."""
32- try :
33- if method .upper () == 'GET' :
34- response = await client .get (url , ** (http_kwargs or {}))
35- elif method .upper () == 'POST' :
36- response = await client .post (
37- url , json = json_payload , ** (http_kwargs or {})
38- )
39- else :
40- raise ValueError (f'Unsupported HTTP method: { method } ' )
41-
42- response .raise_for_status ()
43- return response .json ()
44- except httpx .HTTPStatusError as e :
45- raise A2AClientHTTPError (e .response .status_code , str (e )) from e
46- except json .JSONDecodeError as e :
47- raise A2AClientJSONError (str (e )) from e
48- except httpx .RequestError as e :
49- raise A2AClientHTTPError (
50- 503 , f'Network communication error: { e } '
51- ) from e
32+ logger = logging .getLogger (__name__ )
5233
5334
5435class A2ACardResolver :
@@ -135,6 +116,7 @@ async def get_agent_card(
135116 raise A2AClientJSONError (
136117 f'Failed to validate agent card structure from { target_url } : { e .json ()} '
137118 ) from e
119+
138120 return agent_card
139121
140122
@@ -189,6 +171,7 @@ async def get_client_from_agent_card_url(
189171 agent_card_path: The path to the agent card endpoint, relative to the base URL.
190172 http_kwargs: Optional dictionary of keyword arguments to pass to the
191173 underlying httpx.get request when fetching the agent card.
174+
192175 Returns:
193176 An initialized `A2AClient` instance.
194177
@@ -198,7 +181,9 @@ async def get_client_from_agent_card_url(
198181 """
199182 agent_card : AgentCard = await A2ACardResolver (
200183 httpx_client , base_url = base_url , agent_card_path = agent_card_path
201- ).get_agent_card (http_kwargs = http_kwargs ) # Fetches public card by default
184+ ).get_agent_card (
185+ http_kwargs = http_kwargs
186+ ) # Fetches public card by default
202187 return A2AClient (httpx_client = httpx_client , agent_card = agent_card )
203188
204189 async def send_message (
@@ -304,13 +289,20 @@ async def _send_request(
304289 A2AClientHTTPError: If an HTTP error occurs during the request.
305290 A2AClientJSONError: If the response body cannot be decoded as JSON.
306291 """
307- return await _make_httpx_request (
308- client = self .httpx_client ,
309- method = 'POST' ,
310- url = self .url ,
311- json_payload = rpc_request_payload ,
312- http_kwargs = http_kwargs ,
313- )
292+ try :
293+ response = await self .httpx_client .post (
294+ self .url , json = rpc_request_payload , ** (http_kwargs or {})
295+ )
296+ response .raise_for_status ()
297+ return response .json ()
298+ except httpx .HTTPStatusError as e :
299+ raise A2AClientHTTPError (e .response .status_code , str (e )) from e
300+ except json .JSONDecodeError as e :
301+ raise A2AClientJSONError (str (e )) from e
302+ except httpx .RequestError as e :
303+ raise A2AClientHTTPError (
304+ 503 , f'Network communication error: { e } '
305+ ) from e
314306
315307 async def get_task (
316308 self ,
0 commit comments