Skip to content

Commit 657489d

Browse files
authored
Merge pull request #37 from ipinfo/silvano/eng-298-add-lite-api-support-to-ipinfodjango
Add support for Lite API
2 parents 302ebf8 + 8e3ecc6 commit 657489d

File tree

9 files changed

+184
-27
lines changed

9 files changed

+184
-27
lines changed

ipinfo_django/middleware.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ def __init__(self, get_response=None):
2020

2121
ipinfo_token = getattr(settings, "IPINFO_TOKEN", None)
2222
ipinfo_settings = getattr(settings, "IPINFO_SETTINGS", {})
23-
self.ip_selector = getattr(
24-
settings, "IPINFO_IP_SELECTOR", DefaultIPSelector()
25-
)
23+
self.ip_selector = getattr(settings, "IPINFO_IP_SELECTOR", DefaultIPSelector())
2624
self.ipinfo = ipinfo.getHandler(ipinfo_token, **ipinfo_settings)
2725

2826
def __call__(self, request):
@@ -34,7 +32,7 @@ def __call__(self, request):
3432
request.ipinfo = self.ipinfo.getDetails(
3533
self.ip_selector.get_ip(request)
3634
)
37-
except Exception as exc:
35+
except Exception:
3836
request.ipinfo = None
3937
LOGGER.error(traceback.format_exc())
4038

@@ -57,9 +55,7 @@ def __init__(self, get_response):
5755

5856
ipinfo_token = getattr(settings, "IPINFO_TOKEN", None)
5957
ipinfo_settings = getattr(settings, "IPINFO_SETTINGS", {})
60-
self.ip_selector = getattr(
61-
settings, "IPINFO_IP_SELECTOR", DefaultIPSelector()
62-
)
58+
self.ip_selector = getattr(settings, "IPINFO_IP_SELECTOR", DefaultIPSelector())
6359
self.ipinfo = ipinfo.getHandlerAsync(ipinfo_token, **ipinfo_settings)
6460

6561
def __call__(self, request):
@@ -83,3 +79,23 @@ async def __acall__(self, request):
8379

8480
def is_bot(self, request):
8581
return is_bot(request)
82+
83+
84+
class IPinfoLiteMiddleware(IPinfoMiddleware):
85+
def __init__(self, get_response):
86+
super().__init__(get_response=get_response)
87+
88+
ipinfo_token = getattr(settings, "IPINFO_TOKEN", None)
89+
ipinfo_settings = getattr(settings, "IPINFO_SETTINGS", {})
90+
self.ipinfo = ipinfo.getHandlerLite(ipinfo_token, **ipinfo_settings)
91+
92+
93+
class IPinfoAsyncLiteMiddleware(IPinfoAsyncMiddleware):
94+
sync_capable = False
95+
async_capable = True
96+
97+
def __init__(self, get_response):
98+
super().__init__(get_response=get_response)
99+
ipinfo_token = getattr(settings, "IPINFO_TOKEN", None)
100+
ipinfo_settings = getattr(settings, "IPINFO_SETTINGS", {})
101+
self.ipinfo = ipinfo.getHandlerAsyncLite(ipinfo_token, **ipinfo_settings)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
author_email="[email protected]",
1919
license="Apache License 2.0",
2020
packages=["ipinfo_django", "ipinfo_django.ip_selector"],
21-
install_requires=["django", "ipinfo==4.2.1"],
21+
install_requires=["django", "ipinfo>=5.2.0"],
2222
zip_safe=False,
2323
)

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def ipinfo_middleware(settings):
6+
settings.MIDDLEWARE = [
7+
"ipinfo_django.middleware.IPinfoMiddleware",
8+
]
9+
10+
11+
@pytest.fixture
12+
def ipinfo_async_middleware(settings):
13+
settings.MIDDLEWARE = [
14+
"ipinfo_django.middleware.IPinfoAsyncMiddleware",
15+
]
16+
17+
18+
@pytest.fixture
19+
def ipinfo_lite_middleware(settings):
20+
settings.MIDDLEWARE = [
21+
"ipinfo_django.middleware.IPinfoLiteMiddleware",
22+
]
23+
24+
25+
@pytest.fixture
26+
def ipinfo_async_lite_middleware(settings):
27+
settings.MIDDLEWARE = [
28+
"ipinfo_django.middleware.IPinfoAsyncLiteMiddleware",
29+
]

tests/settings.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@
33

