From 36a1659b82b9f59ffc990dd6fd6b90c5eca4e484 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Wed, 10 Sep 2025 16:31:14 +0200 Subject: [PATCH 1/2] Fix Lite API details fields not being set correctly --- ipinfo/handler_utils.py | 37 ++++++++++++++++++-------------- tests/handler_lite_async_test.py | 22 +++++++++---------- tests/handler_lite_test.py | 15 +++++++------ 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/ipinfo/handler_utils.py b/ipinfo/handler_utils.py index bb6ce2b..971003a 100644 --- a/ipinfo/handler_utils.py +++ b/ipinfo/handler_utils.py @@ -2,10 +2,10 @@ Utilities used in handlers. """ +import copy import json import os import sys -import copy from .version import SDK_VERSION @@ -68,21 +68,26 @@ def format_details( """ Format details given a countries object. """ - details["country_name"] = countries.get(details.get("country")) - details["isEU"] = details.get("country") in eu_countries - details["country_flag_url"] = ( - COUNTRY_FLAGS_URL + (details.get("country") or "") + ".svg" - ) - details["country_flag"] = copy.deepcopy( - countries_flags.get(details.get("country")) - ) - details["country_currency"] = copy.deepcopy( - countries_currencies.get(details.get("country")) - ) - details["continent"] = copy.deepcopy( - continents.get(details.get("country")) - ) - details["latitude"], details["longitude"] = read_coords(details.get("loc")) + country_code = "" + # Core and Lite API return the country_code in differently named fields + if "country_code" in details: + country_code = details.get("country_code") + elif "country" in details: + country_code = details.get("country") + + # country_code = details.get("country") + if country_name := countries.get(country_code): + details["country_name"] = country_name + details["isEU"] = country_code in eu_countries + details["country_flag_url"] = COUNTRY_FLAGS_URL + country_code + ".svg" + if flag := countries_flags.get(country_code): + details["country_flag"] = copy.deepcopy(flag) + if currency := countries_currencies.get(country_code): + details["country_currency"] = copy.deepcopy(currency) + if continent := continents.get(country_code): + details["continent"] = copy.deepcopy(continent) + if location := details.get("loc"): + details["latitude"], details["longitude"] = read_coords(location) def read_coords(location): diff --git a/tests/handler_lite_async_test.py b/tests/handler_lite_async_test.py index 45d90c1..5a5cc25 100644 --- a/tests/handler_lite_async_test.py +++ b/tests/handler_lite_async_test.py @@ -1,13 +1,13 @@ import json import os +import aiohttp +import pytest + +from ipinfo import handler_utils from ipinfo.cache.default import DefaultCache from ipinfo.details import Details -from ipinfo import handler_utils from ipinfo.error import APIError -import pytest -import aiohttp - from ipinfo.handler_lite_async import AsyncHandlerLite @@ -73,17 +73,17 @@ async def test_get_details(): assert details.country_code == "US" assert details.country == "United States" assert details.continent_code == "NA" - assert details.continent is None - assert details.country_name is None + assert details.continent == {"code": "NA", "name": "North America"} + assert details.country_name == "United States" assert not details.isEU assert ( details.country_flag_url - == "https://cdn.ipinfo.io/static/images/countries-flags/United States.svg" + == "https://cdn.ipinfo.io/static/images/countries-flags/US.svg" ) - assert details.country_flag is None - assert details.country_currency is None - assert details.latitude is None - assert details.longitude is None + assert details.country_flag == {"emoji": "🇺🇸", "unicode": "U+1F1FA U+1F1F8"} + assert details.country_currency == {"code": "USD", "symbol": "$"} + assert not hasattr(details, "latitude") + assert not hasattr(details, "longitude") await handler.deinit() diff --git a/tests/handler_lite_test.py b/tests/handler_lite_test.py index 36873b2..baa4c63 100644 --- a/tests/handler_lite_test.py +++ b/tests/handler_lite_test.py @@ -1,6 +1,7 @@ import os import pytest + from ipinfo import handler_utils from ipinfo.cache.default import DefaultCache from ipinfo.details import Details @@ -42,17 +43,17 @@ def test_get_details(): assert details.country_code == "US" assert details.country == "United States" assert details.continent_code == "NA" - assert details.continent is None - assert details.country_name is None + assert details.continent == {"code": "NA", "name": "North America"} + assert details.country_name == "United States" assert not details.isEU assert ( details.country_flag_url - == "https://cdn.ipinfo.io/static/images/countries-flags/United States.svg" + == "https://cdn.ipinfo.io/static/images/countries-flags/US.svg" ) - assert details.country_flag is None - assert details.country_currency is None - assert details.latitude is None - assert details.longitude is None + assert details.country_flag == {"emoji": "🇺🇸", "unicode": "U+1F1FA U+1F1F8"} + assert details.country_currency == {"code": "USD", "symbol": "$"} + assert not hasattr(details, "latitude") + assert not hasattr(details, "longitude") ############# From d5aa1cdbe54a7cf2d7fe3de1212b55bf50eb18a1 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Wed, 10 Sep 2025 16:37:51 +0200 Subject: [PATCH 2/2] Fix location tests so they fail less often --- tests/handler_async_test.py | 6 +++--- tests/handler_test.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/handler_async_test.py b/tests/handler_async_test.py index e51bb4e..6cc1011 100644 --- a/tests/handler_async_test.py +++ b/tests/handler_async_test.py @@ -87,9 +87,9 @@ async def test_get_details(): continent = details.continent assert continent["code"] == "NA" assert continent["name"] == "North America" - assert details.loc == "38.0088,-122.1175" - assert details.latitude == "38.0088" - assert details.longitude == "-122.1175" + assert details.loc is not None + assert details.latitude is not None + assert details.longitude is not None assert details.postal == "94043" assert details.timezone == "America/Los_Angeles" if token: diff --git a/tests/handler_test.py b/tests/handler_test.py index 2523127..329753d 100644 --- a/tests/handler_test.py +++ b/tests/handler_test.py @@ -56,9 +56,9 @@ def test_get_details(): continent = details.continent assert continent["code"] == "NA" assert continent["name"] == "North America" - assert details.loc == "38.0088,-122.1175" - assert details.latitude == "38.0088" - assert details.longitude == "-122.1175" + assert details.loc is not None + assert details.latitude is not None + assert details.longitude is not None assert details.postal == "94043" assert details.timezone == "America/Los_Angeles" if token: