Skip to content

Commit 370b349

Browse files
authored
Merge pull request #62 from ipinfo/umar/isEU
refactored isEU logic
2 parents 1710996 + c379c54 commit 370b349

File tree

10 files changed

+89
-305
lines changed

10 files changed

+89
-305
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Request behavior can be modified by setting the `request_options` keyword argume
232232

233233
### Internationalization
234234

235-
When looking up an IP address, the response object includes a `details.country_name` and `details.isEU` attributes which includes the country name and true if the country is a member of EU, based on American English. It is possible to return the country name in other languages by setting the `countries_file` keyword argument when creating the `IPinfo` object.
235+
When looking up an IP address, the response object includes a `details.country_name` and `details.isEU` attributes which includes the country based on American English. It is possible to return the country name in other languages by setting the `countries_file` and remove or add EU countries by setting `eu_countries_file` keyword argument when creating the `IPinfo` object.
236236

237237
The file must be a `.json` file with the following structure:
238238

ipinfo/countries.json

Lines changed: 1 addition & 252 deletions
Large diffs are not rendered by default.

ipinfo/eu.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["IE","AT","LT","LU","LV","DE","DK","SE","SI","SK","CZ","CY","NL","FI","FR","MT","ES","IT","EE","PL","PT","HU","HR","GR","RO","BG","BE"]

