File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,15 @@ class Client(BaseClient):
113
113
def __init__ (self , host : Optional [str ] = None , ** kwargs ) -> None :
114
114
super ().__init__ (httpx .Client , host , ** kwargs )
115
115
116
+ def close (self ):
117
+ self ._client .close ()
118
+
119
+ def __enter__ (self ):
120
+ return self
121
+
122
+ def __exit__ (self , exc_type , exc_val , exc_tb ):
123
+ self .close ()
124
+
116
125
def _request_raw (self , * args , ** kwargs ):
117
126
try :
118
127
r = self ._client .request (* args , ** kwargs )
@@ -617,6 +626,15 @@ class AsyncClient(BaseClient):
617
626
def __init__ (self , host : Optional [str ] = None , ** kwargs ) -> None :
618
627
super ().__init__ (httpx .AsyncClient , host , ** kwargs )
619
628
629
+ async def close (self ):
630
+ await self ._client .aclose ()
631
+
632
+ async def __aenter__ (self ):
633
+ return self
634
+
635
+ async def __aexit__ (self , exc_type , exc_val , exc_tb ):
636
+ await self .close ()
637
+
620
638
async def _request_raw (self , * args , ** kwargs ):
621
639
try :
622
640
r = await self ._client .request (* args , ** kwargs )
Original file line number Diff line number Diff line change @@ -1140,3 +1140,33 @@ async def test_async_client_connection_error():
1140
1140
with pytest .raises (ConnectionError ) as exc_info :
1141
1141
await client .show ('model' )
1142
1142
assert str (exc_info .value ) == 'Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download'
1143
+
1144
+
1145
+ def test_client_close ():
1146
+ client = Client ()
1147
+ client .close ()
1148
+ assert client ._client .is_closed
1149
+
1150
+
1151
+ @pytest .mark .asyncio
1152
+ async def test_async_client_close ():
1153
+ client = AsyncClient ()
1154
+ await client .close ()
1155
+ assert client ._client .is_closed
1156
+
1157
+
1158
+ def test_client_context_manager ():
1159
+ with Client () as client :
1160
+ assert isinstance (client , Client )
1161
+ assert not client ._client .is_closed
1162
+
1163
+ assert client ._client .is_closed
1164
+
1165
+
1166
+ @pytest .mark .asyncio
1167
+ async def test_async_client_context_manager ():
1168
+ async with AsyncClient () as client :
1169
+ assert isinstance (client , AsyncClient )
1170
+ assert not client ._client .is_closed
1171
+
1172
+ assert client ._client .is_closed
You can’t perform that action at this time.
0 commit comments