Skip to content

Commit 433e739

Browse files
committed
do aiohttp batch req + tests
1 parent 904bee0 commit 433e739

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

ipinfo/handler_async.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import sys
99

1010
import aiohttp
11-
import requests
1211

1312
from .cache.default import DefaultCache
1413
from .details import Details
@@ -139,26 +138,26 @@ async def getBatchDetails(self, ip_addresses):
139138
if len(lookup_addresses) == 0:
140139
return result
141140

142-
# Do the lookup
141+
# do http req
143142
url = self.API_URL + "/batch"
144143
headers = self._get_headers()
145144
headers["content-type"] = "application/json"
146-
response = requests.post(
147-
url, json=lookup_addresses, headers=headers, **self.request_options
148-
)
149-
if response.status_code == 429:
150-
raise RequestQuotaExceededError()
151-
response.raise_for_status()
145+
async with self.httpsess.post(
146+
url, data=json.dumps(lookup_addresses), headers=headers
147+
) as resp:
148+
if resp.status == 429:
149+
raise RequestQuotaExceededError()
150+
resp.raise_for_status()
151+
json_resp = await resp.json()
152152

153-
# Format & fill up cache
154-
json_response = response.json()
155-
for ip_address, details in json_response.items():
153+
# format & fill up cache
154+
for ip_address, details in json_resp.items():
156155
if isinstance(details, dict):
157156
self._format_details(details)
158157
self.cache[ip_address] = details
159158

160-
# Merge cached results with new lookup
161-
result.update(json_response)
159+
# merge cached results with new lookup
160+
result.update(json_resp)
162161

163162
return result
164163

tests/handler_async_test.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ async def test_headers():
2828
assert "authorization" in headers
2929

3030

31+
@pytest.mark.parametrize('n', range(5))
3132
@pytest.mark.asyncio
32-
async def test_get_details():
33+
async def test_get_details(n):
3334
token = os.environ.get("IPINFO_TOKEN", "")
3435
handler = AsyncHandler(token)
3536
details = await handler.getDetails("8.8.8.8")
@@ -81,3 +82,24 @@ async def test_get_details():
8182
assert len(domains["domains"]) == 5
8283

8384
await handler.deinit()
85+
86+
@pytest.mark.parametrize('n', range(5))
87+
@pytest.mark.asyncio
88+
async def test_get_batch_details(n):
89+
token = os.environ.get("IPINFO_TOKEN", "")
90+
handler = AsyncHandler(token)
91+
ips = ["1.1.1.1", "8.8.8.8", "9.9.9.9"]
92+
details = await handler.getBatchDetails(ips)
93+
94+
for ip in ips:
95+
assert ip in details
96+
d = details[ip]
97+
assert d["ip"] == ip
98+
if token:
99+
assert "asn" in d
100+
assert "company" in d
101+
assert "privacy" in d
102+
assert "abuse" in d
103+
assert "domains" in d
104+
105+
await handler.deinit()

tests/handler_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ipinfo.cache.default import DefaultCache
66
from ipinfo.details import Details
77
from ipinfo.handler import Handler
8+
import pytest
89

910

1011
def test_init():
@@ -25,7 +26,8 @@ def test_headers():
2526
assert "authorization" in headers
2627

2728

28-
def test_get_details():
29+
@pytest.mark.parametrize('n', range(5))
30+
def test_get_details(n):
2931
token = os.environ.get("IPINFO_TOKEN", "")
3032
handler = Handler(token)
3133
details = handler.getDetails("8.8.8.8")

0 commit comments

Comments
 (0)