Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Retry flaky request when Smithsonian provider script detects no unit codes (#508)

Staci Cooper1ef4a1332899
diff --git a/openverse_catalog/dags/providers/provider_api_scripts/smithsonian.py b/openverse_catalog/dags/providers/provider_api_scripts/smithsonian.py
index c82cb8a6d..780b1c086 100644
--- a/openverse_catalog/dags/providers/provider_api_scripts/smithsonian.py
+++ b/openverse_catalog/dags/providers/provider_api_scripts/smithsonian.py
@@ -20,6 +20,7 @@ from common.licenses import LicenseInfo
from common.loader import provider_details as prov
from common.requester import DelayedRequester
from common.storage.image import ImageStore
+from retry import retry
logger = logging.getLogger(__name__)
@@ -202,16 +203,28 @@ def get_new_and_outdated_unit_codes(
return new_unit_codes, outdated_unit_codes
-def validate_unit_codes_from_api(units_endpoint: str = UNITS_ENDPOINT) -> None:
- """
- Validates the SMITHSONIAN_SUB_PROVIDERS dictionary, and raises an exception if
- human intervention is needed to add or remove unit codes.
- """
+@retry(ValueError, tries=3, delay=1, backoff=2)
+def get_unit_codes_from_api(
+ units_endpoint: str = UNITS_ENDPOINT, retries: int = 3
+) -> set:
query_params = {"api_key": API_KEY, "q": "online_media_type:Images"}
response_json = delayed_requester.get_response_json(
units_endpoint, query_params=query_params
)
unit_codes_from_api = set(response_json.get("response", {}).get("terms", []))
+
+ if len(unit_codes_from_api) == 0:
+ raise ValueError("No unit codes received.")
+
+ return unit_codes_from_api
+
+
+def validate_unit_codes_from_api(units_endpoint: str = UNITS_ENDPOINT) -> None:
+ """
+ Validates the SMITHSONIAN_SUB_PROVIDERS dictionary, and raises an exception if
+ human intervention is needed to add or remove unit codes.
+ """
+ unit_codes_from_api = get_unit_codes_from_api(units_endpoint)
new_unit_codes, outdated_unit_codes = get_new_and_outdated_unit_codes(
unit_codes_from_api
)
diff --git a/tests/dags/providers/provider_api_scripts/test_smithsonian.py b/tests/dags/providers/provider_api_scripts/test_smithsonian.py
index dd32610b0..bd5d07829 100644
--- a/tests/dags/providers/provider_api_scripts/test_smithsonian.py
+++ b/tests/dags/providers/provider_api_scripts/test_smithsonian.py
@@ -59,6 +59,17 @@ def test_alert_new_unit_codes():
)
+def test_get_unit_codes_from_api_retries():
+ mock_response = {"terms": []}
+ # Patch the sleep function so it doesn't take long
+ with patch.object(
+ si.delayed_requester, "get_response_json", return_value=mock_response
+ ) as request_patch, patch("time.sleep"):
+ with pytest.raises(ValueError, match="No unit codes received."):
+ si.get_unit_codes_from_api()
+ assert request_patch.call_count == 3
+
+
@pytest.mark.parametrize(
"new_unit_codes, outdated_unit_codes",
[
@@ -70,7 +81,7 @@ def test_alert_new_unit_codes():
def test_validate_unit_codes_from_api_raises_exception(
new_unit_codes, outdated_unit_codes
):
- with patch.object(si.delayed_requester, "get_response_json"), patch.object(
+ with patch.object(si, "get_unit_codes_from_api"), patch.object(
si,
"get_new_and_outdated_unit_codes",
return_value=(new_unit_codes, outdated_unit_codes),
@@ -83,7 +94,7 @@ def test_validate_unit_codes_from_api_raises_exception(
def test_validate_unit_codes_from_api():
- with patch.object(si.delayed_requester, "get_response_json"), patch.object(
+ with patch.object(si, "get_unit_codes_from_api"), patch.object(
si, "get_new_and_outdated_unit_codes", return_value=(set(), set())
):
# Validation should run without raising an exception