44
ROOT_URLCONF = "tests.urls"
55

6-
MIDDLEWARE = [
7-
"ipinfo_django.middleware.IPinfoAsyncMiddleware",
8-
]
9-
106
USE_TZ = False
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from http import HTTPStatus
2+
from unittest import mock
3+
4+
import pytest
5+
from ipinfo.details import Details
6+
7+
8+
@pytest.mark.asyncio
9+
async def test_middleware_appends_ip_info(async_client, ipinfo_async_lite_middleware):
10+
with mock.patch("ipinfo.AsyncHandlerLite.getDetails") as mocked_getDetails:
11+
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
12+
res = await async_client.get("/test_view/")
13+
assert res.status_code == HTTPStatus.OK
14+
assert b"For testing: 127.0.0.1" in res.content
15+
16+
17+
@pytest.mark.asyncio
18+
async def test_middleware_filters(async_client, ipinfo_async_lite_middleware):
19+
res = await async_client.get("/test_view/", USER_AGENT="some bot")
20+
assert res.status_code == HTTPStatus.OK
21+
assert b"Request filtered." in res.content
22+
23+
24+
@pytest.mark.asyncio
25+
async def test_middleware_behind_proxy(async_client, ipinfo_async_lite_middleware):
26+
with mock.patch("ipinfo.AsyncHandlerLite.getDetails") as mocked_getDetails:
27+
mocked_getDetails.return_value = Details({"ip": "93.44.186.197"})
28+
res = await async_client.get("/test_view/", X_FORWARDED_FOR="93.44.186.197")
29+
30+
mocked_getDetails.assert_called_once_with("93.44.186.197")
31+
assert res.status_code == HTTPStatus.OK
32+
assert b"For testing: 93.44.186.197" in res.content
33+
34+
35+
@pytest.mark.asyncio
36+
async def test_middleware_not_behind_proxy(async_client, ipinfo_async_lite_middleware):
37+
with mock.patch("ipinfo.AsyncHandlerLite.getDetails") as mocked_getDetails:
38+
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
39+
res = await async_client.get("/test_view/")
40+
41+
mocked_getDetails.assert_called_once_with("127.0.0.1")
42+
assert res.status_code == HTTPStatus.OK
43+
assert b"For testing: 127.0.0.1" in res.content

tests/test_async_middleware.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
@pytest.mark.asyncio
9-
async def test_middleware_appends_ip_info(async_client):
9+
async def test_middleware_appends_ip_info(async_client, ipinfo_async_middleware):
1010
with mock.patch("ipinfo.AsyncHandler.getDetails") as mocked_getDetails:
1111
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
1212
res = await async_client.get("/test_view/")
@@ -15,27 +15,25 @@ async def test_middleware_appends_ip_info(async_client):
1515

1616

1717
@pytest.mark.asyncio
18-
async def test_middleware_filters(async_client):
18+
async def test_middleware_filters(async_client, ipinfo_async_middleware):
1919
res = await async_client.get("/test_view/", USER_AGENT="some bot")
2020
assert res.status_code == HTTPStatus.OK
2121
assert b"Request filtered." in res.content
2222

2323

2424
@pytest.mark.asyncio
25-
async def test_middleware_behind_proxy(async_client):
25+
async def test_middleware_behind_proxy(async_client, ipinfo_async_middleware):
2626
with mock.patch("ipinfo.AsyncHandler.getDetails") as mocked_getDetails:
2727
mocked_getDetails.return_value = Details({"ip": "93.44.186.197"})
28-
res = await async_client.get(
29-
"/test_view/", X_FORWARDED_FOR="93.44.186.197"
30-
)
28+
res = await async_client.get("/test_view/", X_FORWARDED_FOR="93.44.186.197")
3129

3230
mocked_getDetails.assert_called_once_with("93.44.186.197")
3331
assert res.status_code == HTTPStatus.OK
3432
assert b"For testing: 93.44.186.197" in res.content
3533

3634

3735
@pytest.mark.asyncio
38-
async def test_middleware_not_behind_proxy(async_client):
36+
async def test_middleware_not_behind_proxy(async_client, ipinfo_async_middleware):
3937
with mock.patch("ipinfo.AsyncHandler.getDetails") as mocked_getDetails:
4038
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
4139
res = await async_client.get("/test_view/")

