Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make aiohttp and requests optional dependencies #177

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ To install the ``geoip2`` module, type:
.. code-block:: bash
$ pip install geoip2
$ pip install geoip2[aiohttp] # Install aiohttp as well
$ pip install geoip2[requests] # Install requests as well
If you are not able to use pip, you may also use easy_install from the
source directory:
Expand Down
42 changes: 28 additions & 14 deletions geoip2/webservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,24 @@

"""

from __future__ import annotations

import ipaddress
import json
from typing import Any, Dict, cast, List, Optional, Type, Union

import aiohttp
import aiohttp.http
import requests
import requests.utils
try:
import aiohttp
import aiohttp.http
except ImportError:
aiohttp = None # type: ignore[assignment]

try:
import requests
import requests.utils
except ImportError:
requests = None # type: ignore[assignment]


import geoip2
import geoip2.models
Expand All @@ -48,14 +58,6 @@
from geoip2.models import City, Country, Insights
from geoip2.types import IPAddress

_AIOHTTP_UA = (
f"GeoIP2-Python-Client/{geoip2.__version__} {aiohttp.http.SERVER_SOFTWARE}"
)

_REQUEST_UA = (
f"GeoIP2-Python-Client/{geoip2.__version__} {requests.utils.default_user_agent()}"
)


class BaseClient: # pylint: disable=missing-class-docstring, too-few-public-methods
_account_id: str
Expand Down Expand Up @@ -326,10 +328,19 @@ async def insights(self, ip_address: IPAddress = "me") -> Insights:
)

async def _session(self) -> aiohttp.ClientSession:
if aiohttp is None:
raise ImportError(
"aiohttp is required for async mode; install `GeoIP2[aiohttp]`"
)

if not hasattr(self, "_existing_session"):
user_agent = (
f"GeoIP2-Python-Client/{geoip2.__version__} "
f"{aiohttp.http.SERVER_SOFTWARE}"
)
self._existing_session = aiohttp.ClientSession(
auth=aiohttp.BasicAuth(self._account_id, self._license_key),
headers={"Accept": "application/json", "User-Agent": _AIOHTTP_UA},
headers={"Accept": "application/json", "User-Agent": user_agent},
timeout=aiohttp.ClientTimeout(total=self._timeout),
)

Expand Down Expand Up @@ -436,7 +447,10 @@ def __init__( # pylint: disable=too-many-arguments
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
self._session.headers["User-Agent"] = (
f"GeoIP2-Python-Client/{geoip2.__version__}"
f" {requests.utils.default_user_agent()}"
)
if proxy is None:
self._proxies = None
else:
Expand Down
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ authors = [
{name = "Gregory Oschwald", email = "[email protected]"},
]
dependencies = [
"aiohttp>=3.6.2,<4.0.0",
"maxminddb>=2.5.1,<3.0.0",
"requests>=2.24.0,<3.0.0",
]
requires-python = ">=3.8"
readme = "README.rst"
Expand All @@ -38,6 +36,12 @@ classifiers = [
test = [
"pytest-httpserver>=1.0.10",
]
aiohttp = [
"aiohttp>=3.6.2,<4.0.0",
]
requests = [
"requests>=2.24.0,<3.0.0",
]

[tool.setuptools.package-data]
geoip2 = ["py.typed"]
Expand Down
11 changes: 9 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ geoip2 = py.typed
disable = duplicate-code

[tox:tox]
envlist = {py38,py39,py310,py311,py312}-test,py312-{black,lint,flake8,mypy}
envlist =
{py38,py39,py310,py311,py312}-test{,-all}
py312-{black,lint,flake8,mypy}

[gh-actions]
python =
Expand All @@ -19,11 +21,13 @@ python =
3.11: py311
3.12: py312,black,lint,flake8,mypy

[testenv:{py38,py39,py310,py311,py312}-test]
[testenv:{py38,py39,py310,py311,py312}-test{,-all}]
deps =
pytest-httpserver
pytest
commands = pytest tests
extras =
all: aiohttp, requests

[testenv:py312-black]
deps = black
Expand All @@ -45,3 +49,6 @@ deps =
types-requests
voluptuous-stubs
commands = mypy geoip2 tests
extras =
aiohttp
requests
3 changes: 0 additions & 3 deletions tests/database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
from __future__ import unicode_literals

import ipaddress
import sys
import unittest

sys.path.append("..")

import geoip2.database
import geoip2.errors
import maxminddb
Expand Down
2 changes: 0 additions & 2 deletions tests/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from typing import Dict
import unittest

sys.path.append("..")

import geoip2.models


Expand Down
5 changes: 2 additions & 3 deletions tests/webservice_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import asyncio
import copy
import ipaddress
import json
import sys
from typing import cast, Dict
import unittest
from pytest_httpserver import HeaderValueMatcher
Expand All @@ -14,7 +12,6 @@
from collections import defaultdict


sys.path.append("..")
import geoip2
from geoip2.errors import (
AddressNotFoundError,
Expand Down Expand Up @@ -358,6 +355,7 @@ def test_missing_constructor_args(self):

class TestClient(TestBaseClient):
def setUp(self):
pytest.importorskip("requests")
self.client_class = Client
self.client = Client(42, "abcdef123456")
self.client._base_uri = self.httpserver.url_for("/geoip/v2.1")
Expand All @@ -368,6 +366,7 @@ def run_client(self, v):

class TestAsyncClient(TestBaseClient):
def setUp(self):
pytest.importorskip("aiohttp")
self._loop = asyncio.new_event_loop()
self.client_class = AsyncClient
self.client = AsyncClient(42, "abcdef123456")
Expand Down
Loading