|
25 | 25 |
|
26 | 26 | """ |
27 | 27 |
|
| 28 | +import sys |
| 29 | + |
28 | 30 | import geoip2 |
29 | 31 | import geoip2.models |
30 | 32 | import requests |
|
33 | 35 | GeoIP2Error, HTTPError, InvalidRequestError, |
34 | 36 | OutOfQueriesError) |
35 | 37 |
|
36 | | -import sys |
37 | 38 |
|
38 | 39 | if sys.version_info[0] == 2 or (sys.version_info[0] == 3 |
39 | 40 | and sys.version_info[1] < 3): |
40 | 41 | import ipaddr as ipaddress # pylint:disable=F0401 |
| 42 | + |
41 | 43 | ipaddress.ip_address = ipaddress.IPAddress |
42 | 44 | else: |
43 | 45 | import ipaddress # pylint:disable=F0401 |
44 | 46 |
|
45 | 47 |
|
46 | 48 | class Client(object): |
47 | | - |
48 | 49 | """Creates a new client object. |
49 | 50 |
|
50 | 51 | It accepts the following required arguments: |
@@ -88,13 +89,14 @@ class Client(object): |
88 | 89 | """ |
89 | 90 |
|
90 | 91 | def __init__(self, user_id, license_key, host='geoip.maxmind.com', |
91 | | - locales=None): |
| 92 | + locales=None, timeout=None): |
92 | 93 | if locales is None: |
93 | 94 | locales = ['en'] |
94 | 95 | self._locales = locales |
95 | 96 | self._user_id = user_id |
96 | 97 | self._license_key = license_key |
97 | 98 | self._base_uri = 'https://%s/geoip/v2.1' % host |
| 99 | + self._timeout = timeout |
98 | 100 |
|
99 | 101 | def city(self, ip_address='me'): |
100 | 102 | """This method calls the GeoIP2 Precision City endpoint. |
@@ -140,7 +142,8 @@ def _response_for(self, path, model_class, ip_address): |
140 | 142 | uri = '/'.join([self._base_uri, path, ip_address]) |
141 | 143 | response = requests.get(uri, auth=(self._user_id, self._license_key), |
142 | 144 | headers={'Accept': 'application/json', |
143 | | - 'User-Agent': self._user_agent()}) |
| 145 | + 'User-Agent': self._user_agent()}, |
| 146 | + timeout=self._timeout) |
144 | 147 | if response.status_code == 200: |
145 | 148 | body = self._handle_success(response, uri) |
146 | 149 | return model_class(body, locales=self._locales) |
@@ -214,6 +217,8 @@ def _handle_non_200_status(self, status, uri): |
214 | 217 | raise HTTPError('Received a very surprising HTTP status ' |
215 | 218 | '(%(status)i) for %(uri)s' % locals(), status, |
216 | 219 | uri) |
| 220 | + |
| 221 | + |
217 | 222 | """ |
218 | 223 |
|
219 | 224 | :copyright: (c) 2014 by MaxMind, Inc. |
|
0 commit comments