Skip to content

Commit 6000250

Browse files
committed
Run black on the codebase.
1 parent 62fef91 commit 6000250

File tree

6 files changed

+40
-24
lines changed

6 files changed

+40
-24
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

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)