22Main API client handler for fetching data from the IPinfo service.
33"""
44
5- from ipaddress import IPv4Address , IPv6Address
65import time
6+ from ipaddress import IPv4Address , IPv6Address
77
88import requests
99
10- from .error import APIError
10+ from . import handler_utils
11+ from .bogon import is_bogon
1112from .cache .default import DefaultCache
13+ from .data import (
14+ continents ,
15+ countries ,
16+ countries_currencies ,
17+ countries_flags ,
18+ eu_countries ,
19+ )
1220from .details import Details
21+ from .error import APIError
1322from .exceptions import RequestQuotaExceededError , TimeoutExceededError
1423from .handler_utils import (
1524 API_URL ,
16- RESPROXY_API_URL ,
1725 BATCH_MAX_SIZE ,
26+ BATCH_REQ_TIMEOUT_DEFAULT ,
1827 CACHE_MAXSIZE ,
1928 CACHE_TTL ,
2029 REQUEST_TIMEOUT_DEFAULT ,
21- BATCH_REQ_TIMEOUT_DEFAULT ,
30+ RESPROXY_API_URL ,
2231 cache_key ,
23- )
24- from . import handler_utils
25- from .bogon import is_bogon
26- from .data import (
27- continents ,
28- countries ,
29- countries_currencies ,
30- eu_countries ,
31- countries_flags ,
32+ is_prefixed_lookup ,
3233)
3334
3435
@@ -91,9 +92,7 @@ def getDetails(self, ip_address=None, timeout=None):
9192 # If the supplied IP address uses the objects defined in the built-in
9293 # module ipaddress extract the appropriate string notation before
9394 # formatting the URL.
94- if isinstance (ip_address , IPv4Address ) or isinstance (
95- ip_address , IPv6Address
96- ):
95+ if isinstance (ip_address , IPv4Address ) or isinstance (ip_address , IPv6Address ):
9796 ip_address = ip_address .exploded
9897
9998 # check if bogon.
@@ -125,11 +124,11 @@ def getDetails(self, ip_address=None, timeout=None):
125124 raise RequestQuotaExceededError ()
126125 if response .status_code >= 400 :
127126 error_code = response .status_code
128- content_type = response .headers .get (' Content-Type' )
129- if content_type == ' application/json' :
127+ content_type = response .headers .get (" Content-Type" )
128+ if content_type == " application/json" :
130129 error_response = response .json ()
131130 else :
132- error_response = {' error' : response .text }
131+ error_response = {" error" : response .text }
133132 raise APIError (error_code , error_response )
134133 details = response .json ()
135134
@@ -196,7 +195,6 @@ def getResproxy(self, ip_address, timeout=None):
196195
197196 return Details (details )
198197
199-
200198 def getBatchDetails (
201199 self ,
202200 ip_addresses ,
@@ -251,7 +249,11 @@ def getBatchDetails(
251249 ):
252250 ip_address = ip_address .exploded
253251
254- if ip_address and is_bogon (ip_address ):
252+ if (
253+ ip_address
254+ and not is_prefixed_lookup (ip_address )
255+ and is_bogon (ip_address )
256+ ):
255257 details = {}
256258 details ["ip" ] = ip_address
257259 details ["bogon" ] = True
@@ -280,10 +282,7 @@ def getBatchDetails(
280282 headers ["content-type" ] = "application/json"
281283 for i in range (0 , len (lookup_addresses ), batch_size ):
282284 # quit if total timeout is reached.
283- if (
284- timeout_total is not None
285- and time .time () - start_time > timeout_total
286- ):
285+ if timeout_total is not None and time .time () - start_time > timeout_total :
287286 return handler_utils .return_or_fail (
288287 raise_on_fail , TimeoutExceededError (), result
289288 )
@@ -292,9 +291,7 @@ def getBatchDetails(
292291
293292 # lookup
294293 try :
295- response = requests .post (
296- url , json = chunk , headers = headers , ** req_opts
297- )
294+ response = requests .post (url , json = chunk , headers = headers , ** req_opts )
298295 except Exception as e :
299296 return handler_utils .return_or_fail (raise_on_fail , e , result )
300297
@@ -347,9 +344,7 @@ def getMap(self, ips):
347344 url = f"{ API_URL } /map?cli=1"
348345 headers = handler_utils .get_headers (None , self .headers )
349346 headers ["content-type" ] = "application/json"
350- response = requests .post (
351- url , json = ip_strs , headers = headers , ** req_opts
352- )
347+ response = requests .post (url , json = ip_strs , headers = headers , ** req_opts )
353348 response .raise_for_status ()
354349 return response .json ()["reportUrl" ]
355350
@@ -370,7 +365,9 @@ def getBatchDetailsIter(
370365 ):
371366 ip_address = ip_address .exploded
372367
373- if ip_address and is_bogon (ip_address ):
368+ if is_prefixed_lookup (ip_address ):
369+ lookup_addresses .append (ip_address )
370+ elif ip_address and is_bogon (ip_address ):
374371 details = {}
375372 details ["ip" ] = ip_address
376373 details ["bogon" ] = True
0 commit comments