Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Make Phylopic a dated-only DAG (#944)
Madison Swain-Bowdene8bee4a16152
diff --git a/openverse_catalog/dags/providers/provider_api_scripts/phylopic.py b/openverse_catalog/dags/providers/provider_api_scripts/phylopic.pyindex e48887248..52803f0e7 100644--- a/openverse_catalog/dags/providers/provider_api_scripts/phylopic.py+++ b/openverse_catalog/dags/providers/provider_api_scripts/phylopic.py@@ -31,53 +31,34 @@ class PhylopicDataIngester(ProviderDataIngester):providers = {constants.IMAGE: prov.PHYLOPIC_DEFAULT_PROVIDER}batch_limit = 25- def __init__(self, *args, **kwargs):- super().__init__(*args, **kwargs)- # This is made an instance attribute rather than passed around via query params- # because the URL we hit includes it in the path (not the params) depending on- # whether we're running a dated DAG or not. This needs to start at 0 after- # getting called once, so we do not set it as a sentinel for the- # get_next_query_params function.- self.offset = None-@propertydef endpoint(self) -> str:"""- If this is being run as a dated DAG, **only one request is ever issued** to- retrieve all updated IDs. As such, the dated version will only return one- endpoint. The full run DAG does require the typical offset + limit, which gets- recomputed on each call to this property.+ Due to the way this DAG is run (via a dated range), **only one request is ever+ issued** to retrieve all updated IDs. As such, it will only+ return one endpoint."""list_endpoint = f"{self.base_endpoint}/list"- if self.date:- # Process for a given date- end_date = (date.fromisoformat(self.date) + timedelta(days=1)).isoformat()- # Get a list of objects uploaded/updated within a date range- # http://phylopic.org/api/#method-image-time-range- endpoint = f"{list_endpoint}/modified/{self.date}/{end_date}"- else:- # Get all images and limit the results for each request.- endpoint = f"{list_endpoint}/{self.offset}/{self.batch_limit}"+ # Process for a given date+ end_date = (date.fromisoformat(self.date) + timedelta(days=1)).isoformat()+ # Get a list of objects uploaded/updated within a date range+ # http://phylopic.org/api/#method-image-time-range+ endpoint = f"{list_endpoint}/modified/{self.date}/{end_date}"logger.info(f"Constructed endpoint: {endpoint}")return endpointdef get_next_query_params(self, prev_query_params: dict | None, **kwargs) -> dict:"""- Since the query range is determined via endpoint, this only increments the range- to query.+ Since the range is determined by the endpoint property, this step is a noop."""- if self.offset is None:- self.offset = 0- else:- self.offset += self.batch_limitreturn {}def get_should_continue(self, response_json):"""- Override for upstream "return True". Dated runs will only ever make 1 query so+ Override for upstream "return True". This DAG will only ever make 1 query sothey should not continue to loop."""- return not bool(self.date)+ return False@staticmethoddef _get_response_data(response_json) -> dict | list | None:diff --git a/tests/dags/providers/provider_api_scripts/test_phylopic.py b/tests/dags/providers/provider_api_scripts/test_phylopic.pyindex 07c7aba65..833480de4 100644--- a/tests/dags/providers/provider_api_scripts/test_phylopic.py+++ b/tests/dags/providers/provider_api_scripts/test_phylopic.py@@ -24,68 +24,17 @@ def get_json(filename):@pytest.fixturedef ingester() -> PhylopicDataIngester:_pp = PhylopicDataIngester()- offset = _pp.offset- batch_limit = _pp.batch_limityield _pp- _pp.offset = offset- _pp.batch_limit = batch_limit- "offset, limit, non_dated_endpoint",- [- # Basic limit/offset changes- (0, 25, "http://phylopic.org/api/a/image/list/0/25"),- (0, 30, "http://phylopic.org/api/a/image/list/0/30"),- (60, 30, "http://phylopic.org/api/a/image/list/60/30"),- ],-)- "date, dated_endpoint",- [- # Defer to non-dated case- (None, None),- # Dated DAG produces specific endpoint irrespective of everything else- (- "2022-01-01",- "http://phylopic.org/api/a/image/list/modified/2022-01-01/2022-01-02",- ),- ],-)-def test_endpoint(- offset,- limit,- non_dated_endpoint,- date,- dated_endpoint,- ingester,-):- # Defer to dated endpoint if provided- expected_endpoint = dated_endpoint or non_dated_endpoint- ingester.date = date- ingester.offset = offset- ingester.batch_limit = limit+def test_endpoint(ingester):+ expected_endpoint = (+ "http://phylopic.org/api/a/image/list/modified/2022-01-01/2022-01-02"+ )+ ingester.date = "2022-01-01"assert ingester.endpoint == expected_endpoint-def test_get_next_query_params(ingester):- # Initial call sets offset to 0- ingester.batch_limit = 50- # Offset after the first call should be 0, then increment by batch limit- # each other iteration- for expected_offset in [0, 50, 100]:- ingester.get_next_query_params({})- assert ingester.offset == expected_offset--[email protected]("dated", [True, False])-def test_get_should_continue(dated, ingester):- ingester.date = dated- actual = ingester.get_should_continue({})- # Should continue only if we're running as non-dated- assert actual == (not dated)--@pytest.mark.parametrize("response_json, expected",[