ipinfo/handler.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .handler_utils import (
1717
API_URL,
1818
COUNTRY_FILE_DEFAULT,
19+
COUNTRY_EU_FILE_DEFAULT,
1920
BATCH_MAX_SIZE,
2021
CACHE_MAXSIZE,
2122
CACHE_TTL,
@@ -42,6 +43,15 @@ def __init__(self, access_token=None, **kwargs):
4243
# load countries file
4344
self.countries = handler_utils.read_country_names(
4445
kwargs.get("countries_file")
46+
if kwargs.get("countries_file")
47+
else COUNTRY_FILE_DEFAULT
48+
)
49+
50+
# load eu countries file
51+
self.eu_countries = handler_utils.read_country_names(
52+
kwargs.get("eu_countries_file")
53+
if kwargs.get("eu_countries_file")
54+
else COUNTRY_EU_FILE_DEFAULT
4555
)
4656

4757
# setup req opts
@@ -99,7 +109,9 @@ def getDetails(self, ip_address=None, timeout=None):
99109
details = response.json()
100110

101111
# format & cache
102-
handler_utils.format_details(details, self.countries)
112+
handler_utils.format_details(
113+
details, self.countries, self.eu_countries
114+
)
103115
self.cache[cache_key(ip_address)] = details
104116

105117
return Details(details)
@@ -215,7 +227,9 @@ def getBatchDetails(
215227
# format all
216228
for detail in result.values():
217229
if isinstance(detail, dict):
218-
handler_utils.format_details(detail, self.countries)
230+
handler_utils.format_details(
231+
detail, self.countries, self.eu_countries
232+
)
219233

220234
return result
221235

ipinfo/handler_async.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .exceptions import RequestQuotaExceededError, TimeoutExceededError
1717
from .handler_utils import (
1818
API_URL,
19+
COUNTRY_EU_FILE_DEFAULT,
1920
COUNTRY_FILE_DEFAULT,
2021
BATCH_MAX_SIZE,
2122
CACHE_MAXSIZE,
@@ -43,6 +44,15 @@ def __init__(self, access_token=None, **kwargs):
4344
# load countries file
4445
self.countries = handler_utils.read_country_names(
4546
kwargs.get("countries_file")
47+
if kwargs.get("countries_file")
48+
else COUNTRY_FILE_DEFAULT
49+
)
50+
51+
# load eu countries file
52+
self.eu_countries = handler_utils.read_country_names(
53+
kwargs.get("eu_countries_file")
54+
if kwargs.get("eu_countries_file")
55+
else COUNTRY_EU_FILE_DEFAULT
4656
)
4757

4858
# setup req opts
@@ -122,7 +132,9 @@ async def getDetails(self, ip_address=None, timeout=None):
122132
details = await resp.json()
123133

124134
# format & cache
125-
handler_utils.format_details(details, self.countries)
135+
handler_utils.format_details(
136+
details, self.countries, self.eu_countries
137+
)
126138
self.cache[cache_key(ip_address)] = details
127139

128140
return Details(details)
@@ -272,7 +284,9 @@ async def _do_batch_req(
272284
# format & fill up cache
273285
for ip_address, details in json_resp.items():
274286
if isinstance(details, dict):
275-
handler_utils.format_details(details, self.countries)
287+
handler_utils.format_details(
288+
details, self.countries, self.eu_countries
289+
)
276290
self.cache[cache_key(ip_address)] = details
277291

278292
# merge cached results with new lookup

ipinfo/handler_utils.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# expanded country name, e.g. "PK" -> "Pakistan".
1616
COUNTRY_FILE_DEFAULT = "countries.json"
1717

18+
COUNTRY_EU_FILE_DEFAULT = "eu.json"
19+
1820
# The max amount of IPs allowed by the API per batch request.
1921
BATCH_MAX_SIZE = 1000
2022

@@ -50,15 +52,14 @@ def get_headers(access_token):
5052
return headers
5153

5254

53-
def format_details(details, countries):
55+
def format_details(details, countries, eu_countries):
5456
"""
5557
Format details given a countries object.
5658
5759
The countries object can be retrieved from read_country_names.
5860
"""
59-
json_details = countries.get(details.get("country"))
60-
details["country_name"] = json_details["name"] if json_details else None
61-
details["isEU"] = json_details["isEU"] if json_details else False
61+
details["country_name"] = countries.get(details.get("country"))
62+
details["isEU"] = details.get("country") in eu_countries
6263
details["latitude"], details["longitude"] = read_coords(details.get("loc"))
6364

6465

@@ -76,15 +77,12 @@ def read_coords(location):
7677
return lat, lon
7778

7879

79-
def read_country_names(countries_file=None):
80+
def read_country_names(countries_file):
8081
"""
8182
Read list of countries from specified country file or
8283
default file.
8384
"""
84-
if not countries_file:
85-
countries_file = os.path.join(
86-
os.path.dirname(__file__), COUNTRY_FILE_DEFAULT
87-
)
85+
countries_file = os.path.join(os.path.dirname(__file__), countries_file)
8886
with open(countries_file) as f:
8987
countries_json = f.read()
9088

requirements.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cachetools==4.2.0
44
aiohttp>=3.0.0,<=4
55

66
# dev
7-
pytest==6.2.1
8-
pytest-asyncio==0.14.0
9-
pip-tools==5.4.0
10-
black==20.8b1
7+
pytest==7.1.2
8+
pytest-asyncio==0.19.0
9+
pip-tools==6.8.0
10+
black==22.6.0

requirements.txt

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,89 @@
11
#
2-
# This file is autogenerated by pip-compile with python 3.9
2+
# This file is autogenerated by pip-compile with python 3.10
33
# To update, run:
44
#
55
# pip-compile --no-emit-index-url --no-emit-trusted-host
66
#
7-
aiohttp==3.7.4.post0
7+
aiohttp==3.8.1
88
# via -r requirements.in
9-
appdirs==1.4.4
10-
# via black
11-
async-timeout==3.0.1
9+
aiosignal==1.2.0
10+
# via aiohttp
11+
async-timeout==4.0.2
1212
# via aiohttp
13-
attrs==21.2.0
13+
attrs==22.1.0
1414
# via
1515
# aiohttp
1616
# pytest
17-
black==20.8b1
17+
black==22.6.0
1818
# via -r requirements.in
19+
build==0.8.0
20+
# via pip-tools
1921
cachetools==4.2.0
2022
# via -r requirements.in
21-
certifi==2021.5.30
23+
certifi==2022.6.15
2224
# via requests
23-
chardet==4.0.0
25+
charset-normalizer==2.1.1
2426
# via
2527
# aiohttp
2628
# requests
27-
click==8.0.1
29+
click==8.1.3
2830
# via
2931
# black
3032
# pip-tools
31-
idna==2.10
33+
frozenlist==1.3.1
34+
# via
35+
# aiohttp
36+
# aiosignal
37+
idna==3.3
3238
# via
3339
# requests
3440
# yarl
3541
iniconfig==1.1.1
3642
# via pytest
37-
multidict==5.1.0
43+
multidict==6.0.2
3844
# via
3945
# aiohttp
4046
# yarl
4147
mypy-extensions==0.4.3
4248
# via black
43-
packaging==21.0
44-
# via pytest
45-
pathspec==0.8.1
49+
packaging==21.3
50+
# via
51+
# build
52+
# pytest
53+
pathspec==0.9.0
4654
# via black
47-
pip-tools==5.4.0
55+
pep517==0.13.0
56+
# via build
57+
pip-tools==6.8.0
4858
# via -r requirements.in
49-
pluggy==0.13.1
59+
platformdirs==2.5.2
60+
# via black
61+
pluggy==1.0.0
5062
# via pytest
51-
py==1.10.0
63+
py==1.11.0
5264
# via pytest
53-
pyparsing==2.4.7
65+
pyparsing==3.0.9
5466
# via packaging
55-
pytest==6.2.1
67+
pytest==7.1.2
5668
# via
5769
# -r requirements.in
5870
# pytest-asyncio
59-
pytest-asyncio==0.14.0
71+
pytest-asyncio==0.19.0
6072
# via -r requirements.in
61-
regex==2021.7.6
62-
# via black
63-
requests==2.25.1
73+
requests==2.28.1
6474
# via -r requirements.in
65-
six==1.16.0
66-
# via pip-tools
67-
toml==0.10.2
75+
tomli==2.0.1
6876
# via
6977
# black
78+
# build
7079
# pytest
71-
typed-ast==1.4.3
72-
# via black
73-
typing-extensions==3.10.0.0
74-
# via
75-
# aiohttp
76-
# black
77-
urllib3==1.26.6
80+
urllib3==1.26.11
7881
# via requests
79-
yarl==1.6.3
82+
wheel==0.37.1
83+
# via pip-tools
84+
yarl==1.8.1
8085
# via aiohttp
8186

8287
# The following packages are considered to be unsafe in a requirements file:
8388
# pip
89+
# setuptools

tests/handler_async_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ async def test_get_details():
4242
assert details.region == "California"
4343
assert details.country == "US"
4444
assert details.country_name == "United States"
45+
assert details.isEU == False
4546
assert details.loc == "37.4056,-122.0775"
4647
assert details.latitude == "37.4056"
4748
assert details.longitude == "-122.0775"

tests/handler_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_get_details():
3939
assert details.region == "California"
4040
assert details.country == "US"
4141
assert details.country_name == "United States"
42+
assert details.isEU == False
4243
assert details.loc == "37.4056,-122.0775"
4344
assert details.latitude == "37.4056"
4445
assert details.longitude == "-122.0775"

0 commit comments

Comments
 (0)