Skip to content

Commit fc21c4e

Browse files
committed
Fix Lite API details flag URL and avoid setting unknown values to None
1 parent bd3b0d3 commit fc21c4e

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

ipinfo/handler_utils.py

Lines changed: 13 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,18 @@ 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 = details.get("country")
72+
if country_name := countries.get(country):
73+
details["country_name"] = country_name
74+
details["isEU"] = country in eu_countries
75+
details["country_flag_url"] = COUNTRY_FLAGS_URL + (country or "") + ".svg"
76+
details["country_flag"] = copy.deepcopy(countries_flags.get(details.get("country")))
77+
if currency := details.get("country"):
78+
details["country_currency"] = copy.deepcopy(countries_currencies.get(currency))
79+
if continent := continents.get(country):
80+
details["continent"] = copy.deepcopy(continent)
81+
if location := details.get("loc"):
82+
details["latitude"], details["longitude"] = read_coords(location)
8683

8784

8885
def read_coords(location):

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 not hasattr(details, "continent")
47+
assert not hasattr(details, "country_name")
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 not hasattr(details, "country_flag")
54+
assert not hasattr(details, "country_currency")
55+
assert not hasattr(details, "latitude")
56+
assert not hasattr(details, "longitude")
5657

5758

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

0 commit comments

Comments
 (0)