Skip to content

Commit b062bfa

Browse files
committed
Updated tests and docs for ISP
1 parent 050244a commit b062bfa

File tree

5 files changed

+92
-29
lines changed

5 files changed

+92
-29
lines changed

README.rst

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,32 @@ Web Service Example
9797
>>> response.location.longitude
9898
-93.2323
9999
100+
Web Service Client Exceptions
101+
-----------------------------
102+
103+
For details on the possible errors returned by the web service itself, see
104+
http://dev.maxmind.com/geoip/geoip2/web-services for the GeoIP2 web service
105+
docs.
106+
107+
If the web service returns an explicit error document, this is thrown as a
108+
``AddressNotFoundError``, ``AuthenticationError``, ``InvalidRequestError``, or
109+
``OutOfQueriesError`` as appropriate. These all subclass ``GeoIP2Error``.
110+
111+
If some other sort of error occurs, this is thrown as an ``HTTPError``. This
112+
is thrown when some sort of unanticipated error occurs, such as the web
113+
service returning a 500 or an invalid error document. If the web service
114+
returns any status code besides 200, 4xx, or 5xx, this also becomes an
115+
``HTTPError``.
116+
117+
Finally, if the web service returns a 200 but the body is invalid, the client
118+
throws a ``GeoIP2Error``.
119+
100120
Database Example
101121
-------------------
102122

123+
City Database
124+
^^^^^^^^^^^^^
125+
103126
.. code-block:: pycon
104127
105128
>>> import geoip2.database
@@ -134,26 +157,65 @@ Database Example
134157
44.9733
135158
>>> response.location.longitude
136159
-93.2323
160+
>>> reader.close()
137161
138-
Web Service Client Exceptions
139-
-----------------------------
162+
Connection-Type Database
163+
^^^^^^^^^^^^^^^^^^^^^^^^
140164

141-
For details on the possible errors returned by the web service itself, see
142-
http://dev.maxmind.com/geoip/geoip2/web-services for the GeoIP2 web service
143-
docs.
165+
>>> import geoip2.database
166+
>>>
167+
>>> # This creates a Reader object. You should use the same object
168+
>>> # across multiple requests as creation of it is expensive.
169+
>>> reader = geoip2.database.Reader('/path/to/GeoIP2-Connection-Type.mmdb')
170+
>>>
171+
>>> response = reader.connection_type('128.101.101.101')
172+
>>>
173+
>>> response.connection_type
174+
'Corporate'
175+
>>> response.ip_address
176+
'128.101.101.101'
177+
>>> reader.close()
144178

145-
If the web service returns an explicit error document, this is thrown as a
146-
``AddressNotFoundError``, ``AuthenticationError``, ``InvalidRequestError``, or
147-
``OutOfQueriesError`` as appropriate. These all subclass ``GeoIP2Error``.
148179

149-
If some other sort of error occurs, this is thrown as an ``HTTPError``. This
150-
is thrown when some sort of unanticipated error occurs, such as the web
151-
service returning a 500 or an invalid error document. If the web service
152-
returns any status code besides 200, 4xx, or 5xx, this also becomes an
153-
``HTTPError``.
180+
Domain Database
181+
^^^^^^^^^^^^^^^
154182

155-
Finally, if the web service returns a 200 but the body is invalid, the client
156-
throws a ``GeoIP2Error``.
183+
>>> import geoip2.database
184+
>>>
185+
>>> # This creates a Reader object. You should use the same object
186+
>>> # across multiple requests as creation of it is expensive.
187+
>>> reader = geoip2.database.Reader('/path/to/GeoIP2-Domain.mmdb')
188+
>>>
189+
>>> response = reader.domain('128.101.101.101')
190+
>>>
191+
>>> response.domain
192+
'umn.edu'
193+
>>> response.ip_address
194+
'128.101.101.101'
195+
>>> reader.close()
196+
197+
ISP Database
198+
^^^^^^^^^^^^
199+
200+
>>> import geoip2.database
201+
>>>
202+
>>> # This creates a Reader object. You should use the same object
203+
>>> # across multiple requests as creation of it is expensive.
204+
>>> reader = geoip2.database.Reader('/path/to/GeoIP2-ISP.mmdb')
205+
>>>
206+
>>> response = reader.isp('1.128.0.0')
207+
>>>
208+
>>> response.autonomous_system_number
209+
1221
210+
>>> response.autonomous_system_organization
211+
'Telstra Pty Ltd'
212+
>>> response.isp
213+
'Telstra Internet'
214+
>>> response.organization
215+
'Telstra Internet'
216+
>>> response.ip_address
217+
'128.101.101.101'
218+
>>> reader.close()
157219

158220
Database Reader Exceptions
159221
--------------------------

geoip2/database.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ def domain(self, ip_address):
109109
"""
110110
return self._flat_model_for(geoip2.models.Domain, ip_address)
111111

112-
def isp_org(self, ip_address):
113-
"""Get the ISPOrg object for the IP address
112+
def isp(self, ip_address):
113+
"""Get the ISP object for the IP address
114114
115115
:param ip_address: IPv4 or IPv6 address as a string.
116116
117-
:returns: :py:class:`geoip2.models.ISPOrg` object
117+
:returns: :py:class:`geoip2.models.ISP` object
118118
119119
"""
120-
return self._flat_model_for(geoip2.models.ISPOrg, ip_address)
120+
return self._flat_model_for(geoip2.models.ISP, ip_address)
121121

122122
def _get(self, ip_address):
123123
record = self._db_reader.get(ip_address)

geoip2/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,9 @@ def __init__(self, raw):
358358
self.raw = raw
359359

360360

361-
class ISPOrg(object):
361+
class ISP(object):
362362

363-
"""Model class for the GeoIP2 ISP-Org
363+
"""Model class for the GeoIP2 ISP
364364
365365
This class provides the following attribute:
366366

tests/database_test.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,17 @@ def test_domain(self):
8282

8383
reader.close()
8484

85-
def test_isp_org(self):
85+
def test_isp(self):
8686
reader = geoip2.database.Reader(
87-
'tests/data/test-data/GeoIP2-ISP-Org-Test.mmdb')
87+
'tests/data/test-data/GeoIP2-ISP-Test.mmdb')
8888

89-
ip_address = '2001:1700::'
90-
record = reader.isp_org(ip_address)
91-
self.assertEqual(record.autonomous_system_number, 6730)
89+
ip_address = '1.128.0.0'
90+
record = reader.isp(ip_address)
91+
self.assertEqual(record.autonomous_system_number, 1221)
9292
self.assertEqual(record.autonomous_system_organization,
93-
'Sunrise Communications AG')
94-
# XXX - Add org/isp when available
93+
'Telstra Pty Ltd')
94+
self.assertEqual(record.isp, 'Telstra Internet')
95+
self.assertEqual(record.organization, 'Telstra Internet')
9596
self.assertEqual(record.ip_address, ip_address)
9697

9798
reader.close()

0 commit comments

Comments
 (0)