Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- Serializing optional Collection attributes ([#916](https://github.com/stac-utils/pystac/pull/916))
- A couple non-running tests ([#912](https://github.com/stac-utils/pystac/pull/912))
- Support relative stac extension paths via `make_absolute_href` ([#884](https://github.com/stac-utils/pystac/pull/884))
- Filtering on `media_type` in `get_links()` and `get_single_link()` ([#966](https://github.com/stac-utils/pystac/pull/966))

## [v1.6.1]

Expand Down
32 changes: 20 additions & 12 deletions pystac/stac_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,30 @@ def remove_links(self, rel: Union[str, pystac.RelType]) -> None:

def get_single_link(
self,
rel: Union[str, pystac.RelType],
rel: Optional[Union[str, pystac.RelType]] = None,
media_type: Optional[Union[str, pystac.MediaType]] = None,
) -> Optional[Link]:
"""Get single link that match the given ``rel`` and, optionally,
``media_type``. If ``media_type`` is ``None``, then the link is
matched only on the ``rel`` value.
"""Get a single :class:`~pystac.Link` instance associated with this
object.

Args:
rel : The :class:`~pystac.Link` ``rel`` to match on.
media_type: The :class:`~pystack.MediaType` ``media_type`` to match on
"""
rel : If set, filter links such that only those
matching this relationship are returned.
media_type: If set, filter the links such that only
those matching media_type are returned

Returns:
Optional[:class:`~pystac.Link`]: First link that matches ``rel``
and/or ``media_type``, or else the first link associated with
this object.
"""
if rel is None and media_type is None:
return next(iter(self.links), None)
return next(
(
link
for link in self.links
if link.rel == rel
if (rel is None or link.rel == rel)
and (media_type is None or link.media_type == media_type)
),
None,
Expand All @@ -119,16 +126,17 @@ def get_links(
those matching media_type are returned

Returns:
List[:class:`~pystac.Link`]: A list of links that match ``rel`` if set,
or else all links associated with this object.
List[:class:`~pystac.Link`]: A list of links that match ``rel`` and/
or ``media_type`` if set, or else all links associated with this
object.
"""
if rel is None:
if rel is None and media_type is None:
return self.links
else:
return [
link
for link in self.links
if link.rel == rel
if (rel is None or link.rel == rel)
and (media_type is None or link.media_type == media_type)
]

Expand Down
24 changes: 20 additions & 4 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,14 +1238,24 @@ def test_get_single_links_media_type(self) -> None:
)
)

html_link = catalog.get_single_link("search")
html_link = catalog.get_single_link(rel="search")
assert html_link is not None
assert html_link.href == "./search.html"
json_link = catalog.get_single_link("search", media_type="application/geo+json")
html_link = catalog.get_single_link(media_type="text/html")
assert html_link is not None
assert html_link.href == "./search.html"
json_link = catalog.get_single_link(
rel="search", media_type="application/geo+json"
)
assert json_link is not None
assert json_link.href == "./search.json"
no_link = catalog.get_single_link(rel="via")
assert no_link is None
first_link = catalog.get_single_link()
assert first_link is not None
assert first_link.rel == "self"

def test_get_links_media_type(self) -> None:
def test_get_links(self) -> None:
catalog = TestCases.case_1()

catalog.links.append(
Expand All @@ -1256,7 +1266,13 @@ def test_get_links_media_type(self) -> None:
rel="search", target="./search.json", media_type="application/geo+json"
)
)
assert len(catalog.get_links("search", media_type="application/geo+json")) == 1
assert (
len(catalog.get_links(rel="search", media_type="application/geo+json")) == 1
)
assert len(catalog.get_links(media_type="text/html")) == 1
assert len(catalog.get_links(rel="search")) == 2
assert len(catalog.get_links(rel="via")) == 0
assert len(catalog.get_links()) == 6

def test_to_dict_no_self_href(self) -> None:
catalog = Catalog(id="an-id", description="A test Catalog")
Expand Down