mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
fix httpx resource warnings
This commit is contained in:
@@ -28,21 +28,27 @@ class DoSyncRequest(BaseDoRequest):
|
|||||||
_client_class = httpx.Client
|
_client_class = httpx.Client
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
self._client = self._make_client()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
pass
|
self._client.close()
|
||||||
|
del self._client
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def client(self):
|
def client(self):
|
||||||
try:
|
try:
|
||||||
return self._client
|
return self._client
|
||||||
except AttributeError:
|
except AttributeError as e:
|
||||||
self._client = self._make_client()
|
raise ValueError('To access sync client, use "with do_request() as client"') from e
|
||||||
return self._client
|
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
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):
|
def stream(self, *args, **kwargs):
|
||||||
with self.client.stream(*args, **kwargs) as response:
|
with self.client.stream(*args, **kwargs) as response:
|
||||||
|
|||||||
Reference in New Issue
Block a user