Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add additional_query_params to provider DAG configuration (#3648)
Victoria Af2e9a3107e6c
diff --git a/catalog/dags/providers/provider_api_scripts/provider_data_ingester.py b/catalog/dags/providers/provider_api_scripts/provider_data_ingester.pyindex 8c248d139..5c6c0032a 100644--- a/catalog/dags/providers/provider_api_scripts/provider_data_ingester.py+++ b/catalog/dags/providers/provider_api_scripts/provider_data_ingester.py@@ -173,6 +173,9 @@ class ProviderDataIngester(ABC):# Create a generator to facilitate fetching the next set of query_params.self.override_query_params = (qp for qp in query_params_list)+ # An optional set of query params to add to each query+ self.additional_query_params = conf.get("additional_query_params", {})+# A configuration option that is used to skip over ALL errors and continue# ingestion, reporting errors in aggregate when ingestion has completed. This# option should be used sparingly and can only be enabled at the level of an@@ -349,9 +352,10 @@ class ProviderDataIngester(ABC):)return next_params- # Default behavior when no conf options are provided; build the next- # set of query params, given the previous.- return self.get_next_query_params(prev_query_params, **kwargs)+ # Default behavior; build the next set of query params, given the previous,+ # along with any extra params if additional_query_params were provided+ next_query_params = self.get_next_query_params(prev_query_params, **kwargs)+ return next_query_params | self.additional_query_params@abstractmethoddef get_next_query_params(self, prev_query_params: dict | None, **kwargs) -> dict:diff --git a/catalog/dags/providers/provider_dag_factory.py b/catalog/dags/providers/provider_dag_factory.pyindex fd6e49814..c2767a665 100644--- a/catalog/dags/providers/provider_dag_factory.py+++ b/catalog/dags/providers/provider_dag_factory.py@@ -399,6 +399,15 @@ def create_provider_api_workflow_dag(conf: ProviderWorkflow):" be run for just these sets of params."),),+ "additional_query_params": Param(+ default={},+ type=["object", "null"],+ description=(+ "Supplement the `query_params` on each request. This option is used"+ " to run a DAG but restrict the search by specifying extra query"+ " params."+ ),+ ),"skip_ingestion_errors": Param(default=False,type="boolean",diff --git a/catalog/tests/dags/providers/provider_api_scripts/test_provider_data_ingester.py b/catalog/tests/dags/providers/provider_api_scripts/test_provider_data_ingester.pyindex 1afb08bf9..070726c42 100644--- a/catalog/tests/dags/providers/provider_api_scripts/test_provider_data_ingester.py+++ b/catalog/tests/dags/providers/provider_api_scripts/test_provider_data_ingester.py@@ -394,6 +394,37 @@ def test_ingest_records_uses_query_params_list_from_dagrun_conf():)+def test_ingest_records_uses_additional_query_params_from_dagrun_conf():+ # Initialize the ingester with a conf+ ingester = MockProviderDataIngester(+ {"additional_query_params": {"extra_param": 310}}+ )++ with (+ patch.object(ingester, "get_batch") as get_batch_mock,+ patch.object(ingester, "process_batch", return_value=3),+ ):+ get_batch_mock.side_effect = [+ (EXPECTED_BATCH_DATA, True),+ (EXPECTED_BATCH_DATA, True),+ (EXPECTED_BATCH_DATA, True),+ (EXPECTED_BATCH_DATA, False),+ ]++ ingester.ingest_records()++ # All calls should have the extra query param added+ assert get_batch_mock.call_count == 4+ get_batch_mock.assert_has_calls(+ [+ call({"has_image": 1, "page": 1, "extra_param": 310}),+ call({"has_image": 1, "page": 1, "extra_param": 310}),+ call({"has_image": 1, "page": 1, "extra_param": 310}),+ call({"has_image": 1, "page": 1, "extra_param": 310}),+ ]+ )++def test_ingest_records_raises_IngestionError():with patch.object(ingester, "get_batch") as get_batch_mock:get_batch_mock.side_effect = [