Skip to content

Commit 5d363ea

Browse files
committed
address #33
also get rid of useless tests that only mock stuff.
1 parent 7cfafa9 commit 5d363ea

File tree

3 files changed

+31
-86
lines changed

3 files changed

+31
-86
lines changed

ipinfo/handler.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,33 @@ def __init__(self, access_token=None, **kwargs):
5555

5656
def getDetails(self, ip_address=None):
5757
"""Get details for specified IP address as a Details object."""
58-
raw_details = self._requestDetails(ip_address)
59-
handler_utils.format_details(raw_details, self.countries)
60-
return Details(raw_details)
58+
# If the supplied IP address uses the objects defined in the built-in
59+
# module ipaddress extract the appropriate string notation before
60+
# formatting the URL.
61+
if isinstance(ip_address, IPv4Address) or isinstance(
62+
ip_address, IPv6Address
63+
):
64+
ip_address = ip_address.exploded
65+
66+
if ip_address in self.cache:
67+
return Details(self.cache[ip_address])
68+
69+
# not in cache; do http req
70+
url = handler_utils.API_URL
71+
if ip_address:
72+
url += "/" + ip_address
73+
headers = handler_utils.get_headers(self.access_token)
74+
response = requests.get(url, headers=headers, **self.request_options)
75+
if response.status_code == 429:
76+
raise RequestQuotaExceededError()
77+
response.raise_for_status()
78+
details = response.json()
79+
80+
# format & cache
81+
handler_utils.format_details(details, self.countries)
82+
self.cache[ip_address] = details
83+
84+
return Details(details)
6185

6286
def getBatchDetails(self, ip_addresses):
6387
"""Get details for a batch of IP addresses at once."""
@@ -105,31 +129,3 @@ def getBatchDetails(self, ip_addresses):
105129
handler_utils.format_details(detail, self.countries)
106130

107131
return result
108-
109-
def _requestDetails(self, ip_address=None):
110-
"""Get IP address data by sending request to IPinfo API."""
111-
112-
# If the supplied IP address uses the objects defined in the built-in
113-
# module ipaddress extract the appropriate string notation before
114-
# formatting the URL.
115-
if isinstance(ip_address, IPv4Address) or isinstance(
116-
ip_address, IPv6Address
117-
):
118-
ip_address = ip_address.exploded
119-
120-
if ip_address not in self.cache:
121-
url = handler_utils.API_URL
122-
if ip_address:
123-
url += "/" + ip_address
124-
125-
response = requests.get(
126-
url,
127-
headers=handler_utils.get_headers(self.access_token),
128-
**self.request_options
129-
)
130-
if response.status_code == 429:
131-
raise RequestQuotaExceededError()
132-
response.raise_for_status()
133-
self.cache[ip_address] = response.json()
134-
135-
return self.cache[ip_address]

ipinfo/handler_async.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ async def getDetails(self, ip_address=None):
104104
if resp.status == 429:
105105
raise RequestQuotaExceededError()
106106
resp.raise_for_status()
107-
raw_details = await resp.json()
107+
details = await resp.json()
108108

109109
# format & cache
110-
handler_utils.format_details(raw_details, self.countries)
111-
self.cache[ip_address] = raw_details
110+
handler_utils.format_details(details, self.countries)
111+
self.cache[ip_address] = details
112112

113-
return Details(raw_details)
113+
return Details(details)
114114

115115
async def getBatchDetails(self, ip_addresses):
116116
"""Get details for a batch of IP addresses at once."""

tests/handler_test.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -102,54 +102,3 @@ def test_get_batch_details(n):
102102
assert "privacy" in d
103103
assert "abuse" in d
104104
assert "domains" in d
105-
106-
107-
def test_builtin_ip_types():
108-
handler = Handler()
109-
fake_details = {"country": "US", "ip": "127.0.0.1", "loc": "12.34,56.78"}
110-
111-
handler._requestDetails = lambda x: fake_details
112-
113-
details = handler.getDetails(IPv4Address(fake_details["ip"]))
114-
assert isinstance(details, Details)
115-
assert details.country == fake_details["country"]
116-
assert details.country_name == "United States"
117-
assert details.ip == fake_details["ip"]
118-
assert details.loc == fake_details["loc"]
119-
assert details.longitude == "56.78"
120-
assert details.latitude == "12.34"
121-
122-
123-
def test_json_serialization():
124-
handler = Handler()
125-
fake_details = {
126-
"asn": {
127-
"asn": "AS20001",
128-
"domain": "twcable.com",
129-
"name": "Time Warner Cable Internet LLC",
130-
"route": "104.172.0.0/14",
131-
"type": "isp",
132-
},
133-
"city": "Los Angeles",
134-
"company": {
135-
"domain": "twcable.com",
136-
"name": "Time Warner Cable Internet LLC",
137-
"type": "isp",
138-
},
139-
"country": "US",
140-
"country_name": "United States",
141-
"hostname": "cpe-104-175-221-247.socal.res.rr.com",
142-
"ip": "104.175.221.247",
143-
"loc": "34.0293,-118.3570",
144-
"latitude": "34.0293",
145-
"longitude": "-118.3570",
146-
"phone": "323",
147-
"postal": "90016",
148-
"region": "California",
149-
}
150-
151-
handler._requestDetails = lambda x: fake_details
152-
153-
details = handler.getDetails(fake_details["ip"])
154-
assert isinstance(details, Details)
155-
assert json.dumps(details.all)

0 commit comments

Comments
 (0)