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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# IPInfo Changelog

## 2.0.0

#### Breaking Changes

- Fix [Issue #8](https://github.com/ipinfo/python/issues/8).
Deleted the `ip_address` key in the details object which was of type [`IPv4Address`](https://docs.python.org/3/library/ipaddress.html).

This allows serializing the details object (into JSON or something else) without errors by default.

Users who expected that object type can simply pull the `ip` key instead and turn it into an [`IPv4Address`](https://docs.python.org/3/library/ipaddress.html)
object on their own.
16 changes: 0 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ US
United States
```

### IP Address

`details.ip_address` will return the an `ipaddress` object from the [Python Standard Library](https://docs.python.org/3/library/ipaddress.html). `details.ip` will still return a string.

```python
>>> details.ip
104.175.221.247
>>> type(details.ip)
<class 'str'>
>>> details.ip_address
104.175.221.247
>>> type(details.ip_address)
<class 'ipaddress.IPv4Address'>
```

#### Longitude and Latitude

`details.latitude` and `details.longitude` will return latitude and longitude, respectively, as strings. `details.loc` will still return a composite string of both values.
Expand Down Expand Up @@ -125,7 +110,6 @@ United States
'country_name': 'United States',
'hostname': 'cpe-104-175-221-247.socal.res.rr.com',
'ip': '104.175.221.247',
'ip_address': IPv4Address('104.175.221.247'),
'loc': '34.0293,-118.3570',
'latitude': '34.0293',
'longitude': '-118.3570',
Expand Down
5 changes: 1 addition & 4 deletions ipinfo/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Main API client handler for fetching data from the IPinfo service.
"""

import ipaddress
import json
import os
import sys
Expand Down Expand Up @@ -30,7 +29,6 @@ def __init__(self, access_token=None, **kwargs):
"""Initialize the Handler object with country name list and the cache initialized."""
self.access_token = access_token
self.countries = self._read_country_names(kwargs.get("countries_file"))

self.request_options = kwargs.get("request_options", {})
if "timeout" not in self.request_options:
self.request_options["timeout"] = self.REQUEST_TIMEOUT_DEFAULT
Expand All @@ -47,7 +45,6 @@ def getDetails(self, ip_address=None):
"""Get details for specified IP address as a Details object."""
raw_details = self._requestDetails(ip_address)
raw_details["country_name"] = self.countries.get(raw_details.get("country"))
raw_details["ip_address"] = ipaddress.ip_address(raw_details.get("ip"))
raw_details["latitude"], raw_details["longitude"] = self._read_coords(
raw_details.get("loc")
)
Expand All @@ -73,7 +70,7 @@ def _requestDetails(self, ip_address=None):
def _get_headers(self):
"""Built headers for request to IPinfo API."""
headers = {
"user-agent": "IPinfoClient/Python{version}/1.0".format(
"user-agent": "IPinfoClient/Python{version}/2.0.0".format(
version=sys.version_info[0]
),
"accept": "application/json",
Expand Down
12 changes: 7 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --no-index --output-file=requirements.txt requirements.in
# pip-compile --no-index
#
appdirs==1.4.3 # via black
atomicwrites==1.3.0 # via pytest
Expand All @@ -13,13 +13,15 @@ certifi==2019.3.9 # via requests
chardet==3.0.4 # via requests
click==7.0 # via black, pip-tools
idna==2.8 # via requests
more-itertools==6.0.0 # via pytest
importlib-metadata==0.18 # via pluggy
more-itertools==7.0.0 # via pytest
pip-tools==3.7.0
pluggy==0.9.0 # via pytest
pluggy==0.12.0 # via pytest
py==1.8.0 # via pytest
pytest==4.5.0
requests==2.21.0
requests==2.22.0
six==1.12.0 # via pip-tools, pytest
toml==0.10.0 # via black
urllib3==1.24.1 # via requests
urllib3==1.25.3 # via requests
wcwidth==0.1.7 # via pytest
zipp==0.5.1 # via importlib-metadata
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

setup(
name="ipinfo",
version="1.1.2",
version="2.0.0",
description="Official Python library for IPInfo",
long_description=long_description,
url="https://github.com/ipinfo/python",
author="James Timmins",
author_email="jameshtimmins@gmail.com",
author="IPinfo",
author_email="support@ipinfo.io",
license="Apache License 2.0",
packages=["ipinfo", "ipinfo.cache"],
install_requires=["requests", "cachetools", "six"],
Expand Down
8 changes: 4 additions & 4 deletions tests/default_cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ def _get_new_cache():

def test_contains():
cache = _get_new_cache()
cache['foo'] = 'bar'
cache["foo"] = "bar"

assert 'foo' in cache
assert "foo" in cache


def test_get():
cache = _get_new_cache()
cache['foo'] = 'bar'
cache["foo"] = "bar"

assert cache['foo'] == 'bar'
assert cache["foo"] == "bar"
10 changes: 5 additions & 5 deletions tests/details_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@


def test_init():
data = {'foo': 'bar'}
data = {"foo": "bar"}
details = Details(data)
assert details.details == data


def test_getattr_success():
data = {'foo': 'bar'}
data = {"foo": "bar"}
details = Details(data)
assert details.foo == data['foo']
assert details.foo == data["foo"]


def test_getattr_fail():
data = {'foo': 'bar'}
data = {"foo": "bar"}
details = Details(data)
with pytest.raises(Exception):
details.blah


def test_all():
data = {'foo': 'bar', 'ham': 'eggs'}
data = {"foo": "bar", "ham": "eggs"}
details = Details(data)
assert details.all == data
66 changes: 48 additions & 18 deletions tests/handler_test.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,74 @@
import ipaddress
import json

from ipinfo.cache.default import DefaultCache
from ipinfo.details import Details
from ipinfo.handler import Handler


def test_init():
token = 'mytesttoken'
token = "mytesttoken"
handler = Handler(token)
assert handler.access_token == token
assert isinstance(handler.cache, DefaultCache)
assert 'US' in handler.countries
assert "US" in handler.countries


def test_headers():
token = 'mytesttoken'
token = "mytesttoken"
handler = Handler(token)
headers = handler._get_headers()

assert 'user-agent' in headers
assert 'accept' in headers
assert 'authorization' in headers
assert "user-agent" in headers
assert "accept" in headers
assert "authorization" in headers


def test_get_details():
handler = Handler()
fake_details = {"country": "US", "ip": "127.0.0.1", "loc": "12.34,56.78"}

handler._requestDetails = lambda x: fake_details

details = handler.getDetails(fake_details["ip"])
assert isinstance(details, Details)
assert details.country == fake_details["country"]
assert details.country_name == "United States"
assert details.ip == fake_details["ip"]
assert details.loc == fake_details["loc"]
assert details.longitude == "56.78"
assert details.latitude == "12.34"


def test_json_serialization():
handler = Handler()
fake_details = {
'country': 'US',
'ip': '127.0.0.1',
'loc': '12.34,56.78'
"asn": {
"asn": "AS20001",
"domain": "twcable.com",
"name": "Time Warner Cable Internet LLC",
"route": "104.172.0.0/14",
"type": "isp",
},
"city": "Los Angeles",
"company": {
"domain": "twcable.com",
"name": "Time Warner Cable Internet LLC",
"type": "isp",
},
"country": "US",
"country_name": "United States",
"hostname": "cpe-104-175-221-247.socal.res.rr.com",
"ip": "104.175.221.247",
"loc": "34.0293,-118.3570",
"latitude": "34.0293",
"longitude": "-118.3570",
"phone": "323",
"postal": "90016",
"region": "California",
}

handler._requestDetails = lambda x: fake_details

details = handler.getDetails(fake_details['ip'])
details = handler.getDetails(fake_details["ip"])
assert isinstance(details, Details)
assert details.country == fake_details['country']
assert details.country_name == 'United States'
assert details.ip == fake_details['ip']
assert isinstance(details.ip_address, ipaddress.IPv4Address)
assert details.loc == fake_details['loc']
assert details.longitude == '56.78'
assert details.latitude == '12.34'
assert json.dumps(details.all)
2 changes: 1 addition & 1 deletion tests/init_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

def test_get_handler():
handler = ipinfo.getHandler()
assert(isinstance(handler, Handler))
assert isinstance(handler, Handler)