Skip to content

Commit 3458a87

Browse files
committed
more resilient _DerivedFrom._from_url` eu-cdse/openeo-cdse-infra#1338
1 parent cc0ed51 commit 3458a87

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Changed
1313

1414
- Convert setup.py to pyproject.toml ([#920](https://github.com/Open-EO/openeo-python-client/issues/920))
15+
- Make `_DerivedFrom._from_url` more resilient against unresolvable/unparsable `derived_from` links ([#928](https://github.com/Open-EO/openeo-python-client/issues/928), eu-cdse/openeo-cdse-infra#1338)
1516

1617
### Removed
1718

‎openeo/_version.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.53.0a1"
1+
__version__ = "0.53.0a2"

‎openeo/testing/results.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,15 @@ def _from_href(self, href: str, base: Union[str, Path, None] = None) -> Iterable
540540
return [_ProductRef(item_id=self._normalize_legacy_derived_from(href))]
541541

542542
def _from_url(self, url: str) -> Iterable[_ProductRef]:
543-
resp = self._request_session.get(url)
544-
resp.raise_for_status()
545-
doc = resp.json()
546-
return self._from_json_document(doc=doc, doc_ref=url)
543+
try:
544+
resp = self._request_session.get(url)
545+
resp.raise_for_status()
546+
doc = resp.json()
547+
return self._from_json_document(doc=doc, doc_ref=url)
548+
except Exception as e:
549+
_log.warning(f"Failed to get/parse 'derived_from' document {url=}: {e=}")
550+
# Handle URL as id
551+
return [_ProductRef(item_id=url)]
547552

548553
def _from_path(self, path: Union[str, Path]) -> Iterable[_ProductRef]:
549554
with open(path) as f:

‎tests/testing/test_results.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,41 @@ def test_from_links_legacy(self):
788788
_ProductRef(item_id="NDVI300_20250927_V3"),
789789
}
790790

791+
def test_from_links_legacy_unresolvable_url(self, requests_mock, caplog):
792+
"""
793+
Legacy derived_from link: href is an URL, but not expected to be resolvable
794+
(e.g. it is internal or requires auth)
795+
"""
796+
href = "https://internal.test/item"
797+
requests_mock.get(href, status_code=401)
798+
links = [
799+
{"rel": "derived_from", "href": href},
800+
]
801+
derived_from = set(_DerivedFrom().from_links(links))
802+
assert derived_from == {
803+
_ProductRef(item_id="https://internal.test/item"),
804+
}
805+
assert caplog.messages == [
806+
dirty_equals.IsStr(regex=".*Failed to.*parse.*HTTPError.*401 Client Error.*"),
807+
]
808+
809+
def test_from_links_legacy_no_json_doc(self, requests_mock, caplog):
810+
"""
811+
Legacy derived_from link: href is an URL, but not a JSON document
812+
"""
813+
href = "https://internal.test/item"
814+
requests_mock.get(href, text="look ma no JSON!")
815+
links = [
816+
{"rel": "derived_from", "href": href},
817+
]
818+
derived_from = set(_DerivedFrom().from_links(links))
819+
assert derived_from == {
820+
_ProductRef(item_id="https://internal.test/item"),
821+
}
822+
assert caplog.messages == [
823+
dirty_equals.IsStr(regex=".*Failed to.*parse.*JSONDecodeError.*"),
824+
]
825+
791826
@pytest.fixture()
792827
def item_collection_doc(self):
793828
return {

0 commit comments

Comments
 (0)