Skip to content

Commit 8f6785b

Browse files
committed
Use correct UA string for aiohttp and calc once
1 parent 0b07b65 commit 8f6785b

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

geoip2/webservice.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
from typing import Any, cast, List, Optional, Type, Union
3131

3232
import aiohttp
33+
import aiohttp.http
3334
import requests
34-
from requests.utils import default_user_agent
35+
import requests.utils
3536

3637
import geoip2
3738
import geoip2.models
@@ -47,14 +48,22 @@
4748
from geoip2.models import City, Country, Insights
4849
from geoip2.types import IPAddress
4950

51+
_AIOHTTP_UA = "GeoIP2-Python-Client/%s %s" % (
52+
geoip2.__version__,
53+
aiohttp.http.SERVER_SOFTWARE,
54+
)
55+
_REQUEST_UA = "GeoIP2-Python-Client/%s %s" % (
56+
geoip2.__version__,
57+
requests.utils.default_user_agent(),
58+
)
59+
5060

5161
class BaseClient: # pylint: disable=missing-class-docstring, too-few-public-methods
5262
_account_id: str
5363
_host: str
5464
_license_key: str
5565
_locales: List[str]
5666
_timeout: float
57-
_user_agent: str
5867

5968
def __init__(
6069
self,
@@ -63,7 +72,6 @@ def __init__(
6372
host: str,
6473
locales: Optional[List[str]],
6574
timeout: float,
66-
http_user_agent: str,
6775
) -> None:
6876
"""Construct a Client."""
6977
# pylint: disable=too-many-arguments
@@ -79,10 +87,6 @@ def __init__(
7987
self._license_key = license_key
8088
self._base_uri = "https://%s/geoip/v2.1" % host
8189
self._timeout = timeout
82-
self._user_agent = "GeoIP2-Python-Client/%s %s" % (
83-
geoip2.__version__,
84-
http_user_agent,
85-
)
8690

8791
def _uri(self, path: str, ip_address: IPAddress) -> str:
8892
if ip_address != "me":
@@ -250,7 +254,7 @@ def __init__( # pylint: disable=too-many-arguments
250254
timeout: float = 60,
251255
) -> None:
252256
super().__init__(
253-
account_id, license_key, host, locales, timeout, default_user_agent()
257+
account_id, license_key, host, locales, timeout,
254258
)
255259

256260
async def city(self, ip_address: IPAddress = "me") -> City:
@@ -301,7 +305,7 @@ async def _session(self) -> aiohttp.ClientSession:
301305
if not hasattr(self, "_existing_session"):
302306
self._existing_session = aiohttp.ClientSession(
303307
auth=aiohttp.BasicAuth(self._account_id, self._license_key),
304-
headers={"Accept": "application/json", "User-Agent": self._user_agent},
308+
headers={"Accept": "application/json", "User-Agent": _AIOHTTP_UA},
305309
timeout=aiohttp.ClientTimeout(total=self._timeout),
306310
)
307311

@@ -395,13 +399,11 @@ def __init__( # pylint: disable=too-many-arguments
395399
locales: Optional[List[str]] = None,
396400
timeout: float = 60,
397401
) -> None:
398-
super().__init__(
399-
account_id, license_key, host, locales, timeout, default_user_agent()
400-
)
402+
super().__init__(account_id, license_key, host, locales, timeout)
401403
self._session = requests.Session()
402404
self._session.auth = (self._account_id, self._license_key)
403405
self._session.headers["Accept"] = "application/json"
404-
self._session.headers["User-Agent"] = self._user_agent
406+
self._session.headers["User-Agent"] = _REQUEST_UA
405407

406408
def city(self, ip_address: IPAddress = "me") -> City:
407409
"""Call GeoIP2 Precision City endpoint with the specified IP.

0 commit comments

Comments
 (0)