Skip to content

Commit 4445263

Browse files
committed
add map integration.
1 parent ed864d3 commit 4445263

File tree

4 files changed

+1038
-0
lines changed

4 files changed

+1038
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
This allows more reliable changes to cached data in the future without
77
causing confusing incompatibilities. This should be transparent to the user.
88
This is primarily useful for users with persistent cache implementations.
9+
- Add Map integration.
10+
See https://ipinfo.io/map for details.
911

1012
## 4.1.0
1113

ipinfo/handler.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,28 @@ def getBatchDetails(
218218
handler_utils.format_details(detail, self.countries)
219219

220220
return result
221+
222+
def getMap(self, ips):
223+
"""
224+
Gets a URL to a map on https://ipinfo.io/map given a list of IPs (max
225+
500,000).
226+
"""
227+
ip_strs = []
228+
for ip in ips:
229+
# if the supplied IP address uses the objects defined in the
230+
# built-in module ipaddress extract the appropriate string notation
231+
# before formatting the URL.
232+
if isinstance(ip, IPv4Address) or isinstance(ip, IPv6Address):
233+
ip = ip.exploded
234+
235+
ip_strs.append(ip)
236+
237+
req_opts = {**self.request_options}
238+
url = f"{API_URL}/map?cli=1"
239+
headers = handler_utils.get_headers(None)
240+
headers["content-type"] = "application/json"
241+
response = requests.post(
242+
url, json=ip_strs, headers=headers, **req_opts
243+
)
244+
response.raise_for_status()
245+
return response.json()["reportUrl"]

tests/handler_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,14 @@ def test_get_batch_details_total_timeout(batch_size):
127127
handler.getBatchDetails(
128128
ips, batch_size=batch_size, timeout_total=0.001
129129
)
130+
131+
132+
#############
133+
# MAP TESTS
134+
#############
135+
136+
137+
def test_get_map():
138+
handler = Handler()
139+
mapUrl = handler.getMap(open("tests/map-ips.txt").read().splitlines())
140+
print(f"got URL={mapUrl}")

0 commit comments

Comments
 (0)