Skip to content

Commit 22f8be2

Browse files
Fix origin IP lookup
1 parent ecd4b3e commit 22f8be2

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

ipregistry/core.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16-
1716
from .cache import IpregistryCache, NoCache
17+
from .json import IpInfo, RequesterIpInfo
1818
from .model import LookupError, ApiResponse, ApiResponseCredits, ApiResponseThrottling
1919
from .request import DefaultRequestHandler, IpregistryRequestHandler
2020

@@ -72,7 +72,7 @@ def batch_lookup_ips(self, ips, **options):
7272
return response
7373

7474
def lookup_ip(self, ip='', **options):
75-
if isinstance(ip, str) and len(ip) > 0:
75+
if isinstance(ip, str):
7676
return self.__lookup_ip(ip, options)
7777
else:
7878
raise ValueError("Invalid value for 'ip' parameter: " + ip)
@@ -86,7 +86,8 @@ def __lookup_ip(self, ip, options):
8686

8787
if cache_value is None:
8888
response = self._requestHandler.lookup_ip(ip, options)
89-
self._cache.put(cache_key, response.data)
89+
if isinstance(response.data, IpInfo):
90+
self._cache.put(cache_key, response.data)
9091
return response
9192

9293
return ApiResponse(

ipregistry/request.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import requests
2424

2525
from .__init__ import __version__
26-
from .model import ApiError, ApiResponse, ApiResponseCredits, ApiResponseThrottling, ClientError, IpInfo, LookupError
26+
from .model import (ApiError, ApiResponse, ApiResponseCredits, ApiResponseThrottling, ClientError, IpInfo,
27+
LookupError, RequesterIpInfo)
2728

2829

2930
class IpregistryRequestHandler(ABC):
@@ -88,7 +89,11 @@ def lookup_ip(self, ip, options):
8889
response.raise_for_status()
8990
json_response = response.json()
9091

91-
return self.build_api_response(response, IpInfo(**json_response))
92+
return self.build_api_response(
93+
response,
94+
RequesterIpInfo(**json_response) if ip == ''
95+
else IpInfo(**json_response)
96+
)
9297
except requests.HTTPError:
9398
self.__create_api_error(response)
9499
except Exception as err:
@@ -112,7 +117,6 @@ def build_api_response(response, data):
112117
ipregistry_credits_remaining,
113118
),
114119
data,
115-
None if throttling_limit is None and throttling_remaining is None and throttling_reset is None else
116120
ApiResponseThrottling(
117121
throttling_limit,
118122
throttling_remaining,

tests/test_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,18 @@ def test_client_cache_inmemory_batch_ips_lookup(self):
6868
batch_ips_response2 = client.batch_lookup_ips(['1.1.1.1', '1.1.1.3'])
6969
self.assertEqual(0, batch_ips_response2.credits.consumed)
7070

71+
def test_origin_lookup_ip(self):
72+
"""
73+
Test that a simple origin IP lookup returns data
74+
"""
75+
client = IpregistryClient(os.getenv('IPREGISTRY_API_KEY'))
76+
response = client.lookup_ip()
77+
self.assertIsNotNone(response.data.ip)
78+
self.assertIsNotNone(response.data.user_agent)
79+
7180
def test_lookup_ip(self):
7281
"""
73-
Test that a simple lookup returns data
82+
Test that a simple IP lookup returns data
7483
"""
7584
client = IpregistryClient(os.getenv('IPREGISTRY_API_KEY'))
7685
response = client.lookup_ip('8.8.8.8')

0 commit comments

Comments
 (0)