2626)
2727
2828
29+ async def _make_httpx_request (
30+ client : httpx .AsyncClient ,
31+ method : str ,
32+ url : str ,
33+ json_payload : dict [str , Any ] | None = None ,
34+ http_kwargs : dict [str , Any ] | None = None ,
35+ ) -> dict [str , Any ]:
36+ """Makes an HTTP request and handles common errors, returning parsed JSON."""
37+ try :
38+ if method .upper () == 'GET' :
39+ response = await client .get (url , ** (http_kwargs or {}))
40+ elif method .upper () == 'POST' :
41+ response = await client .post (
42+ url , json = json_payload , ** (http_kwargs or {})
43+ )
44+ else :
45+ raise ValueError (f'Unsupported HTTP method: { method } ' )
46+
47+ response .raise_for_status ()
48+ return response .json ()
49+ except httpx .HTTPStatusError as e :
50+ raise A2AClientHTTPError (e .response .status_code , str (e )) from e
51+ except json .JSONDecodeError as e :
52+ raise A2AClientJSONError (str (e )) from e
53+ except httpx .RequestError as e :
54+ raise A2AClientHTTPError (
55+ 503 , f'Network communication error: { e } '
56+ ) from e
57+
58+
2959class A2ACardResolver :
3060 """Agent Card resolver."""
3161
@@ -42,21 +72,13 @@ def __init__(
4272 async def get_agent_card (
4373 self , http_kwargs : dict [str , Any ] | None = None
4474 ) -> AgentCard :
45- try :
46- response = await self .httpx_client .get (
47- f'{ self .base_url } /{ self .agent_card_path } ' ,
48- ** (http_kwargs or {}),
49- )
50- response .raise_for_status ()
51- return AgentCard .model_validate (response .json ())
52- except httpx .HTTPStatusError as e :
53- raise A2AClientHTTPError (e .response .status_code , str (e )) from e
54- except json .JSONDecodeError as e :
55- raise A2AClientJSONError (str (e )) from e
56- except httpx .RequestError as e :
57- raise A2AClientHTTPError (
58- 503 , f'Network communication error: { e } '
59- ) from e
75+ response_json = await _make_httpx_request (
76+ client = self .httpx_client ,
77+ method = 'GET' ,
78+ url = f'{ self .base_url } /{ self .agent_card_path } ' ,
79+ http_kwargs = http_kwargs ,
80+ )
81+ return AgentCard .model_validate (response_json )
6082
6183
6284class A2AClient :
@@ -152,22 +174,15 @@ async def _send_request(
152174
153175 Args:
154176 rpc_request_payload: JSON RPC payload for sending the request
155- **kwargs : Additional keyword arguments to pass to the httpx client.
177+ http_kwargs : Additional keyword arguments to pass to the httpx client.
156178 """
157- try :
158- response = await self .httpx_client .post (
159- self .url , json = rpc_request_payload , ** (http_kwargs or {})
160- )
161- response .raise_for_status ()
162- return response .json ()
163- except httpx .HTTPStatusError as e :
164- raise A2AClientHTTPError (e .response .status_code , str (e )) from e
165- except json .JSONDecodeError as e :
166- raise A2AClientJSONError (str (e )) from e
167- except httpx .RequestError as e :
168- raise A2AClientHTTPError (
169- 503 , f'Network communication error: { e } '
170- ) from e
179+ return await _make_httpx_request (
180+ client = self .httpx_client ,
181+ method = 'POST' ,
182+ url = self .url ,
183+ json_payload = rpc_request_payload ,
184+ http_kwargs = http_kwargs ,
185+ )
171186
172187 async def get_task (
173188 self ,
0 commit comments