Skip to content

Commit 8d871d5

Browse files
committed
Merge pull request maxmind#36 from maxmind/greg/credit-exceptions
Add credit errors, use ipaddress, doc tweaks
2 parents 5df4909 + 8428299 commit 8d871d5

File tree

9 files changed

+129
-99
lines changed

9 files changed

+129
-99
lines changed

HISTORY.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
History
44
-------
55

6-
2.3.1 (2016-XX-XX)
6+
2.4.0 (2016-XX-XX)
77
++++++++++++++++++
88

9+
* This module now uses ``ipaddress`` on Python 2 rather than ``ipaddr`` to
10+
validate IP addresses before sending them to the web service.
11+
* Added handling of additional error codes that the web service may return.
12+
* PEP 257 documentation fixes.
913
* Updated documentation to reflect that the accuracy radius is now included
1014
in City.
1115

geoip2/compat.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Intended for internal use only."""
2+
import sys
3+
4+
import ipaddress
5+
6+
# pylint: skip-file
7+
8+
if sys.version_info[0] == 2:
9+
def compat_ip_address(address):
10+
"""Intended for internal use only."""
11+
if isinstance(address, bytes):
12+
address = address.decode()
13+
return ipaddress.ip_address(address)
14+
else:
15+
def compat_ip_address(address):
16+
"""Intended for internal use only."""
17+
return ipaddress.ip_address(address)

geoip2/database.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Reader(object):
4141
"""
4242

4343
def __init__(self, filename, locales=None, mode=MODE_AUTO):
44-
"""Create GeoIP2 Reader
44+
"""Create GeoIP2 Reader.
4545
4646
:param filename: The path to the GeoIP2 database.
4747
:param locales: This is list of locale codes. This argument will be
@@ -89,7 +89,7 @@ def __exit__(self, exc_type, exc_value, traceback):
8989
self.close()
9090

9191
def country(self, ip_address):
92-
"""Get the Country object for the IP address
92+
"""Get the Country object for the IP address.
9393
9494
:param ip_address: IPv4 or IPv6 address as a string.
9595
@@ -100,7 +100,7 @@ def country(self, ip_address):
100100
return self._model_for(geoip2.models.Country, 'Country', ip_address)
101101

102102
def city(self, ip_address):
103-
"""Get the City object for the IP address
103+
"""Get the City object for the IP address.
104104
105105
:param ip_address: IPv4 or IPv6 address as a string.
106106
@@ -110,7 +110,7 @@ def city(self, ip_address):
110110
return self._model_for(geoip2.models.City, 'City', ip_address)
111111

112112
def anonymous_ip(self, ip_address):
113-
"""Get the AnonymousIP object for the IP address
113+
"""Get the AnonymousIP object for the IP address.
114114
115115
:param ip_address: IPv4 or IPv6 address as a string.
116116
@@ -121,7 +121,7 @@ def anonymous_ip(self, ip_address):
121121
'GeoIP2-Anonymous-IP', ip_address)
122122

123123
def connection_type(self, ip_address):
124-
"""Get the ConnectionType object for the IP address
124+
"""Get the ConnectionType object for the IP address.
125125
126126
:param ip_address: IPv4 or IPv6 address as a string.
127127
@@ -132,7 +132,7 @@ def connection_type(self, ip_address):
132132
'GeoIP2-Connection-Type', ip_address)
133133

134134
def domain(self, ip_address):
135-
"""Get the Domain object for the IP address
135+
"""Get the Domain object for the IP address.
136136
137137
:param ip_address: IPv4 or IPv6 address as a string.
138138
@@ -143,7 +143,7 @@ def domain(self, ip_address):
143143
ip_address)
144144

145145
def enterprise(self, ip_address):
146-
"""Get the Enterprise object for the IP address
146+
"""Get the Enterprise object for the IP address.
147147
148148
:param ip_address: IPv4 or IPv6 address as a string.
149149
@@ -154,7 +154,7 @@ def enterprise(self, ip_address):
154154
ip_address)
155155

156156
def isp(self, ip_address):
157-
"""Get the ISP object for the IP address
157+
"""Get the ISP object for the IP address.
158158
159159
:param ip_address: IPv4 or IPv6 address as a string.
160160
@@ -187,13 +187,13 @@ def _flat_model_for(self, model_class, types, ip_address):
187187
return model_class(record)
188188

189189
def metadata(self):
190-
"""The metadata for the open database
190+
"""The metadata for the open database.
191191
192192
:returns: :py:class:`maxminddb.reader.Metadata` object
193193
"""
194194
return self._db_reader.metadata()
195195

196196
def close(self):
197-
"""Closes the GeoIP2 database"""
197+
"""Closes the GeoIP2 database."""
198198

199199
self._db_reader.close()

geoip2/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ class InvalidRequestError(GeoIP2Error):
4545

4646
class OutOfQueriesError(GeoIP2Error):
4747
"""Your account is out of funds for the service queried."""
48+
49+
50+
class PermissionRequiredError(GeoIP2Error):
51+
"""Your account does not have permission to access this service."""

geoip2/models.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
class Country(SimpleEquality):
21-
"""Model for the GeoIP2 Precision: Country and the GeoIP2 Country database
21+
"""Model for the GeoIP2 Precision: Country and the GeoIP2 Country database.
2222
2323
This class provides the following attributes:
2424
@@ -99,7 +99,8 @@ def __repr__(self):
9999

100100

101101
class City(Country):
102-
"""Model for the GeoIP2 Precision: City and the GeoIP2 City database
102+
"""Model for the GeoIP2 Precision: City and the GeoIP2 City database.
103+
103104
.. attribute:: city
104105
105106
City object for the requested IP address.
@@ -174,7 +175,7 @@ def __init__(self, raw_response, locales=None):
174175

175176

176177
class Insights(City):
177-
"""Model for the GeoIP2 Precision: Insights web service endpoint
178+
"""Model for the GeoIP2 Precision: Insights web service endpoint.
178179
179180
.. attribute:: city
180181
@@ -238,7 +239,7 @@ class Insights(City):
238239

239240

240241
class Enterprise(City):
241-
"""Model for the GeoIP2 Enterprise database
242+
"""Model for the GeoIP2 Enterprise database.
242243
243244
.. attribute:: city
244245
@@ -315,7 +316,7 @@ def __repr__(self):
315316

316317

317318
class AnonymousIP(SimpleModel):
318-
"""Model class for the GeoIP2 Anonymous IP
319+
"""Model class for the GeoIP2 Anonymous IP.
319320
320321
This class provides the following attribute:
321322
@@ -368,7 +369,7 @@ def __init__(self, raw):
368369

369370

370371
class ConnectionType(SimpleModel):
371-
"""Model class for the GeoIP2 Connection-Type
372+
"""Model class for the GeoIP2 Connection-Type.
372373
373374
This class provides the following attribute:
374375
@@ -399,7 +400,7 @@ def __init__(self, raw):
399400

400401

401402
class Domain(SimpleModel):
402-
"""Model class for the GeoIP2 Domain
403+
"""Model class for the GeoIP2 Domain.
403404
404405
This class provides the following attribute:
405406
@@ -424,7 +425,7 @@ def __init__(self, raw):
424425

425426

426427
class ISP(SimpleModel):
427-
"""Model class for the GeoIP2 ISP
428+
"""Model class for the GeoIP2 ISP.
428429
429430
This class provides the following attribute:
430431

0 commit comments

Comments
 (0)