Skip to content

Commit 7daf146

Browse files
authored
Merge pull request #105 from maxmind/greg/geolite-ws
Document how to use GeoLite2 web service
2 parents 91440cd + 33ff7cc commit 7daf146

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

README.rst

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,20 @@ IP geolocation is inherently imprecise. Locations are often near the center of
4242
the population. Any location provided by a GeoIP2 database or web service
4343
should not be used to identify a particular address or household.
4444

45-
Usage
46-
-----
45+
Web Service Usage
46+
-----------------
4747

48-
To use this API, you first create either a web service object with your
49-
MaxMind ``account_id`` and ``license_key`` or a database reader object with the
50-
path to your database file. After doing this, you may call the method
51-
corresponding to request type (e.g., ``city`` or ``country``), passing it the
52-
IP address you want to look up.
48+
To use this API, you first construct either a ``geoip2.webservice.Client`` or
49+
``geoip2.webservice.AsyncClient``, passing your MaxMind ``account_id`` and
50+
``license_key`` to the constructor. To use the GeoLite2 web service instead of
51+
GeoIP2 Precision, set the optional ``host`` keyword argument to
52+
``geolite.info``.
53+
54+
After doing this, you may call the method corresponding to request type
55+
(e.g., ``city`` or ``country``), passing it the IP address you want to look up.
5356

5457
If the request succeeds, the method call will return a model class for the
55-
end point you called. This model in turn contains multiple record classes,
58+
endpoint you called. This model in turn contains multiple record classes,
5659
each of which represents part of the data returned by the web service.
5760

5861
If the request fails, the client class throws an exception.
@@ -66,12 +69,14 @@ Sync Web Service Example
6669
>>>
6770
>>> # This creates a Client object that can be reused across requests.
6871
>>> # Replace "42" with your account ID and "license_key" with your license
69-
>>> # key.
72+
>>> # key. Set the "host" keyword argument to "geolite.info" to use the
73+
>>> # GeoLite2 web service instead of GeoIP2 Precision.
7074
>>> with geoip2.webservice.Client(42, 'license_key') as client:
7175
>>>
72-
>>> # Replace "insights" with the method corresponding to the web service
73-
>>> # that you are using, e.g., "country", "city".
74-
>>> response = client.insights('203.0.113.0')
76+
>>> # Replace "city" with the method corresponding to the web service
77+
>>> # that you are using, i.e., "country", "city", or "insights". Please
78+
>>> # note that Insights is not supported by the GeoLite2 web service.
79+
>>> response = client.city('203.0.113.0')
7580
>>>
7681
>>> response.country.iso_code
7782
'US'
@@ -114,12 +119,14 @@ Async Web Service Example
114119
>>> # loops, you must ensure the object is not used on another loop.
115120
>>> #
116121
>>> # Replace "42" with your account ID and "license_key" with your license
117-
>>> # key.
122+
>>> # key. Set the "host" keyword argument to "geolite.info" to use the
123+
>>> # GeoLite2 web service instead of GeoIP2 Precision.
118124
>>> async with geoip2.webservice.AsyncClient(42, 'license_key') as client:
119125
>>>
120-
>>> # Replace "insights" with the method corresponding to the web service
121-
>>> # that you are using, e.g., "country", "city".
122-
>>> response = await client.insights('203.0.113.0')
126+
>>> # Replace "city" with the method corresponding to the web service
127+
>>> # that you are using, i.e., "country", "city", or "insights". Please
128+
>>> # note that Insights is not supported by the GeoLite2 web service.
129+
>>> response = await client.city('203.0.113.0')
123130
>>>
124131
>>> response.country.iso_code
125132
'US'
@@ -169,8 +176,22 @@ returns any status code besides 200, 4xx, or 5xx, this also becomes an
169176
Finally, if the web service returns a 200 but the body is invalid, the client
170177
throws a ``GeoIP2Error``.
171178

