@@ -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 ]
0 commit comments