Skip to content

Commit d91ca60

Browse files
committed
feat: restore Link.resolve_stac_object
1 parent bd3b175 commit d91ca60

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/pystac/link.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
from typing_extensions import deprecated
88

9+
import pystac
910
from pystac.errors import STACError
1011
from pystac.media_type import MediaType
1112
from pystac.rel_type import RelType
12-
from pystac.utils import make_absolute_href, make_posix_style
13+
from pystac.utils import is_absolute_href, make_absolute_href, make_posix_style
1314

1415
from .reader import Reader
1516

@@ -203,6 +204,34 @@ def to_dict(self, transform_href: bool | None = None) -> dict[str, Any]:
203204
data["body"] = self.body
204205
return data
205206

207+
def resolve_stac_object(self, start_href: str = "") -> Link:
208+
"""Resolves a STAC object from the HREF of this link, if the link is not
209+
already resolved.
210+
211+
Args:
212+
start_href : Optional string to put ahead of the href in this Link.
213+
214+
NOTE: This uses reader.DEFAULT_READER to read the HREF, if necessary.
215+
"""
216+
if self._target:
217+
return self
218+
elif self._href:
219+
# If it's a relative link, base it off the parent.
220+
target_href = self._href
221+
if not is_absolute_href(target_href):
222+
target_href = make_absolute_href(self._href, start_href=start_href)
223+
try:
224+
obj = pystac.read_file(target_href)
225+
except Exception as e:
226+
raise STACError(
227+
f"HREF: '{target_href}' does not resolve to a STAC object"
228+
) from e
229+
self._target = obj
230+
else:
231+
raise ValueError("Cannot resolve STAC object without a target")
232+
233+
return self
234+
206235
@override
207236
def __repr__(self) -> str:
208237
return f"Link(rel={self.rel}, href={self._href})"

tests/v1/test_link.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,11 @@ def test_link_does_not_fail_if_href_is_none(item: pystac.Item) -> None:
8585
assert link.get_href() is None
8686

8787

88-
@pytest.mark.xfail(reason="STACObjects no longer have resolve_stac_object method")
8988
def test_resolve_stac_object_no_root_and_target_is_item(item: pystac.Item) -> None:
9089
link = Link("my rel", target=item)
9190
link.resolve_stac_object()
9291

9392

94-
@pytest.mark.xfail(reason="STACObjects no longer have resolve_stac_object method")
9593
@pytest.mark.skipif(os.name == "nt", reason="Non-windows test")
9694
def test_resolve_stac_object_throws_informative_error() -> None:
9795
link = Link("root", target="/a/b/foo.json")
@@ -101,7 +99,6 @@ def test_resolve_stac_object_throws_informative_error() -> None:
10199
link.resolve_stac_object()
102100

103101

104-
@pytest.mark.xfail(reason="STACObjects no longer have resolve_stac_object method")
105102
def test_resolved_self_href() -> None:
106103
catalog = Catalog(id="test", description="test desc")
107104
with TemporaryDirectory() as temporary_directory:

0 commit comments

Comments
 (0)