Skip to content

Commit 056324b

Browse files
committed
adding iterative function
1 parent 45b3378 commit 056324b

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

ipinfo/handler.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,24 @@ def getMap(self, ips):
305305
)
306306
response.raise_for_status()
307307
return response.json()["reportUrl"]
308+
309+
def getIterativeBatchDetails(self, ip_addresses, batch_size=None):
310+
if batch_size == None:
311+
batch_size = BATCH_MAX_SIZE
312+
313+
url = API_URL + "/batch"
314+
headers = handler_utils.get_headers(self.access_token, self.headers)
315+
headers["content-type"] = "application/json"
316+
317+
# Split the IP addresses into batches
318+
batches = [
319+
ip_addresses[i : i + batch_size]
320+
for i in range(0, len(ip_addresses), batch_size)
321+
]
322+
323+
for batch in batches:
324+
response = requests.post(url, json=batch, headers=headers)
325+
json_response = response.json()
326+
327+
for ip_address, details in json_response.items():
328+
yield ip_address, details

ipinfo/handler_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ def __init__(self, access_token=None, **kwargs):
9898
if "ttl" not in cache_options:
9999
cache_options["ttl"] = CACHE_TTL
100100
self.cache = DefaultCache(**cache_options)
101-
101+
102102
# setup custom headers
103103
self.headers = kwargs.get("headers", None)
104-
104+
105105
async def init(self):
106106
"""
107107
Initializes internal aiohttp connection pool.

ipinfo/handler_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151
def get_headers(access_token, custom_headers):
5252
"""Build headers for request to IPinfo API."""
5353
headers = {
54-
"user-agent": "IPinfoClient/Python{version}/{sdk_version}".format(
55-
version=sys.version_info[0], sdk_version=SDK_VERSION
54+
"user-agent": "IPinfoClient/Python{version}/{sdk_version}".format(
55+
version=sys.version_info[0], sdk_version=SDK_VERSION
5656
),
5757
"accept": "application/json",
5858
}
5959

6060
if custom_headers:
61-
headers = {** headers, ** custom_headers}
61+
headers = {**headers, **custom_headers}
6262

6363
if access_token:
6464
headers["authorization"] = "Bearer {}".format(access_token)

tests/handler_async_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,4 @@ async def test_bogon_details():
162162
token = os.environ.get("IPINFO_TOKEN", "")
163163
handler = AsyncHandler(token)
164164
details = await handler.getDetails("127.0.0.1")
165-
assert details.all == {'bogon': True, 'ip': '127.0.0.1'}
165+
assert details.all == {"bogon": True, "ip": "127.0.0.1"}

tests/handler_test.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@ def _check_batch_details(ips, details, token):
128128
assert "domains" in d
129129

130130

131+
def _check_iterative_batch_details(ip, details, token):
132+
"""Helper for iterative batch tests."""
133+
assert ip == details.get("ip")
134+
assert "country" in details
135+
assert "city" in details
136+
if token:
137+
assert "asn" in details
138+
assert "company" in details
139+
assert "privacy" in details
140+
assert "abuse" in details
141+
assert "domains" in details
142+
143+
131144
@pytest.mark.parametrize("batch_size", [None, 1, 2, 3])
132145
def test_get_batch_details(batch_size):
133146
handler, token, ips = _prepare_batch_test()
@@ -144,6 +157,16 @@ def test_get_batch_details_total_timeout(batch_size):
144157
)
145158

146159

160+
@pytest.mark.parametrize("batch_size", [None, 1, 2, 3])
161+
def test_get_iterative_batch_details(batch_size):
162+
handler, token, ips = _prepare_batch_test()
163+
details_iterator = handler.getIterativeBatchDetails(
164+
ips, batch_size=batch_size
165+
)
166+
for ip, details in details_iterator:
167+
_check_iterative_batch_details(ip, details, token)
168+
169+
147170
#############
148171
# MAP TESTS
149172
#############
@@ -165,4 +188,4 @@ def test_bogon_details():
165188
handler = Handler(token)
166189
details = handler.getDetails("127.0.0.1")
167190
assert isinstance(details, Details)
168-
assert details.all == {'bogon': True, 'ip': '127.0.0.1'}
191+
assert details.all == {"bogon": True, "ip": "127.0.0.1"}

0 commit comments

Comments
 (0)