Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
This allows more reliable changes to cached data in the future without
causing confusing incompatibilities. This should be transparent to the user.
This is primarily useful for users with persistent cache implementations.
- Add Map integration.
See https://ipinfo.io/map for details.

## 4.1.0

Expand Down
25 changes: 25 additions & 0 deletions ipinfo/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,28 @@ def getBatchDetails(
handler_utils.format_details(detail, self.countries)

return result

def getMap(self, ips):
"""
Gets a URL to a map on https://ipinfo.io/map given a list of IPs (max
500,000).
"""
ip_strs = []
for ip in ips:
# if the supplied IP address uses the objects defined in the
# built-in module ipaddress extract the appropriate string notation
# before formatting the URL.
if isinstance(ip, IPv4Address) or isinstance(ip, IPv6Address):
ip = ip.exploded

ip_strs.append(ip)

req_opts = {**self.request_options}
url = f"{API_URL}/map?cli=1"
headers = handler_utils.get_headers(None)
headers["content-type"] = "application/json"
response = requests.post(
url, json=ip_strs, headers=headers, **req_opts
)
response.raise_for_status()
return response.json()["reportUrl"]
11 changes: 11 additions & 0 deletions tests/handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,14 @@ def test_get_batch_details_total_timeout(batch_size):
handler.getBatchDetails(
ips, batch_size=batch_size, timeout_total=0.001
)


#############
# MAP TESTS
#############


def test_get_map():
handler = Handler()
mapUrl = handler.getMap(open("tests/map-ips.txt").read().splitlines())
print(f"got URL={mapUrl}")
Loading