Skip to content

Commit dc5b953

Browse files
feat(client): expose close() and async context manager support on abstract Client
1 parent 59b8871 commit dc5b953

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/a2a/client/client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
from abc import ABC, abstractmethod
55
from collections.abc import AsyncIterator, Callable, Coroutine
6+
from types import TracebackType
67
from typing import Any
78

9+
from typing_extensions import Self
10+
811
import httpx
912

1013
from a2a.client.middleware import ClientCallContext, ClientCallInterceptor
@@ -106,6 +109,19 @@ def __init__(
106109
self._consumers = consumers
107110
self._middleware = middleware
108111

112+
async def __aenter__(self) -> Self:
113+
"""Enters the async context manager."""
114+
return self
115+
116+
async def __aexit__(
117+
self,
118+
exc_type: type[BaseException] | None,
119+
exc_val: BaseException | None,
120+
exc_tb: TracebackType | None,
121+
) -> None:
122+
"""Exits the async context manager and closes the client."""
123+
await self.close()
124+
109125
@abstractmethod
110126
async def send_message(
111127
self,
@@ -217,3 +233,7 @@ async def consume(
217233
return
218234
for c in self._consumers:
219235
await c(event, card)
236+
237+
@abstractmethod
238+
async def close(self) -> None:
239+
"""Closes the client and releases any underlying resources."""

0 commit comments

Comments
 (0)