Skip to content

Commit

Permalink
Add close and aio_close methods
Browse files Browse the repository at this point in the history
These methods provide a more direct way to close the underlying httpx_client if the user decides to initialize an RDAPClient.
  • Loading branch information
pogzyb committed Feb 7, 2022
1 parent 2698d25 commit 906f03f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
32 changes: 16 additions & 16 deletions whodap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def lookup_domain(
finally:
# close the default client created by DNSClient if it exists;
# otherwise it's up to the user to close their `httpx_client`
if not httpx_client and not dns_client.httpx_client.is_closed:
dns_client.httpx_client.close()
if not httpx_client:
dns_client.close()
return resp


Expand All @@ -70,8 +70,8 @@ async def aio_lookup_domain(
finally:
# close the default client created by DNSClient if it exists;
# otherwise it's up to the user to close their `httpx_client`
if not httpx_client and not dns_client.httpx_client.is_closed:
await dns_client.httpx_client.aclose()
if not httpx_client:
await dns_client.aio_close()
return resp


Expand All @@ -94,8 +94,8 @@ def lookup_ipv4(
finally:
# close the default client created by IPv4Client if it exists;
# otherwise it's up to the user to close their `httpx_client`
if not httpx_client and not ipv4_client.httpx_client.is_closed:
ipv4_client.httpx_client.close()
if not httpx_client:
ipv4_client.close()
return resp


Expand All @@ -118,8 +118,8 @@ async def aio_lookup_ipv4(
finally:
# close the default client created by IPv4Client if it exists;
# otherwise it's up to the user to close their `httpx_client`
if not httpx_client and not ipv4_client.httpx_client.is_closed:
await ipv4_client.httpx_client.aclose()
if not httpx_client:
await ipv4_client.aio_close()
return resp


Expand All @@ -142,8 +142,8 @@ def lookup_ipv6(
finally:
# close the default client created by IPv6Client if it exists;
# otherwise it's up to the user to close their `httpx_client`
if not httpx_client and not ipv6_client.httpx_client.is_closed:
ipv6_client.httpx_client.close()
if not httpx_client:
ipv6_client.close()
return resp


Expand All @@ -166,8 +166,8 @@ async def aio_lookup_ipv6(
finally:
# close the default client created by IPv6Client if it exists;
# otherwise it's up to the user to close their `httpx_client`
if not httpx_client and not ipv6_client.httpx_client.is_closed:
await ipv6_client.httpx_client.aclose()
if not httpx_client:
await ipv6_client.aio_close()
return resp


Expand All @@ -190,8 +190,8 @@ def lookup_asn(
finally:
# close the default client created by ASNClient if it exists;
# otherwise it's up to the user to close their `httpx_client`
if not httpx_client and not asn_client.httpx_client.is_closed:
asn_client.httpx_client.close()
if not httpx_client:
asn_client.close()
return resp


Expand All @@ -214,6 +214,6 @@ async def aio_lookup_asn(
finally:
# close the default client created by ASNClient if it exists;
# otherwise it's up to the user to close their `httpx_client`
if not httpx_client and not asn_client.httpx_client.is_closed:
await asn_client.httpx_client.aclose()
if not httpx_client:
await asn_client.aio_close()
return resp
14 changes: 14 additions & 0 deletions whodap/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ def _set_iana_info(self, iana_resp: Dict[str, Any]) -> None:
"""
...

def close(self):
"""
Closes the underlying `httpx.Client`
"""
if not self.httpx_client.is_closed:
self.httpx_client.close()

async def aio_close(self):
"""
Closes the underlying `httpx.AsyncClient`
"""
if not self.httpx_client.is_closed:
await self.httpx_client.aclose()

@classmethod
@contextmanager
def new_client_context(cls, httpx_client: Optional[httpx.Client] = None):
Expand Down

0 comments on commit 906f03f

Please sign in to comment.