Skip to content

Commit a3152e9

Browse files
author
Dishy.Dev
committed
Removing unused urllib3 dependency, loosening requests version requirement and making aiohttp and requests optional installs
1 parent 91440cd commit a3152e9

File tree

6 files changed

+46
-15
lines changed

6 files changed

+46
-15
lines changed

.github/workflows/lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
pip install --upgrade pylint black mypy voluptuous-stubs
2828
2929
- name: Install
30-
run: python setup.py install
30+
run: pip install -e .[all]
3131

3232
- name: Run mypy
3333
run: mypy geoip2 tests

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ To install the ``geoip2`` module, type:
1919
.. code-block:: bash
2020
2121
$ pip install geoip2
22+
$ pip install geoip2[aiohttp]
23+
$ pip install geoip2[requests]
24+
$ pip install geoip2[all] # Install both requests and aiohttp support
2225
2326
If you are not able to use pip, you may also use easy_install from the
2427
source directory:

geoip2/webservice.py

+35-11
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@
2727

2828
import ipaddress
2929
import json
30+
import sys
3031
from typing import Any, Dict, cast, List, Optional, Type, Union
3132

32-
import aiohttp
33-
import aiohttp.http
34-
import requests
35-
import requests.utils
33+
try:
34+
import aiohttp
35+
import aiohttp.http
36+
except ImportError:
37+
pass
38+
39+
try:
40+
import requests
41+
import requests.utils
42+
except ImportError:
43+
pass
44+
3645

3746
import geoip2
3847
import geoip2.models
@@ -48,13 +57,28 @@
4857
from geoip2.models import City, Country, Insights
4958
from geoip2.types import IPAddress
5059

51-
_AIOHTTP_UA = (
52-
f"GeoIP2-Python-Client/{geoip2.__version__} {aiohttp.http.SERVER_SOFTWARE}"
53-
)
54-
55-
_REQUEST_UA = (
56-
f"GeoIP2-Python-Client/{geoip2.__version__} {requests.utils.default_user_agent()}"
57-
)
60+
# If neither requests or aiohttp is installed then inform user how to
61+
# install them
62+
if "aiohttp" not in sys.modules and "requests" not in sys.modules:
63+
raise ImportError(
64+
"""To enable geoip2.webservice,
65+
install aiohttp or requests support.
66+
pip install geoip2[aiohttp]
67+
pip install geoip2[requests]
68+
pip install geoip2[all]"""
69+
)
70+
71+
72+
if "aiohttp" in sys.modules:
73+
_AIOHTTP_UA = (
74+
f"GeoIP2-Python-Client/{geoip2.__version__} {aiohttp.http.SERVER_SOFTWARE}"
75+
)
76+
77+
if "requests" in sys.modules:
78+
_REQUEST_UA = (
79+
f"GeoIP2-Python-Client/{geoip2.__version__}"
80+
f" {requests.utils.default_user_agent()}"
81+
)
5882

5983

6084
class BaseClient: # pylint: disable=missing-class-docstring, too-few-public-methods

requirements.txt

-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
aiohttp>=3.6.2,<4.0.0
21
maxminddb>=2.0.0,<3.0.0
3-
requests>=2.24.0,<3.0.0
4-
urllib3>=1.25.2,<2.0.0

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ deps =
2323
pytest
2424
mocket
2525
commands = pytest tests
26+
usedevelop = true
27+
extras = aiohttp, requests

setup.py

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
include_package_data=True,
2727
python_requires=">=3.6",
2828
install_requires=requirements,
29+
extras_require={
30+
"all": ["requests>=2.14.0,<3.0.0", "aiohttp>=3.6.2,<4.0.0"],
31+
"requests": "requests>=2.14.0,<3.0.0",
32+
"aiohttp": "aiohttp>=3.6.2,<4.0.0",
33+
},
2934
tests_require=["mocket>=3.8.9"],
3035
test_suite="tests",
3136
license=geoip2.__license__,

0 commit comments

Comments
 (0)