77import os
88import sys
99
10+ import aiohttp
1011import requests
1112
1213from .cache .default import DefaultCache
@@ -41,6 +42,9 @@ def __init__(self, access_token=None, **kwargs):
4142 if "timeout" not in self .request_options :
4243 self .request_options ["timeout" ] = self .REQUEST_TIMEOUT_DEFAULT
4344
45+ # setup aiohttp
46+ self .httpsess = None
47+
4448 # setup cache
4549 if "cache" in kwargs :
4650 self .cache = kwargs ["cache" ]
@@ -52,8 +56,34 @@ def __init__(self, access_token=None, **kwargs):
5256 cache_options ["ttl" ] = self .CACHE_TTL
5357 self .cache = DefaultCache (** cache_options )
5458
59+ async def init (self ):
60+ """
61+ Initializes internal aiohttp connection pool.
62+
63+ This isn't _required_, as the pool is initialized lazily when needed.
64+ But in case you require non-lazy initialization, you may await this.
65+
66+ This is idempotent.
67+ """
68+ await self ._ensure_aiohttp_ready ()
69+
70+ async def deinit (self ):
71+ """
72+ Deinitialize the async handler.
73+
74+ This is required in case you need to let go of the memory/state
75+ associated with the async handler in a long-running process.
76+
77+ This is idempotent.
78+ """
79+ if self .httpsess :
80+ await self .httpsess .close ()
81+ self .httpsess = None
82+
5583 async def getDetails (self , ip_address = None ):
5684 """Get details for specified IP address as a Details object."""
85+ self ._ensure_aiohttp_ready ()
86+
5787 # If the supplied IP address uses the objects defined in the built-in
5888 # module ipaddress, extract the appropriate string notation before
5989 # formatting the URL.
@@ -82,6 +112,8 @@ async def getDetails(self, ip_address=None):
82112
83113 async def getBatchDetails (self , ip_addresses ):
84114 """Get details for a batch of IP addresses at once."""
115+ self ._ensure_aiohttp_ready ()
116+
85117 result = {}
86118
87119 # Pre-populate with anything we've got in the cache, and keep around
@@ -128,6 +160,11 @@ async def getBatchDetails(self, ip_addresses):
128160
129161 return result
130162
163+ def _ensure_aiohttp_ready (self ):
164+ """Ensures aiohttp internal state is initialized."""
165+ if not self .httpsess :
166+ self .httpsess = aiohttp .ClientSession ()
167+
131168 def _get_headers (self ):
132169 """Built headers for request to IPinfo API."""
133170 headers = {
0 commit comments