Skip to content

Commit c3570c5

Browse files
authored
Merge pull request #13 from ipinfo/uman/fix-deps
Fix missing `six` dependency
2 parents 62fef91 + d2b611f commit c3570c5

File tree

9 files changed

+72
-55
lines changed

9 files changed

+72
-55
lines changed

ipinfo/cache/default.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import cachetools
6+
67
from .interface import CacheInterface
78

89

ipinfo/details.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Details returned by the IPinfo service.
33
"""
44

5+
56
class Details:
67
"""Encapsulates data for single IP address."""
78

@@ -14,7 +15,7 @@ def __getattr__(self, attr):
1415
if attr in self.details:
1516
return self.details[attr]
1617
else:
17-
raise AttributeError('{} is not a valid attribute of Details'.format(attr))
18+
raise AttributeError("{} is not a valid attribute of Details".format(attr))
1819

1920
@property
2021
def all(self):

ipinfo/exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Exceptions thrown by the IPinfo service.
33
"""
44

5+
56
class RequestQuotaExceededError(Exception):
67
"""Error indicating that users monthly request quota has been passed."""
8+
79
pass

ipinfo/handler.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import ipaddress
66
import json
77
import os
8-
import requests
98
import sys
9+
10+
import requests
11+
1012
from .cache.default import DefaultCache
1113
from .details import Details
1214
from .exceptions import RequestQuotaExceededError
@@ -18,45 +20,49 @@ class Handler:
1820
and maintains access to cache.
1921
"""
2022

21-
API_URL = 'https://ipinfo.io'
23+
API_URL = "https://ipinfo.io"
2224
CACHE_MAXSIZE = 4096
2325
CACHE_TTL = 60 * 60 * 24
24-
COUNTRY_FILE_DEFAULT = 'countries.json'
26+
COUNTRY_FILE_DEFAULT = "countries.json"
2527
REQUEST_TIMEOUT_DEFAULT = 2
2628

2729
def __init__(self, access_token=None, **kwargs):
2830
"""Initialize the Handler object with country name list and the cache initialized."""
2931
self.access_token = access_token
30-
self.countries = self._read_country_names(kwargs.get('countries_file'))
32+
self.countries = self._read_country_names(kwargs.get("countries_file"))
3133

32-
self.request_options = kwargs.get('request_options', {})
33-
if 'timeout' not in self.request_options:
34-
self.request_options['timeout'] = self.REQUEST_TIMEOUT_DEFAULT
34+
self.request_options = kwargs.get("request_options", {})
35+
if "timeout" not in self.request_options:
36+
self.request_options["timeout"] = self.REQUEST_TIMEOUT_DEFAULT
3537

36-
if 'cache' in kwargs:
37-
self.cache = kwargs['cache']
38+
if "cache" in kwargs:
39+
self.cache = kwargs["cache"]
3840
else:
39-
cache_options = kwargs.get('cache_options', {})
40-
maxsize = cache_options.get('maxsize', self.CACHE_MAXSIZE)
41-
ttl = cache_options.get('ttl', self.CACHE_TTL)
41+
cache_options = kwargs.get("cache_options", {})
42+
maxsize = cache_options.get("maxsize", self.CACHE_MAXSIZE)
43+
ttl = cache_options.get("ttl", self.CACHE_TTL)
4244
self.cache = DefaultCache(maxsize, ttl, **cache_options)
4345

4446
def getDetails(self, ip_address=None):
4547
"""Get details for specified IP address as a Details object."""
4648
raw_details = self._requestDetails(ip_address)
47-
raw_details['country_name'] = self.countries.get(raw_details.get('country'))
48-
raw_details['ip_address'] = ipaddress.ip_address(raw_details.get('ip'))
49-
raw_details['latitude'], raw_details['longitude'] = self._read_coords(raw_details.get('loc'))
49+
raw_details["country_name"] = self.countries.get(raw_details.get("country"))
50+
raw_details["ip_address"] = ipaddress.ip_address(raw_details.get("ip"))
51+
raw_details["latitude"], raw_details["longitude"] = self._read_coords(
52+
raw_details.get("loc")
53+
)
5054
return Details(raw_details)
5155

5256
def _requestDetails(self, ip_address=None):
5357
"""Get IP address data by sending request to IPinfo API."""
5458
if ip_address not in self.cache:
5559
url = self.API_URL
5660
if ip_address:
57-
url += '/' + ip_address
61+
url += "/" + ip_address
5862

59-
response = requests.get(url, headers=self._get_headers(), **self.request_options)
63+
response = requests.get(
64+
url, headers=self._get_headers(), **self.request_options
65+
)
6066
if response.status_code == 429:
6167
raise RequestQuotaExceededError()
6268
response.raise_for_status()
@@ -67,26 +73,30 @@ def _requestDetails(self, ip_address=None):
6773
def _get_headers(self):
6874
"""Built headers for request to IPinfo API."""
6975
headers = {
70-
'user-agent': 'IPinfoClient/Python{version}/1.0'.format(version=sys.version_info[0]),
71-
'accept': 'application/json'
76+
"user-agent": "IPinfoClient/Python{version}/1.0".format(
77+
version=sys.version_info[0]
78+
),
79+
"accept": "application/json",
7280
}
7381

7482
if self.access_token:
75-
headers['authorization'] = 'Bearer {}'.format(self.access_token)
83+
headers["authorization"] = "Bearer {}".format(self.access_token)
7684

7785
return headers
7886

7987
def _read_coords(self, location):
8088
lat, lon = None, None
81-
coords = tuple(location.split(',')) if location else ''
89+
coords = tuple(location.split(",")) if location else ""
8290
if len(coords) == 2 and coords[0] and coords[1]:
8391
lat, lon = coords[0], coords[1]
8492
return lat, lon
8593

8694
def _read_country_names(self, countries_file=None):
8795
"""Read list of countries from specified country file or default file."""
8896
if not countries_file:
89-
countries_file = os.path.join(os.path.dirname(__file__), self.COUNTRY_FILE_DEFAULT)
97+
countries_file = os.path.join(
98+
os.path.dirname(__file__), self.COUNTRY_FILE_DEFAULT
99+
)
90100
with open(countries_file) as f:
91101
countries_json = f.read()
92102

requirements.in

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
cachetools==2.1.0
2-
pip-tools==3.1.0
3-
pycodestyle==2.4.0
4-
pytest==3.8.2
5-
python-dateutil==2.6.1
6-
pytz==2017.2
1+
# For app
72
requests>=2.18.4
3+
cachetools==3.1.1
4+
pytest==4.5.0
5+
6+
# For dev
7+
pip-tools==3.7.0
8+
black==19.3b0

requirements.txt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22
# This file is autogenerated by pip-compile
33
# To update, run:
44
#
5-
# pip-compile --no-index --output-file requirements.txt requirements.in
5+
# pip-compile --no-index --output-file=requirements.txt requirements.in
66
#
7+
appdirs==1.4.3 # via black
78
atomicwrites==1.3.0 # via pytest
8-
attrs==19.1.0 # via pytest
9-
cachetools==2.1.0
9+
attrs==19.1.0 # via black, pytest
10+
black==19.3b0
11+
cachetools==3.1.1
1012
certifi==2019.3.9 # via requests
1113
chardet==3.0.4 # via requests
12-
click==7.0 # via pip-tools
14+
click==7.0 # via black, pip-tools
1315
idna==2.8 # via requests
1416
more-itertools==6.0.0 # via pytest
15-
pip-tools==3.1.0
17+
pip-tools==3.7.0
1618
pluggy==0.9.0 # via pytest
1719
py==1.8.0 # via pytest
18-
pycodestyle==2.4.0
19-
pytest==3.8.2
20-
python-dateutil==2.6.1
21-
pytz==2017.2
20+
pytest==4.5.0
2221
requests==2.21.0
23-
six==1.12.0 # via pip-tools, pytest, python-dateutil
22+
six==1.12.0 # via pip-tools, pytest
23+
toml==0.10.0 # via black
2424
urllib3==1.24.1 # via requests
25+
wcwidth==0.1.7 # via pytest

setup.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@
88
You can visit our developer docs at https://ipinfo.io/developers.
99
"""
1010

