-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathwebservice.py
527 lines (434 loc) · 17.3 KB
/
webservice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
"""
============================
WebServices Client API
============================
This class provides a client API for all the GeoIP2 Precision web service end
points. The end points are Country, City, and Insights. Each end point returns
a different set of data about an IP address, with Country returning the least
data and Insights the most.
Each web service end point is represented by a different model class, and
these model classes in turn contain multiple record classes. The record
classes have attributes which contain data about the IP address.
If the web service does not return a particular piece of data for an IP
address, the associated attribute is not populated.
The web service may not return any information for an entire record, in which
case all of the attributes for that record class will be empty.
SSL
---
Requests to the GeoIP2 Precision web service are always made with SSL.
"""
import ipaddress
import json
import sys
from typing import Any, Dict, cast, List, Optional, Type, Union
try:
import aiohttp
import aiohttp.http
except ImportError:
pass
try:
import requests
import requests.utils
except ImportError:
pass
import geoip2
import geoip2.models
from geoip2.errors import (
AddressNotFoundError,
AuthenticationError,
GeoIP2Error,
HTTPError,
InvalidRequestError,
OutOfQueriesError,
PermissionRequiredError,
)
from geoip2.models import City, Country, Insights
from geoip2.types import IPAddress
# If neither requests or aiohttp is installed then inform user how to
# install them
if "aiohttp" not in sys.modules and "requests" not in sys.modules:
raise ImportError(
"""To enable geoip2.webservice,
install aiohttp or requests support.
pip install geoip2[aiohttp]
pip install geoip2[requests]
pip install geoip2[all]"""
)
if "aiohttp" in sys.modules:
_AIOHTTP_UA = (
f"GeoIP2-Python-Client/{geoip2.__version__} {aiohttp.http.SERVER_SOFTWARE}"
)
if "requests" in sys.modules:
_REQUEST_UA = (
f"GeoIP2-Python-Client/{geoip2.__version__}"
f" {requests.utils.default_user_agent()}"
)
class BaseClient: # pylint: disable=missing-class-docstring, too-few-public-methods
_account_id: str
_host: str
_license_key: str
_locales: List[str]
_timeout: float
def __init__(
self,
account_id: int,
license_key: str,
host: str,
locales: Optional[List[str]],
timeout: float,
) -> None:
"""Construct a Client."""
# pylint: disable=too-many-arguments
if locales is None:
locales = ["en"]
self._locales = locales
# requests 2.12.2 requires that the username passed to auth be bytes
# or a string, with the former being preferred.
self._account_id = (
account_id if isinstance(account_id, bytes) else str(account_id)
)
self._license_key = license_key
self._base_uri = f"https://{host}/geoip/v2.1"
self._timeout = timeout
def _uri(self, path: str, ip_address: IPAddress) -> str:
if ip_address != "me":
ip_address = ipaddress.ip_address(ip_address)
return "/".join([self._base_uri, path, str(ip_address)])
@staticmethod
def _handle_success(body: str, uri: str) -> Any:
try:
return json.loads(body)
except ValueError as ex:
raise GeoIP2Error(
f"Received a 200 response for {uri}"
" but could not decode the response as "
"JSON: " + ", ".join(ex.args),
200,
uri,
) from ex
def _exception_for_error(
self, status: int, content_type: str, body: str, uri: str
) -> GeoIP2Error:
if 400 <= status < 500:
return self._exception_for_4xx_status(status, content_type, body, uri)
if 500 <= status < 600:
return self._exception_for_5xx_status(status, uri, body)
return self._exception_for_non_200_status(status, uri, body)
def _exception_for_4xx_status(
self, status: int, content_type: str, body: str, uri: str
) -> GeoIP2Error:
if not body:
return HTTPError(
f"Received a {status} error for {uri} with no body.",
status,
uri,
body,
)
if content_type.find("json") == -1:
return HTTPError(
f"Received a {status} for {uri} with the following body: {body}",
status,
uri,
body,
)
try:
decoded_body = json.loads(body)
except ValueError as ex:
return HTTPError(
f"Received a {status} error for {uri} but it did not include "
+ "the expected JSON body: "
+ ", ".join(ex.args),
status,
uri,
body,
)
else:
if "code" in decoded_body and "error" in decoded_body:
return self._exception_for_web_service_error(
decoded_body.get("error"), decoded_body.get("code"), status, uri
)
return HTTPError(
"Response contains JSON but it does not specify code or error keys",
status,
uri,
body,
)
@staticmethod
def _exception_for_web_service_error(
message: str, code: str, status: int, uri: str
) -> Union[
AuthenticationError,
AddressNotFoundError,
PermissionRequiredError,
OutOfQueriesError,
InvalidRequestError,
]:
if code in ("IP_ADDRESS_NOT_FOUND", "IP_ADDRESS_RESERVED"):
return AddressNotFoundError(message)
if code in (
"ACCOUNT_ID_REQUIRED",
"ACCOUNT_ID_UNKNOWN",
"AUTHORIZATION_INVALID",
"LICENSE_KEY_REQUIRED",
"USER_ID_REQUIRED",
"USER_ID_UNKNOWN",
):
return AuthenticationError(message)
if code in ("INSUFFICIENT_FUNDS", "OUT_OF_QUERIES"):
return OutOfQueriesError(message)
if code == "PERMISSION_REQUIRED":
return PermissionRequiredError(message)
return InvalidRequestError(message, code, status, uri)
@staticmethod
def _exception_for_5xx_status(
status: int, uri: str, body: Optional[str]
) -> HTTPError:
return HTTPError(
f"Received a server error ({status}) for {uri}",
status,
uri,
body,
)
@staticmethod
def _exception_for_non_200_status(
status: int, uri: str, body: Optional[str]
) -> HTTPError:
return HTTPError(
f"Received a very surprising HTTP status ({status}) for {uri}",
status,
uri,
body,
)
class AsyncClient(BaseClient):
"""An async GeoIP2 client.
It accepts the following required arguments:
:param account_id: Your MaxMind account ID.
:param license_key: Your MaxMind license key.
Go to https://www.maxmind.com/en/my_license_key to see your MaxMind
account ID and license key.
The following keyword arguments are also accepted:
:param host: The hostname to make a request against. This defaults to
"geoip.maxmind.com". In most cases, you should not need to set this
explicitly.
:param locales: This is list of locale codes. This argument will be
passed on to record classes to use when their name properties are
called. The default value is ['en'].
The order of the locales is significant. When a record class has
multiple names (country, city, etc.), its name property will return
the name in the first locale that has one.
Note that the only locale which is always present in the GeoIP2
data is "en". If you do not include this locale, the name property
may end up returning None even when the record has an English name.
Currently, the valid locale codes are:
* de -- German
* en -- English names may still include accented characters if that is
the accepted spelling in English. In other words, English does not
mean ASCII.
* es -- Spanish
* fr -- French
* ja -- Japanese
* pt-BR -- Brazilian Portuguese
* ru -- Russian
* zh-CN -- Simplified Chinese.
:param timeout: The timeout in seconds to use when waiting on the request.
This sets both the connect timeout and the read timeout. The default is
60.
:param proxy: The URL of an HTTP proxy to use. It may optionally include
a basic auth username and password, e.g.,
``http://username:password@host:port``.
"""
_existing_session: aiohttp.ClientSession
_proxy: Optional[str]
def __init__( # pylint: disable=too-many-arguments
self,
account_id: int,
license_key: str,
host: str = "geoip.maxmind.com",
locales: Optional[List[str]] = None,
timeout: float = 60,
proxy: Optional[str] = None,
) -> None:
super().__init__(
account_id,
license_key,
host,
locales,
timeout,
)
self._proxy = proxy
async def city(self, ip_address: IPAddress = "me") -> City:
"""Call GeoIP2 Precision City endpoint with the specified IP.
:param ip_address: IPv4 or IPv6 address as a string. If no
address is provided, the address that the web service is
called from will be used.
:returns: :py:class:`geoip2.models.City` object
"""
return cast(
City, await self._response_for("city", geoip2.models.City, ip_address)
)
async def country(self, ip_address: IPAddress = "me") -> Country:
"""Call the GeoIP2 Country endpoint with the specified IP.
:param ip_address: IPv4 or IPv6 address as a string. If no address
is provided, the address that the web service is called from will
be used.
:returns: :py:class:`geoip2.models.Country` object
"""
return cast(
Country,
await self._response_for("country", geoip2.models.Country, ip_address),
)
async def insights(self, ip_address: IPAddress = "me") -> Insights:
"""Call the GeoIP2 Precision: Insights endpoint with the specified IP.
:param ip_address: IPv4 or IPv6 address as a string. If no address
is provided, the address that the web service is called from will
be used.
:returns: :py:class:`geoip2.models.Insights` object
"""
return cast(
Insights,
await self._response_for("insights", geoip2.models.Insights, ip_address),
)
async def _session(self) -> aiohttp.ClientSession:
if not hasattr(self, "_existing_session"):
self._existing_session = aiohttp.ClientSession(
auth=aiohttp.BasicAuth(self._account_id, self._license_key),
headers={"Accept": "application/json", "User-Agent": _AIOHTTP_UA},
timeout=aiohttp.ClientTimeout(total=self._timeout),
)
return self._existing_session
async def _response_for(
self,
path: str,
model_class: Union[Type[Insights], Type[City], Type[Country]],
ip_address: IPAddress,
) -> Union[Country, City, Insights]:
uri = self._uri(path, ip_address)
session = await self._session()
async with await session.get(uri, proxy=self._proxy) as response:
status = response.status
content_type = response.content_type
body = await response.text()
if status != 200:
raise self._exception_for_error(status, content_type, body, uri)
decoded_body = self._handle_success(body, uri)
return model_class(decoded_body, locales=self._locales)
async def close(self):
"""Close underlying session
This will close the session and any associated connections.
"""
if hasattr(self, "_existing_session"):
await self._existing_session.close()
async def __aenter__(self) -> "AsyncClient":
return self
async def __aexit__(self, exc_type: None, exc_value: None, traceback: None) -> None:
await self.close()
class Client(BaseClient):
"""A synchronous GeoIP2 client.
It accepts the following required arguments:
:param account_id: Your MaxMind account ID.
:param license_key: Your MaxMind license key.
Go to https://www.maxmind.com/en/my_license_key to see your MaxMind
account ID and license key.
The following keyword arguments are also accepted:
:param host: The hostname to make a request against. This defaults to
"geoip.maxmind.com". In most cases, you should not need to set this
explicitly.
:param locales: This is list of locale codes. This argument will be
passed on to record classes to use when their name properties are
called. The default value is ['en'].
The order of the locales is significant. When a record class has
multiple names (country, city, etc.), its name property will return
the name in the first locale that has one.
Note that the only locale which is always present in the GeoIP2
data is "en". If you do not include this locale, the name property
may end up returning None even when the record has an English name.
Currently, the valid locale codes are:
* de -- German
* en -- English names may still include accented characters if that is
the accepted spelling in English. In other words, English does not
mean ASCII.
* es -- Spanish
* fr -- French
* ja -- Japanese
* pt-BR -- Brazilian Portuguese
* ru -- Russian
* zh-CN -- Simplified Chinese.
:param timeout: The timeout in seconds to use when waiting on the request.
This sets both the connect timeout and the read timeout. The default is
60.
:param proxy: The URL of an HTTP proxy to use. It may optionally include
a basic auth username and password, e.g.,
``http://username:password@host:port``.
"""
_session: requests.Session
_proxies: Optional[Dict[str, str]]
def __init__( # pylint: disable=too-many-arguments
self,
account_id: int,
license_key: str,
host: str = "geoip.maxmind.com",
locales: Optional[List[str]] = None,
timeout: float = 60,
proxy: Optional[str] = None,
) -> None:
super().__init__(account_id, license_key, host, locales, timeout)
self._session = requests.Session()
self._session.auth = (self._account_id, self._license_key)
self._session.headers["Accept"] = "application/json"
self._session.headers["User-Agent"] = _REQUEST_UA
if proxy is None:
self._proxies = None
else:
self._proxies = {"https": proxy}
def city(self, ip_address: IPAddress = "me") -> City:
"""Call GeoIP2 Precision City endpoint with the specified IP.
:param ip_address: IPv4 or IPv6 address as a string. If no
address is provided, the address that the web service is
called from will be used.
:returns: :py:class:`geoip2.models.City` object
"""
return cast(City, self._response_for("city", geoip2.models.City, ip_address))
def country(self, ip_address: IPAddress = "me") -> Country:
"""Call the GeoIP2 Country endpoint with the specified IP.
:param ip_address: IPv4 or IPv6 address as a string. If no address
is provided, the address that the web service is called from will
be used.
:returns: :py:class:`geoip2.models.Country` object
"""
return cast(
Country, self._response_for("country", geoip2.models.Country, ip_address)
)
def insights(self, ip_address: IPAddress = "me") -> Insights:
"""Call the GeoIP2 Precision: Insights endpoint with the specified IP.
:param ip_address: IPv4 or IPv6 address as a string. If no address
is provided, the address that the web service is called from will
be used.
:returns: :py:class:`geoip2.models.Insights` object
"""
return cast(
Insights, self._response_for("insights", geoip2.models.Insights, ip_address)
)
def _response_for(
self,
path: str,
model_class: Union[Type[Insights], Type[City], Type[Country]],
ip_address: IPAddress,
) -> Union[Country, City, Insights]:
uri = self._uri(path, ip_address)
response = self._session.get(uri, proxies=self._proxies, timeout=self._timeout)
status = response.status_code
content_type = response.headers["Content-Type"]
body = response.text
if status != 200:
raise self._exception_for_error(status, content_type, body, uri)
decoded_body = self._handle_success(body, uri)
return model_class(decoded_body, locales=self._locales)
def close(self):
"""Close underlying session
This will close the session and any associated connections.
"""
self._session.close()
def __enter__(self) -> "Client":
return self
def __exit__(self, exc_type: None, exc_value: None, traceback: None) -> None:
self.close()