@@ -24,20 +24,12 @@ async def sse_client(
24
24
headers : dict [str , Any ] | None = None ,
25
25
timeout : float = 5 ,
26
26
sse_read_timeout : float = 60 * 5 ,
27
- client : httpx .AsyncClient | None = None ,
28
27
):
29
28
"""
30
29
Client transport for SSE.
31
30
32
31
`sse_read_timeout` determines how long (in seconds) the client will wait for a new
33
32
event before disconnecting. All other HTTP operations are controlled by `timeout`.
34
-
35
- Args:
36
- url: The URL to connect to
37
- headers: Optional headers to send with the request
38
- timeout: Connection timeout in seconds
39
- sse_read_timeout: Read timeout in seconds
40
- client: Optional httpx.AsyncClient instance to use for requests
41
33
"""
42
34
read_stream : MemoryObjectReceiveStream [types .JSONRPCMessage | Exception ]
43
35
read_stream_writer : MemoryObjectSendStream [types .JSONRPCMessage | Exception ]
@@ -51,13 +43,7 @@ async def sse_client(
51
43
async with anyio .create_task_group () as tg :
52
44
try :
53
45
logger .info (f"Connecting to SSE endpoint: { remove_request_params (url )} " )
54
- if client is None :
55
- client = httpx .AsyncClient (headers = headers )
56
- should_close_client = True
57
- else :
58
- should_close_client = False
59
-
60
- try :
46
+ async with httpx .AsyncClient (headers = headers ) as client :
61
47
async with aconnect_sse (
62
48
client ,
63
49
"GET" ,
@@ -151,9 +137,6 @@ async def post_writer(endpoint_url: str):
151
137
yield read_stream , write_stream
152
138
finally :
153
139
tg .cancel_scope .cancel ()
154
- finally :
155
- if should_close_client :
156
- await client .aclose ()
157
140
finally :
158
141
await read_stream_writer .aclose ()
159
142
await write_stream .aclose ()
0 commit comments