Skip to content

Commit 0eebbbd

Browse files
committed
Make aiohttp and requests optional dependencies
Closes maxmind#104 (supersedes it) Fixes maxmind#101
1 parent 645795b commit 0eebbbd

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

README.rst

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ To install the ``geoip2`` module, type:
1717
.. code-block:: bash
1818
1919
$ pip install geoip2
20+
$ pip install geoip2[aiohttp] # Install aiohttp as well
21+
$ pip install geoip2[requests] # Install requests as well
2022
2123
If you are not able to use pip, you may also use easy_install from the
2224
source directory:

geoip2/webservice.py

+28-14
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,25 @@
2525
2626
"""
2727

28+
from __future__ import annotations
29+
2830
import ipaddress
2931
import json
3032
from collections.abc import Sequence
3133
from typing import Any, Optional, Union, cast
3234

33-
import aiohttp
34-
import aiohttp.http
35-
import requests
36-
import requests.utils
35+
try:
36+
import aiohttp
37+
import aiohttp.http
38+
except ImportError:
39+
aiohttp = None # type: ignore[assignment]
40+
41+
try:
42+
import requests
43+
import requests.utils
44+
except ImportError:
45+
requests = None # type: ignore[assignment]
46+
3747

3848
import geoip2
3949
import geoip2.models
@@ -49,14 +59,6 @@
4959
from geoip2.models import City, Country, Insights
5060
from geoip2.types import IPAddress
5161

52-
_AIOHTTP_UA = (
53-
f"GeoIP2-Python-Client/{geoip2.__version__} {aiohttp.http.SERVER_SOFTWARE}"
54-
)
55-
56-
_REQUEST_UA = (
57-
f"GeoIP2-Python-Client/{geoip2.__version__} {requests.utils.default_user_agent()}"
58-
)
59-
6062

6163
class BaseClient: # pylint: disable=missing-class-docstring, too-few-public-methods
6264
_account_id: str
@@ -346,10 +348,19 @@ async def insights(self, ip_address: IPAddress = "me") -> Insights:
346348
)
347349

348350
async def _session(self) -> aiohttp.ClientSession:
351+
if aiohttp is None:
352+
raise ImportError(
353+
"aiohttp is required for async mode; install `GeoIP2[aiohttp]`"
354+
)
355+
349356
if not hasattr(self, "_existing_session"):
357+
user_agent = (
358+
f"GeoIP2-Python-Client/{geoip2.__version__} "
359+
f"{aiohttp.http.SERVER_SOFTWARE}"
360+
)
350361
self._existing_session = aiohttp.ClientSession(
351362
auth=aiohttp.BasicAuth(self._account_id, self._license_key),
352-
headers={"Accept": "application/json", "User-Agent": _AIOHTTP_UA},
363+
headers={"Accept": "application/json", "User-Agent": user_agent},
353364
timeout=aiohttp.ClientTimeout(total=self._timeout),
354365
)
355366

@@ -456,7 +467,10 @@ def __init__( # pylint: disable=too-many-arguments,too-many-positional-argument
456467
self._session = requests.Session()
457468
self._session.auth = (self._account_id, self._license_key)
458469
self._session.headers["Accept"] = "application/json"
459-
self._session.headers["User-Agent"] = _REQUEST_UA
470+
self._session.headers["User-Agent"] = (
471+
f"GeoIP2-Python-Client/{geoip2.__version__}"
472+
f" {requests.utils.default_user_agent()}"
473+
)
460474
if proxy is None:
461475
self._proxies = None
462476
else:

pyproject.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ authors = [
1010
{name = "Gregory Oschwald", email = "[email protected]"},
1111
]
1212
dependencies = [
13-
"aiohttp>=3.6.2,<4.0.0",
1413
"maxminddb>=2.5.1,<3.0.0",
15-
"requests>=2.24.0,<3.0.0",
1614
]
1715
requires-python = ">=3.9"
1816
readme = "README.rst"
@@ -38,6 +36,12 @@ classifiers = [
3836
test = [
3937
"pytest-httpserver>=1.0.10",
4038
]
39+
aiohttp = [
40+
"aiohttp>=3.6.2,<4.0.0",
41+
]
42+
requests = [
43+
"requests>=2.24.0,<3.0.0",
44+
]
4145

4246
[tool.ruff.lint]
4347
select = ["ALL"]

setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ deps =
4545
types-requests
4646
voluptuous-stubs
4747
commands = mypy geoip2 tests
48+
extras =
49+
aiohttp
50+
requests

tests/webservice_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ class TestClient(TestBaseClient):
396396
client: Client
397397

398398
def setUp(self) -> None:
399+
pytest.importorskip("requests")
399400
self.client_class = Client
400401
self.client = Client(42, "abcdef123456")
401402
self.client._base_uri = self.httpserver.url_for("/geoip/v2.1")
@@ -409,6 +410,7 @@ class TestAsyncClient(TestBaseClient):
409410
client: AsyncClient
410411

411412
def setUp(self) -> None:
413+
pytest.importorskip("aiohttp")
412414
self._loop = asyncio.new_event_loop()
413415
self.client_class = AsyncClient
414416
self.client = AsyncClient(42, "abcdef123456")

0 commit comments

Comments
 (0)