Skip to content

Commit 0b07b65

Browse files
committed
Move settings from get request to session creation
1 parent 2b438e8 commit 0b07b65

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ History
1818
* ``user_id`` is no longer supported as a named argument for the constructor
1919
on ``geoip2.webservice.Client``. Use ``account_id`` or a positional
2020
parameter instead.
21+
* For both ``Client`` and ``AsyncClient`` requests, the default timeout is
22+
now 60 seconds.
2123

2224
3.0.0 (2019-12-20)
2325
++++++++++++++++++

geoip2/webservice.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class BaseClient: # pylint: disable=missing-class-docstring, too-few-public-met
5353
_host: str
5454
_license_key: str
5555
_locales: List[str]
56-
_timeout: Optional[float]
56+
_timeout: float
5757
_user_agent: str
5858

5959
def __init__(
@@ -62,7 +62,7 @@ def __init__(
6262
license_key: str,
6363
host: str,
6464
locales: Optional[List[str]],
65-
timeout: Optional[float],
65+
timeout: float,
6666
http_user_agent: str,
6767
) -> None:
6868
"""Construct a Client."""
@@ -233,8 +233,9 @@ class AsyncClient(BaseClient):
233233
* pt-BR -- Brazilian Portuguese
234234
* ru -- Russian
235235
* zh-CN -- Simplified Chinese.
236-
:param timeout: The timeout to use when waiting on the request. This sets
237-
both the connect timeout and the read timeout.
236+
:param timeout: The timeout in seconts to use when waiting on the request.
237+
This sets both the connect timeout and the read timeout. The default is
238+
60.
238239
239240
"""
240241

@@ -246,7 +247,7 @@ def __init__( # pylint: disable=too-many-arguments
246247
license_key: str,
247248
host: str = "geoip.maxmind.com",
248249
locales: Optional[List[str]] = None,
249-
timeout: Optional[float] = None,
250+
timeout: float = 60,
250251
) -> None:
251252
super().__init__(
252253
account_id, license_key, host, locales, timeout, default_user_agent()
@@ -298,7 +299,12 @@ async def insights(self, ip_address: IPAddress = "me") -> Insights:
298299

299300
async def _session(self) -> aiohttp.ClientSession:
300301
if not hasattr(self, "_existing_session"):
301-
self._existing_session = aiohttp.ClientSession()
302+
self._existing_session = aiohttp.ClientSession(
303+
auth=aiohttp.BasicAuth(self._account_id, self._license_key),
304+
headers={"Accept": "application/json", "User-Agent": self._user_agent},
305+
timeout=aiohttp.ClientTimeout(total=self._timeout),
306+
)
307+
302308
return self._existing_session
303309

304310
async def _response_for(
@@ -309,12 +315,7 @@ async def _response_for(
309315
) -> Union[Country, City, Insights]:
310316
uri = self._uri(path, ip_address)
311317
session = await self._session()
312-
async with await session.get(
313-
uri,
314-
auth=aiohttp.BasicAuth(self._account_id, self._license_key),
315-
headers={"Accept": "application/json", "User-Agent": self._user_agent},
316-
timeout=self._timeout,
317-
) as response:
318+
async with await session.get(uri) as response:
318319
status = response.status
319320
content_type = response.content_type
320321
body = await response.text()
@@ -378,8 +379,9 @@ class Client(BaseClient):
378379
* pt-BR -- Brazilian Portuguese
379380
* ru -- Russian
380381
* zh-CN -- Simplified Chinese.
381-
:param timeout: The timeout to use when waiting on the request. This sets
382-
both the connect timeout and the read timeout.
382+
:param timeout: The timeout in seconts to use when waiting on the request.
383+
This sets both the connect timeout and the read timeout. The default is
384+
60.
383385
384386
"""
385387

@@ -391,12 +393,15 @@ def __init__( # pylint: disable=too-many-arguments
391393
license_key: str,
392394
host: str = "geoip.maxmind.com",
393395
locales: Optional[List[str]] = None,
394-
timeout: Optional[float] = None,
396+
timeout: float = 60,
395397
) -> None:
396398
super().__init__(
397399
account_id, license_key, host, locales, timeout, default_user_agent()
398400
)
399401
self._session = requests.Session()
402+
self._session.auth = (self._account_id, self._license_key)
403+
self._session.headers["Accept"] = "application/json"
404+
self._session.headers["User-Agent"] = self._user_agent
400405

401406
def city(self, ip_address: IPAddress = "me") -> City:
402407
"""Call GeoIP2 Precision City endpoint with the specified IP.
@@ -445,12 +450,7 @@ def _response_for(
445450
ip_address: IPAddress,
446451
) -> Union[Country, City, Insights]:
447452
uri = self._uri(path, ip_address)
448-
response = self._session.get(
449-
uri,
450-
auth=(self._account_id, self._license_key),
451-
headers={"Accept": "application/json", "User-Agent": self._user_agent},
452-
timeout=self._timeout,
453-
)
453+
response = self._session.get(uri, timeout=self._timeout)
454454
status = response.status_code
455455
content_type = response.headers["Content-Type"]
456456
body = response.text

0 commit comments

Comments
 (0)