11-
setup(name='ipinfo',
12-
version='1.1.1',
13-
description='Official Python library for IPInfo',
14-
long_description=long_description,
15-
url='https://github.com/ipinfo/python',
16-
author='James Timmins',
17-
author_email='[email protected]',
18-
license='Apache License 2.0',
19-
packages=['ipinfo', 'ipinfo.cache'],
20-
install_requires=[
21-
'requests',
22-
'cachetools',
23-
],
24-
include_package_data=True,
25-
zip_safe=False)
11+
setup(
12+
name="ipinfo",
13+
version="1.1.2",
14+
description="Official Python library for IPInfo",
15+
long_description=long_description,
16+
url="https://github.com/ipinfo/python",
17+
author="James Timmins",
18+
author_email="[email protected]",
19+
license="Apache License 2.0",
20+
packages=["ipinfo", "ipinfo.cache"],
21+
install_requires=["requests", "cachetools", "six"],
22+
include_package_data=True,
23+
zip_safe=False,
24+
)

tests/details_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
23
from ipinfo.details import Details
34

45

tests/handler_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import ipaddress
2+
13
from ipinfo.cache.default import DefaultCache
24
from ipinfo.details import Details
35
from ipinfo.handler import Handler
4-
import ipaddress
56

67

78
def test_init():

0 commit comments

Comments
 (0)