Skip to content

Commit e74c158

Browse files
authored
Merge pull request #112 from ipinfo/silvano/st-1613-ticket-no-268595-null-and-404-values-for-lite-api-response-w
Fix Lite API `Details` response object
2 parents bd3b0d3 + d5aa1cd commit e74c158

File tree

5 files changed

+46
-40
lines changed

5 files changed

+46
-40
lines changed

ipinfo/handler_utils.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Utilities used in handlers.
33
"""
44

5+
import copy
56
import json
67
import os
78
import sys
8-
import copy
99

1010
from .version import SDK_VERSION
1111

@@ -68,21 +68,26 @@ def format_details(
6868
"""
6969
Format details given a countries object.
7070
"""
71-
details["country_name"] = countries.get(details.get("country"))
72-
details["isEU"] = details.get("country") in eu_countries
73-
details["country_flag_url"] = (
74-
COUNTRY_FLAGS_URL + (details.get("country") or "") + ".svg"
75-
)
76-
details["country_flag"] = copy.deepcopy(
77-
countries_flags.get(details.get("country"))
78-
)
79-
details["country_currency"] = copy.deepcopy(
80-
countries_currencies.get(details.get("country"))
81-
)
82-
details["continent"] = copy.deepcopy(
83-
continents.get(details.get("country"))
84-
)
85-
details["latitude"], details["longitude"] = read_coords(details.get("loc"))
71+
country_code = ""
72+
# Core and Lite API return the country_code in differently named fields
73+
if "country_code" in details:
74+
country_code = details.get("country_code")
75+
elif "country" in details:
76+
country_code = details.get("country")
77+
78+
# country_code = details.get("country")
79+
if country_name := countries.get(country_code):
80+
details["country_name"] = country_name
81+
details["isEU"] = country_code in eu_countries
82+
details["country_flag_url"] = COUNTRY_FLAGS_URL + country_code + ".svg"
83+
if flag := countries_flags.get(country_code):
84+
details["country_flag"] = copy.deepcopy(flag)
85+
if currency := countries_currencies.get(country_code):
86+
details["country_currency"] = copy.deepcopy(currency)
87+
if continent := continents.get(country_code):
88+
details["continent"] = copy.deepcopy(continent)
89+
if location := details.get("loc"):
90+
details["latitude"], details["longitude"] = read_coords(location)
8691

8792

8893
def read_coords(location):

tests/handler_async_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ async def test_get_details():
8787
continent = details.continent
8888
assert continent["code"] == "NA"
8989
assert continent["name"] == "North America"
90-
assert details.loc == "38.0088,-122.1175"
91-
assert details.latitude == "38.0088"
92-
assert details.longitude == "-122.1175"
90+
assert details.loc is not None
91+
assert details.latitude is not None
92+
assert details.longitude is not None
9393
assert details.postal == "94043"
9494
assert details.timezone == "America/Los_Angeles"
9595
if token:

tests/handler_lite_async_test.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import json
22
import os
33

4+
import aiohttp
5+
import pytest
6+
7+
from ipinfo import handler_utils
48
from ipinfo.cache.default import DefaultCache
59
from ipinfo.details import Details
6-
from ipinfo import handler_utils
710
from ipinfo.error import APIError
8-
import pytest
9-
import aiohttp
10-
1111
from ipinfo.handler_lite_async import AsyncHandlerLite
1212

1313

@@ -73,17 +73,17 @@ async def test_get_details():
7373
assert details.country_code == "US"
7474
assert details.country == "United States"
7575
assert details.continent_code == "NA"
76-
assert details.continent is None
77-
assert details.country_name is None
76+
assert details.continent == {"code": "NA", "name": "North America"}
77+
assert details.country_name == "United States"
7878
assert not details.isEU
7979
assert (
8080
details.country_flag_url
81-
== "https://cdn.ipinfo.io/static/images/countries-flags/United States.svg"
81+
== "https://cdn.ipinfo.io/static/images/countries-flags/US.svg"
8282
)
83-
assert details.country_flag is None
84-
assert details.country_currency is None
85-
assert details.latitude is None
86-
assert details.longitude is None
83+
assert details.country_flag == {"emoji": "🇺🇸", "unicode": "U+1F1FA U+1F1F8"}
84+
assert details.country_currency == {"code": "USD", "symbol": "$"}
85+
assert not hasattr(details, "latitude")
86+
assert not hasattr(details, "longitude")
8787

8888
await handler.deinit()
8989

tests/handler_lite_test.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
import pytest
4+
45
from ipinfo import handler_utils
56
from ipinfo.cache.default import DefaultCache
67
from ipinfo.details import Details
@@ -42,17 +43,17 @@ def test_get_details():
4243
assert details.country_code == "US"
4344
assert details.country == "United States"
4445
assert details.continent_code == "NA"
45-
assert details.continent is None
46-
assert details.country_name is None
46+
assert details.continent == {"code": "NA", "name": "North America"}
47+
assert details.country_name == "United States"
4748
assert not details.isEU
4849
assert (
4950
details.country_flag_url
50-
== "https://cdn.ipinfo.io/static/images/countries-flags/United States.svg"
51+
== "https://cdn.ipinfo.io/static/images/countries-flags/US.svg"
5152
)
52-
assert details.country_flag is None
53-
assert details.country_currency is None
54-
assert details.latitude is None
55-
assert details.longitude is None
53+
assert details.country_flag == {"emoji": "🇺🇸", "unicode": "U+1F1FA U+1F1F8"}
54+
assert details.country_currency == {"code": "USD", "symbol": "$"}
55+
assert not hasattr(details, "latitude")
56+
assert not hasattr(details, "longitude")
5657

5758

5859
#############

tests/handler_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ def test_get_details():
5656
continent = details.continent
5757
assert continent["code"] == "NA"
5858
assert continent["name"] == "North America"
59-
assert details.loc == "38.0088,-122.1175"
60-
assert details.latitude == "38.0088"
61-
assert details.longitude == "-122.1175"
59+
assert details.loc is not None
60+
assert details.latitude is not None
61+
assert details.longitude is not None
6262
assert details.postal == "94043"
6363
assert details.timezone == "America/Los_Angeles"
6464
if token:

0 commit comments

Comments
 (0)