Skip to content

Commit ee5cc2a

Browse files
committed
updating suggestions
1 parent 9b935de commit ee5cc2a

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

ipinfo/handler.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -312,73 +312,58 @@ def getIterativeBatchDetails(
312312
batch_size=None,
313313
raise_on_fail=True,
314314
):
315-
if batch_size == None:
315+
if batch_size is None:
316316
batch_size = BATCH_MAX_SIZE
317317

318318
results = {}
319319

320-
# pre-populate with anything we've got in the cache, and keep around
321-
# the IPs not in the cache.
322320
lookup_addresses = []
323321
for ip_address in ip_addresses:
324-
# if the supplied IP address uses the objects defined in the
325-
# built-in module ipaddress extract the appropriate string notation
326-
# before formatting the URL.
327322
if isinstance(ip_address, IPv4Address) or isinstance(
328323
ip_address, IPv6Address
329324
):
330325
ip_address = ip_address.exploded
331326

332-
# check if bogon.
333327
if ip_address and is_bogon(ip_address):
334328
details = {}
335329
details["ip"] = ip_address
336330
details["bogon"] = True
337331
yield Details(details)
338332

339-
# check cache first.
340333
try:
341334
cached_ipaddr = self.cache[cache_key(ip_address)]
342335
results[ip_address] = cached_ipaddr
343336
except KeyError:
344337
lookup_addresses.append(ip_address)
345338

346-
# all in cache - return early.
347339
if len(lookup_addresses) == 0:
348-
for ip_address, details in results.items():
349-
yield ip_address, details
340+
yield from results.items()
350341

351-
# loop over batch chunks and do lookup for each.
352342
url = API_URL + "/batch"
353343
headers = handler_utils.get_headers(self.access_token, self.headers)
354344
headers["content-type"] = "application/json"
355345
for i in range(0, len(lookup_addresses), batch_size):
356346
batch = lookup_addresses[i : i + batch_size]
357347

358-
# lookup.
359348
try:
360349
response = requests.post(url, json=batch, headers=headers)
361350
except Exception as e:
362-
return handler_utils.return_or_fail(raise_on_fail, e, result)
351+
return handler_utils.return_or_fail(raise_on_fail, e, results)
363352

364-
# fail on bad status codes
365353
try:
366354
if response.status_code == 429:
367355
raise RequestQuotaExceededError()
368356
response.raise_for_status()
369357
except Exception as e:
370-
return handler_utils.return_or_fail(raise_on_fail, e, result)
358+
return handler_utils.return_or_fail(raise_on_fail, e, results)
371359

372-
# fill cache
373360
json_response = response.json()
374361
print(f"JSON: {json_response}")
375362
for ip_address, details in json_response.items():
376363
self.cache[cache_key(ip_address)] = details
377364

378-
# merge cached results with new lookup
379365
results.update(json_response)
380366

381-
# format all
382367
for detail in results.values():
383368
if isinstance(detail, dict):
384369
handler_utils.format_details(
@@ -390,5 +375,4 @@ def getIterativeBatchDetails(
390375
self.continents,
391376
)
392377

393-
for ip_address, details in results.items():
394-
yield ip_address, details
378+
yield from results.items()

0 commit comments

Comments
 (0)