From 73d11e80eb064b98030aa041a37aca31460db9ae Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 15 Dec 2023 13:34:52 +0000 Subject: [PATCH] fix httpx resource warnings --- tests/integration/test_httpx.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/integration/test_httpx.py b/tests/integration/test_httpx.py index 723daed..d81b065 100644 --- a/tests/integration/test_httpx.py +++ b/tests/integration/test_httpx.py @@ -28,21 +28,27 @@ class DoSyncRequest(BaseDoRequest): _client_class = httpx.Client def __enter__(self): + self._client = self._make_client() return self def __exit__(self, *args): - pass + self._client.close() + del self._client @property def client(self): try: return self._client - except AttributeError: - self._client = self._make_client() - return self._client + except AttributeError as e: + raise ValueError('To access sync client, use "with do_request() as client"') from e def __call__(self, *args, **kwargs): - return self.client.request(*args, timeout=60, **kwargs) + if hasattr(self, "_client"): + return self.client.request(*args, timeout=60, **kwargs) + + # Use one-time context and dispose of the client afterwards + with self: + return self.client.request(*args, timeout=60, **kwargs) def stream(self, *args, **kwargs): with self.client.stream(*args, **kwargs) as response: