33from abc import ABC , abstractmethod
44from collections .abc import Callable , Iterable
55from html import escape
6- from typing import (
7- TYPE_CHECKING ,
8- Any ,
9- TypeVar ,
10- cast ,
11- )
6+ from typing import TYPE_CHECKING , Any , TypeAlias , TypeVar , cast
127
138import pystac
149from pystac import STACError
2722
2823S = TypeVar ("S" , bound = "STACObject" )
2924
25+ STACObject_MediaType : TypeAlias = str | pystac .MediaType | None
26+
3027
3128class STACObjectType (StringEnum ):
3229 CATALOG = "Catalog"
@@ -177,7 +174,7 @@ def traverse(obj: str | STACObject, visited: set[str | STACObject]) -> bool:
177174 def get_single_link (
178175 self ,
179176 rel : str | pystac .RelType | None = None ,
180- media_type : str | pystac . MediaType | None = None ,
177+ media_type : STACObject_MediaType | Iterable [ STACObject_MediaType ] = None ,
181178 ) -> Link | None :
182179 """Get a single :class:`~pystac.Link` instance associated with this
183180 object.
@@ -186,37 +183,41 @@ def get_single_link(
186183 rel : If set, filter links such that only those
187184 matching this relationship are returned.
188185 media_type: If set, filter the links such that only
189- those matching media_type are returned
186+ those matching media_type are returned. media_type can
187+ be a single value or a list of values.
190188
191189 Returns:
192- Optional[ :class:`~pystac.Link`] : First link that matches ``rel``
190+ :class:`~pystac.Link` | None : First link that matches ``rel``
193191 and/or ``media_type``, or else the first link associated with
194192 this object.
195193 """
196194 if rel is None and media_type is None :
197195 return next (iter (self .links ), None )
196+ if media_type and isinstance (media_type , (str , pystac .MediaType )):
197+ media_type = [media_type ]
198198 return next (
199199 (
200200 link
201201 for link in self .links
202202 if (rel is None or link .rel == rel )
203- and (media_type is None or link .media_type == media_type )
203+ and (media_type is None or link .media_type in media_type )
204204 ),
205205 None ,
206206 )
207207
208208 def get_links (
209209 self ,
210210 rel : str | pystac .RelType | None = None ,
211- media_type : str | pystac . MediaType | None = None ,
211+ media_type : STACObject_MediaType | Iterable [ STACObject_MediaType ] = None ,
212212 ) -> list [Link ]:
213213 """Gets the :class:`~pystac.Link` instances associated with this object.
214214
215215 Args:
216216 rel : If set, filter links such that only those
217217 matching this relationship are returned.
218218 media_type: If set, filter the links such that only
219- those matching media_type are returned
219+ those matching media_type are returned. media_type can
220+ be a single value or a list of values.
220221
221222 Returns:
222223 List[:class:`~pystac.Link`]: A list of links that match ``rel`` and/
@@ -225,13 +226,14 @@ def get_links(
225226 """
226227 if rel is None and media_type is None :
227228 return self .links
228- else :
229- return [
230- link
231- for link in self .links
232- if (rel is None or link .rel == rel )
233- and (media_type is None or link .media_type == media_type )
234- ]
229+ if media_type and isinstance (media_type , (str , pystac .MediaType )):
230+ media_type = [media_type ]
231+ return [
232+ link
233+ for link in self .links
234+ if (rel is None or link .rel == rel )
235+ and (media_type is None or link .media_type in media_type )
236+ ]
235237
236238 def clear_links (self , rel : str | pystac .RelType | None = None ) -> None :
237239 """Clears all :class:`~pystac.Link` instances associated with this object.
@@ -252,7 +254,10 @@ def get_root_link(self) -> Link | None:
252254 :class:`~pystac.Link` or None: The root link for this object,
253255 or ``None`` if no root link is set.
254256 """
255- return self .get_single_link (pystac .RelType .ROOT )
257+ return self .get_single_link (
258+ rel = pystac .RelType .ROOT ,
259+ media_type = [None , pystac .MediaType .GEOJSON , pystac .MediaType .JSON ],
260+ )
256261
257262 @property
258263 def self_href (self ) -> str :
0 commit comments