fix(client): correct destructor the httpx object boht sync and async (#636)

Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com>
This commit is contained in:
Aaron Pham
2023-11-13 18:11:17 -05:00
committed by GitHub
parent de06557844
commit 853def95cd
2 changed files with 14 additions and 3 deletions

View File

@@ -46,9 +46,6 @@ class HTTPClient(Client):
return {'Authorization': f'Bearer {env}'}
return super()._build_auth_headers()
def __del__(self):
self.close()
@property
def _metadata(self):
if self.__metadata is None:

View File

@@ -1,5 +1,6 @@
# This provides a base shim with httpx and acts as base request
from __future__ import annotations
import asyncio
import email.utils
import logging
import platform
@@ -374,6 +375,9 @@ class Client(BaseClient[httpx.Client, Stream[t.Any]]):
def __exit__(self, *args) -> None:
self.close()
def __del__(self):
self.close()
def request(
self,
response_cls: type[Response],
@@ -504,6 +508,16 @@ class AsyncClient(BaseClient[httpx.AsyncClient, AsyncStream[t.Any]]):
async def __aexit__(self, *args) -> None:
await self.close()
def __del__(self):
try:
loop = asyncio.get_event_loop()
if loop.is_running():
loop.create_task(self.close())
else:
loop.run_until_complete(self.close())
except Exception:
pass
async def request(
self,
response_cls: type[Response],