Skip to content

Commit

Permalink
Merge pull request #50 from ipinfo/uman/maps
Browse files Browse the repository at this point in the history
Add map integration.
  • Loading branch information
UmanShahzad committed Apr 22, 2021
2 parents ed864d3 + 4445263 commit d1bf8bf
Show file tree
Hide file tree
Showing 4 changed files with 1,038 additions and 0 deletions.
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

0 comments on commit d1bf8bf

Please sign in to comment.