179+
Database Usage
180+
--------------
181+
182+
To use the database API, you first construct a ``geoip2.database.Reader`` using
183+
the path to the file as the first argument. After doing this, you may call the
184+
method corresponding to database type (e.g., ``city`` or ``country``), passing it
185+
the IP address you want to look up.
186+
187+
If the lookup succeeds, the method call will return a model class for the
188+
database method you called. This model in turn contains multiple record classes,
189+
each of which represents part of the data for the record.
190+
191+
If the request fails, the reader class throws an exception.
192+
172193
Database Example
173-
-------------------
194+
----------------
174195

175196
City Database
176197
^^^^^^^^^^^^^
@@ -399,7 +420,7 @@ What data is returned?
399420
----------------------
400421

401422
While many of the models contain the same basic records, the attributes which
402-
can be populated vary between web service end points or databases. In
423+
can be populated vary between web service endpoints or databases. In
403424
addition, while a model may offer a particular piece of data, MaxMind does not
404425
always have every piece of data for any given IP address.
405426

geoip2/webservice.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
SSL
2222
---
2323
24-
Requests to the GeoIP2 Precision web service are always made with SSL.
24+
Requests to the web service are always made with SSL.
2525
2626
"""
2727

@@ -219,8 +219,8 @@ class AsyncClient(BaseClient):
219219
The following keyword arguments are also accepted:
220220
221221
:param host: The hostname to make a request against. This defaults to
222-
"geoip.maxmind.com". In most cases, you should not need to set this
223-
explicitly.
222+
"geoip.maxmind.com". To use the GeoLite2 web service instead of GeoIP2
223+
Precision, set this to "geolite.info".
224224
:param locales: This is list of locale codes. This argument will be
225225
passed on to record classes to use when their name properties are
226226
called. The default value is ['en'].
@@ -276,7 +276,7 @@ def __init__( # pylint: disable=too-many-arguments
276276
self._proxy = proxy
277277

278278
async def city(self, ip_address: IPAddress = "me") -> City:
279-
"""Call GeoIP2 Precision City endpoint with the specified IP.
279+
"""Call City endpoint with the specified IP.
280280
281281
:param ip_address: IPv4 or IPv6 address as a string. If no
282282
address is provided, the address that the web service is
@@ -305,7 +305,10 @@ async def country(self, ip_address: IPAddress = "me") -> Country:
305305
)
306306

307307
async def insights(self, ip_address: IPAddress = "me") -> Insights:
308-
"""Call the GeoIP2 Precision: Insights endpoint with the specified IP.
308+
"""Call the Insights endpoint with the specified IP.
309+
310+
Insights is only supported by GeoIP2 Precision. The GeoLite2 web
311+
service does not support it.
309312
310313
:param ip_address: IPv4 or IPv6 address as a string. If no address
311314
is provided, the address that the web service is called from will
@@ -375,8 +378,8 @@ class Client(BaseClient):
375378
The following keyword arguments are also accepted:
376379
377380
:param host: The hostname to make a request against. This defaults to
378-
"geoip.maxmind.com". In most cases, you should not need to set this
379-
explicitly.
381+
"geoip.maxmind.com". To use the GeoLite2 web service instead of GeoIP2
382+
Precision, set this to "geolite.info".
380383
:param locales: This is list of locale codes. This argument will be
381384
passed on to record classes to use when their name properties are
382385
called. The default value is ['en'].
@@ -434,7 +437,7 @@ def __init__( # pylint: disable=too-many-arguments
434437
self._proxies = {"https": proxy}
435438

436439
def city(self, ip_address: IPAddress = "me") -> City:
437-
"""Call GeoIP2 Precision City endpoint with the specified IP.
440+
"""Call City endpoint with the specified IP.
438441
439442
:param ip_address: IPv4 or IPv6 address as a string. If no
440443
address is provided, the address that the web service is
@@ -460,7 +463,10 @@ def country(self, ip_address: IPAddress = "me") -> Country:
460463
)
461464

462465
def insights(self, ip_address: IPAddress = "me") -> Insights:
463-
"""Call the GeoIP2 Precision: Insights endpoint with the specified IP.
466+
"""Call the Insights endpoint with the specified IP.
467+
468+
Insights is only supported by GeoIP2 Precision. The GeoLite2 web
469+
service does not support it.
464470
465471
:param ip_address: IPv4 or IPv6 address as a string. If no address
466472
is provided, the address that the web service is called from will

0 commit comments

Comments
 (0)