Skip to content

Commit e3e1cae

Browse files
committed
Add docstrings.
1 parent cd33976 commit e3e1cae

File tree

7 files changed

+31
-6
lines changed

7 files changed

+31
-6
lines changed

ipinfo_wrapper/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from .handler import Handler
22

3+
34
def getHandler(access_token=None, **kwargs):
5+
"""Create and return Handler object."""
46
return Handler(access_token, **kwargs)

ipinfo_wrapper/cache/default.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from .interface import CacheInterface
21
import cachetools
2+
from .interface import CacheInterface
3+
34

45
class DefaultCache(CacheInterface):
6+
"""Default, in-memory cache."""
57

68
def __init__(self, maxsize, ttl, **cache_options):
79
self.cache = cachetools.TTLCache(maxsize, ttl, **cache_options)

ipinfo_wrapper/cache/interface.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import abc
22

3+
34
class CacheInterface(metaclass=abc.ABCMeta):
5+
"""Interface for using custom cache."""
46

57
@abc.abstractmethod
68
def __contains__(self, key):

ipinfo_wrapper/details.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
class Details:
2+
"""Encapsulates data for single IP address."""
23

34
def __init__(self, details):
5+
"""Initialize by settings `details` attribute."""
46
self.details = details
57

68
def __getattr__(self, attr):
9+
"""Return attribute if it exists in details array, else return error."""
710
if attr in self.details:
811
return self.details[attr]
912
else:
1013
raise AttributeError('{attr} is not a valid attribute of Details'.format(attr))
1114

1215
@property
1316
def all(self):
17+
"""Return all details as dict."""
1418
return self.details

ipinfo_wrapper/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class RequestQuotaExceededError(Exception):
2+
"""Error indicating that users monthly request quota has been passed."""
23
pass

ipinfo_wrapper/handler.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
from .details import Details
99
from .exceptions import RequestQuotaExceededError
1010

11+
1112
class Handler():
13+
"""
14+
Allows client to request data for specified IP address. Instantiates and
15+
and maintains access to cache.
16+
"""
1217

1318
API_URL = 'https://ipinfo.io'
1419
CACHE_MAXSIZE = 4096
1520
CACHE_TTL = 60 * 60 * 24
1621
COUNTRY_FILE_DEFAULT = 'countries.json'
1722

1823
def __init__(self, access_token=None, **kwargs):
24+
"""Initialize the Handler object with country name list and the cache initialized."""
1925
self.access_token = access_token
2026
self.countries = self._read_country_names(kwargs.get('countries_file'))
2127

@@ -27,20 +33,22 @@ def __init__(self, access_token=None, **kwargs):
2733
ttl = cache_options.get('ttl', self.CACHE_TTL)
2834
self.cache = DefaultCache(maxsize, ttl, **cache_options)
2935

30-
def getDetails(self, ip_address = None):
36+
def getDetails(self, ip_address=None):
37+
"""Get details for specified IP address as a Details object."""
3138
raw_details = self._requestDetails(ip_address)
3239
raw_details['country_name'] = self.countries.get(raw_details.get('country'))
3340
raw_details['ip_address'] = ipaddress.ip_address(raw_details.get('ip'))
3441

3542
lat, lon = None, None
36-
coords = tuple(raw_details.get('loc','').split(','))
43+
coords = tuple(raw_details.get('loc', '').split(','))
3744
if coords:
3845
lat, lon = coords[0], coords[1]
3946
raw_details['latitude'], raw_details['longitude'] = lat, lon
4047

4148
return Details(raw_details)
4249

43-
def _requestDetails(self, ip_address = None):
50+
def _requestDetails(self, ip_address=None):
51+
"""Get IP address data by sending request to IPinfo API."""
4452
if ip_address not in self.cache:
4553
url = self.API_URL
4654
if ip_address:
@@ -55,6 +63,7 @@ def _requestDetails(self, ip_address = None):
5563
return self.cache[ip_address]
5664

5765
def _get_headers(self):
66+
"""Built headers for request to IPinfo API."""
5867
headers = {
5968
'user-agent': 'IPinfoClient/Python{version}/1.0'.format(version=sys.version_info[0]),
6069
'accept': 'application/json'
@@ -65,7 +74,8 @@ def _get_headers(self):
6574

6675
return headers
6776

68-
def _read_country_names(self, countries_file = None):
77+
def _read_country_names(self, countries_file=None):
78+
"""Read list of countries from specified country file or default file."""
6979
if not countries_file:
7080
countries_file = os.path.join(os.path.dirname(__file__), self.COUNTRY_FILE_DEFAULT)
7181
with open(countries_file) as f:

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from setuptools import setup
22

33
setup(name='ipinfo_wrapper',
4-
version='0.1.3',
4+
version='0.1.4',
55
description='Official Python library for IPInfo',
66
url='https://github.com/ipinfo/python',
77
author='James Timmins',
88
author_email='[email protected]',
99
license='Apache License 2.0',
1010
packages=['ipinfo_wrapper', 'ipinfo_wrapper.cache'],
11+
install_requires=[
12+
'requests',
13+
'cachetools',
14+
],
1115
include_package_data=True,
1216
zip_safe=False)

0 commit comments

Comments
 (0)