Skip to content

Commit 18409c0

Browse files
committed
updating handler function
1 parent ca0dc37 commit 18409c0

File tree

3 files changed

+33
-44
lines changed

3 files changed

+33
-44
lines changed

ipinfo/handler.py

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,6 @@ def getIterativeBatchDetails(
315315
if batch_size is None:
316316
batch_size = BATCH_MAX_SIZE
317317

318-
results = {}
319-
320318
lookup_addresses = []
321319
for ip_address in ip_addresses:
322320
if isinstance(ip_address, IPv4Address) or isinstance(
@@ -329,16 +327,9 @@ def getIterativeBatchDetails(
329327
details["ip"] = ip_address
330328
details["bogon"] = True
331329
yield Details(details)
332-
333-
try:
334-
cached_ipaddr = self.cache[cache_key(ip_address)]
335-
results[ip_address] = cached_ipaddr
336-
except KeyError:
330+
else:
337331
lookup_addresses.append(ip_address)
338332

339-
if len(lookup_addresses) == 0:
340-
yield from results.items()
341-
342333
url = API_URL + "/batch"
343334
headers = handler_utils.get_headers(self.access_token, self.headers)
344335
headers["content-type"] = "application/json"
@@ -348,30 +339,28 @@ def getIterativeBatchDetails(
348339
try:
349340
response = requests.post(url, json=batch, headers=headers)
350341
except Exception as e:
351-
return handler_utils.return_or_fail(raise_on_fail, e, results)
342+
return handler_utils.return_or_fail(raise_on_fail, e)
352343

353344
try:
354345
if response.status_code == 429:
355346
raise RequestQuotaExceededError()
356347
response.raise_for_status()
357348
except Exception as e:
358-
return handler_utils.return_or_fail(raise_on_fail, e, results)
359-
360-
json_response = response.json()
361-
for ip_address, details in json_response.items():
362-
self.cache[cache_key(ip_address)] = details
363-
364-
results.update(json_response)
365-
366-
for detail in results.values():
367-
if isinstance(detail, dict):
368-
handler_utils.format_details(
369-
detail,
370-
self.countries,
371-
self.eu_countries,
372-
self.countries_flags,
373-
self.countries_currencies,
374-
self.continents,
375-
)
376-
377-
yield from results.items()
349+
return handler_utils.return_or_fail(raise_on_fail, e)
350+
351+
details = response.json()
352+
353+
# format & cache
354+
handler_utils.format_details(
355+
details,
356+
self.countries,
357+
self.eu_countries,
358+
self.countries_flags,
359+
self.countries_currencies,
360+
self.continents,
361+
)
362+
for ip in batch:
363+
detail = details.get(ip)
364+
if detail is not None:
365+
self.cache[cache_key(ip)] = detail
366+
yield detail

ipinfo/handler_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def format_details(
8080
details["country_name"] = countries.get(details.get("country"))
8181
details["isEU"] = details.get("country") in eu_countries
8282
details["country_flag_url"] = (
83-
COUNTRY_FLAGS_URL + details.get("country") + ".svg"
83+
COUNTRY_FLAGS_URL + (details.get("country") or "") + ".svg"
8484
)
8585
details["country_flag"] = copy.deepcopy(
8686
countries_flags.get(details.get("country"))

tests/handler_test.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def test_get_details():
108108

109109
def _prepare_batch_test():
110110
"""Helper for preparing batch test cases."""
111-
token = os.environ.get("IPINFO_TOKEN", "")
111+
token = os.environ.get("IPINFO_TOKEN", "")
112112
if not token:
113113
pytest.skip("token required for batch tests")
114114
handler = Handler(token)
@@ -131,17 +131,17 @@ def _check_batch_details(ips, details, token):
131131
assert "domains" in d
132132

133133

134-
def _check_iterative_batch_details(ip, details, token):
134+
def _check_iterative_batch_details(details, token):
135135
"""Helper for iterative batch tests."""
136-
assert ip == details.get("ip")
137-
assert "country" in details
138-
assert "city" in details
136+
assert "ip" in details, "Key 'ip' not found in details"
137+
assert "country" in details, "Key 'country' not found in details"
138+
assert "city" in details, "Key 'city' not found in details"
139139
if token:
140-
assert "asn" in details
141-
assert "company" in details
142-
assert "privacy" in details
143-
assert "abuse" in details
144-
assert "domains" in details
140+
assert "asn" in details, "Key 'asn' not found in details"
141+
assert "company" in details, "Key 'company' not found in details"
142+
assert "privacy" in details, "Key 'privacy' not found in details"
143+
assert "abuse" in details, "Key 'abuse' not found in details"
144+
assert "domains" in details, "Key 'domains' not found in details"
145145

146146

147147
@pytest.mark.parametrize("batch_size", [None, 1, 2, 3])
@@ -166,8 +166,8 @@ def test_get_iterative_batch_details(batch_size):
166166
details_iterator = handler.getIterativeBatchDetails(
167167
ips, batch_size=batch_size
168168
)
169-
for ip, details in details_iterator:
170-
_check_iterative_batch_details(ip, details, token)
169+
for details in details_iterator:
170+
_check_iterative_batch_details(details, token)
171171

172172

173173
#############

0 commit comments

Comments
 (0)