tests/test_lite_middleware.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from http import HTTPStatus
2+
from unittest import mock
3+
4+
from ipinfo.details import Details
5+
6+
7+
def test_middleware_appends_ip_info(client, ipinfo_lite_middleware):
8+
with mock.patch("ipinfo.HandlerLite.getDetails") as mocked_getDetails:
9+
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
10+
res = client.get("/test_view/")
11+
assert res.status_code == HTTPStatus.OK
12+
assert b"For testing: 127.0.0.1" in res.content
13+
14+
15+
def test_middleware_filters(client, ipinfo_lite_middleware):
16+
res = client.get("/test_view/", HTTP_USER_AGENT="some bot")
17+
assert res.status_code == HTTPStatus.OK
18+
assert b"Request filtered." in res.content
19+
20+
21+
def test_middleware_behind_proxy(client, ipinfo_lite_middleware):
22+
with mock.patch("ipinfo.HandlerLite.getDetails") as mocked_getDetails:
23+
mocked_getDetails.return_value = Details({"ip": "93.44.186.197"})
24+
res = client.get("/test_view/", HTTP_X_FORWARDED_FOR="93.44.186.197")
25+
26+
mocked_getDetails.assert_called_once_with("93.44.186.197")
27+
assert res.status_code == HTTPStatus.OK
28+
assert b"For testing: 93.44.186.197" in res.content
29+
30+
31+
def test_middleware_not_behind_proxy(client, ipinfo_lite_middleware):
32+
with mock.patch("ipinfo.HandlerLite.getDetails") as mocked_getDetails:
33+
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
34+
res = client.get("/test_view/")
35+
36+
mocked_getDetails.assert_called_once_with("127.0.0.1")
37+
assert res.status_code == HTTPStatus.OK
38+
assert b"For testing: 127.0.0.1" in res.content

tests/test_middleware.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from http import HTTPStatus
2+
from unittest import mock
3+
4+
from ipinfo.details import Details
5+
6+
7+
def test_middleware_appends_ip_info(client, ipinfo_middleware):
8+
with mock.patch("ipinfo.Handler.getDetails") as mocked_getDetails:
9+
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
10+
res = client.get("/test_view/")
11+
assert res.status_code == HTTPStatus.OK
12+
assert b"For testing: 127.0.0.1" in res.content
13+
14+
15+
def test_middleware_filters(client, ipinfo_middleware):
16+
res = client.get("/test_view/", HTTP_USER_AGENT="some bot")
17+
assert res.status_code == HTTPStatus.OK
18+
assert b"Request filtered." in res.content
19+
20+
21+
def test_middleware_behind_proxy(client, ipinfo_middleware):
22+
with mock.patch("ipinfo.Handler.getDetails") as mocked_getDetails:
23+
mocked_getDetails.return_value = Details({"ip": "93.44.186.197"})
24+
res = client.get("/test_view/", HTTP_X_FORWARDED_FOR="93.44.186.197")
25+
26+
mocked_getDetails.assert_called_once_with("93.44.186.197")
27+
assert res.status_code == HTTPStatus.OK
28+
assert b"For testing: 93.44.186.197" in res.content
29+
30+
31+
def test_middleware_not_behind_proxy(client, ipinfo_middleware):
32+
with mock.patch("ipinfo.Handler.getDetails") as mocked_getDetails:
33+
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
34+
res = client.get("/test_view/")
35+
36+
mocked_getDetails.assert_called_once_with("127.0.0.1")
37+
assert res.status_code == HTTPStatus.OK
38+
assert b"For testing: 127.0.0.1" in res.content

tox.ini

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ envlist =
55
py3.10-django{40}
66

77
[testenv]
8-
commands = pytest {posargs}
9-
passenv = PYTHONPATH
8+
commands = pytest {posargs:tests/}
9+
setenv =
10+
DJANGO_SETTINGS_MODULE = tests.settings
11+
PYTHONPATH = {toxinidir}
12+
deps = requests
1013

1114
[testenv:py3.10-django32]
12-
commands = pytest tests/test_async_middleware.py
15+
commands = pytest tests
1316
deps = -rrequirements/py310-django32.txt
1417

1518
[testenv:py3.10-django40]
16-
commands = pytest tests/test_async_middleware.py
19+
commands = pytest tests
1720
deps = -rrequirements/py310-django40.txt
18-
19-
[pytest]
20-
DJANGO_SETTINGS_MODULE = tests.settings
21-
python_files = test_*.py

0 commit comments

Comments
 (0)