33
44from collections .abc import AsyncGenerator
55from typing import Any , NoReturn
6+ from urllib .parse import quote
67
78import httpx
89
1314from a2a .client .transports .base import ClientTransport
1415from a2a .client .transports .http_helpers import (
1516 get_http_args ,
17+ handle_http_exceptions ,
1618 send_http_request ,
1719 send_http_stream_request ,
1820)
@@ -89,6 +91,11 @@ def _parse_rest_error(
8991 return None
9092
9193
94+ def _quote_id (value : str ) -> str :
95+ """Percent-encodes an id so it stays a single URL path segment."""
96+ return quote (value , safe = '' )
97+
98+
9299@trace_class (kind = SpanKind .CLIENT )
93100class RestTransport (ClientTransport ):
94101 """A REST transport for the A2A client."""
@@ -158,7 +165,7 @@ async def get_task(
158165
159166 response_data = await self ._execute_request (
160167 'GET' ,
161- f'/tasks/{ request .id } ' ,
168+ f'/tasks/{ _quote_id ( request .id ) } ' ,
162169 request .tenant ,
163170 context = context ,
164171 params = params ,
@@ -198,7 +205,7 @@ async def cancel_task(
198205 """Requests the agent to cancel a specific task."""
199206 response_data = await self ._execute_request (
200207 'POST' ,
201- f'/tasks/{ request .id } :cancel' ,
208+ f'/tasks/{ _quote_id ( request .id ) } :cancel' ,
202209 request .tenant ,
203210 context = context ,
204211 json = MessageToDict (request ),
@@ -215,7 +222,7 @@ async def create_task_push_notification_config(
215222 """Sets or updates the push notification configuration for a specific task."""
216223 response_data = await self ._execute_request (
217224 'POST' ,
218- f'/tasks/{ request .task_id } /pushNotificationConfigs' ,
225+ f'/tasks/{ _quote_id ( request .task_id ) } /pushNotificationConfigs' ,
219226 request .tenant ,
220227 context = context ,
221228 json = MessageToDict (request ),
@@ -242,7 +249,8 @@ async def get_task_push_notification_config(
242249
243250 response_data = await self ._execute_request (
244251 'GET' ,
245- f'/tasks/{ request .task_id } /pushNotificationConfigs/{ request .id } ' ,
252+ f'/tasks/{ _quote_id (request .task_id )} '
253+ f'/pushNotificationConfigs/{ _quote_id (request .id )} ' ,
246254 request .tenant ,
247255 context = context ,
248256 params = params ,
@@ -267,7 +275,7 @@ async def list_task_push_notification_configs(
267275
268276 response_data = await self ._execute_request (
269277 'GET' ,
270- f'/tasks/{ request .task_id } /pushNotificationConfigs' ,
278+ f'/tasks/{ _quote_id ( request .task_id ) } /pushNotificationConfigs' ,
271279 request .tenant ,
272280 context = context ,
273281 params = params ,
@@ -292,13 +300,24 @@ async def delete_task_push_notification_config(
292300 if 'tenant' in params :
293301 del params ['tenant' ]
294302
295- await self ._execute_request (
296- 'DELETE' ,
297- f'/tasks/ { request . task_id } / pushNotificationConfigs/{ request .id } ' ,
303+ path = self ._get_path (
304+ f'/tasks/ { _quote_id ( request . task_id ) } '
305+ f'/pushNotificationConfigs/{ _quote_id ( request .id ) } ' ,
298306 request .tenant ,
299- context = context ,
307+ )
308+ http_request = self .httpx_client .build_request (
309+ 'DELETE' ,
310+ f'{ self .url } { path } ' ,
300311 params = params ,
312+ ** get_http_args (context ),
301313 )
314+ if self .httpx_client .is_closed :
315+ raise A2AClientError ('HTTP client is closed' )
316+ # The body is not read: servers confirm the deletion with `{}`,
317+ # with 204 No Content, or with a 200 that has no body.
318+ with handle_http_exceptions (self ._handle_http_error ):
319+ response = await self .httpx_client .send (http_request )
320+ response .raise_for_status ()
302321
303322 async def subscribe (
304323 self ,
@@ -309,7 +328,7 @@ async def subscribe(
309328 """Reconnects to get task updates."""
310329 async for event in self ._send_stream_request (
311330 'POST' ,
312- f'/tasks/{ request .id } :subscribe' ,
331+ f'/tasks/{ _quote_id ( request .id ) } :subscribe' ,
313332 request .tenant ,
314333 context = context ,
315334 ):
@@ -399,8 +418,22 @@ async def _send_stream_request(
399418 ** http_kwargs ,
400419 ):
401420 event : StreamResponse = parse_lenient (sse_data , StreamResponse ())
421+ if event .WhichOneof ('payload' ) is None :
422+ self ._raise_in_band_error (sse_data )
402423 yield event
403424
425+ def _raise_in_band_error (self , sse_data : str ) -> None :
426+ """Raises the error carried by a data event, if it carries one.
427+
428+ The HTTP+JSON binding defines no SSE event type for errors, so a
429+ server may send the ``google.rpc.Status`` body as an ordinary data
430+ event instead of an ``event: error`` one. ``StreamResponse`` has no
431+ ``error`` field, so that body parses to an event with no payload.
432+ """
433+ payload = json .loads (sse_data )
434+ if isinstance (payload , dict ) and 'error' in payload :
435+ self ._handle_sse_error (sse_data )
436+
404437 async def _send_request (self , request : httpx .Request ) -> dict [str , Any ]:
405438 return await send_http_request (
406439 self .httpx_client , request , self ._handle_http_error
0 commit comments