Skip to content

Commit a60a643

Browse files
committed
fix: get collection links without bug
rename internal core method '_get_collection()' to '_format_collection()' remove label:assets if null
1 parent f5494d6 commit a60a643

3 files changed

Lines changed: 29 additions & 25 deletions

File tree

stac_fastapi/eodag/core.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,14 @@ class EodagCoreClient(CustomCoreClient):
8585
post_request_model: type[BaseModel] = attr.ib(default=BaseSearchPostRequest)
8686
stac_metadata_model: type[CommonStacMetadata] = attr.ib(default=CommonStacMetadata)
8787

88-
def _get_collection(
89-
self, collection: EodagCollection, request: Request, collections_providers: dict[str, set]
90-
) -> Collection:
91-
"""Convert a EODAG produt type to a STAC collection."""
88+
def _format_collection(self, collection: EodagCollection, request: Request) -> Collection:
89+
"""Convert a EODAG STAC collection to a STAC collection for API."""
9290

9391
# keep only federation backends which allow order mechanism
9492
# to create "retrieve" collection links from them
9593
# TODO: this needs to be changed: we cannot request the search plugins for each collection, it is too costly.
96-
# TODO: We should find a way to know which federation backends support the order mechanism without requesting the plugins manager
94+
# TODO: We should find a way to know which federation backends support
95+
# the order mechanism without requesting the plugins manager
9796
def has_ecmwf_search_plugin(federation_backends, request):
9897
for fb in federation_backends:
9998
search_plugins = request.app.state.dag._plugins_manager.get_search_plugins(provider=fb)
@@ -111,13 +110,20 @@ def has_ecmwf_search_plugin(federation_backends, request):
111110
):
112111
extension_names.remove("CollectionOrderExtension")
113112

114-
coll_with_links = collection.model_dump(mode="json", exclude={"alias", "eodag_stac_collection"})
115-
coll_with_links["links"] = CollectionLinks(
113+
coll_dict = collection.model_dump(mode="json", exclude={"alias", "eodag_stac_collection"})
114+
for link in coll_dict["links"]:
115+
if link.get("label:assets") is None:
116+
link.pop("label:assets")
117+
118+
# add API-required links
119+
all_coll_links = CollectionLinks(
116120
collection_id=collection.id,
117121
request=request,
118-
).get_links(extensions=extension_names, extra_links=coll_with_links["links"])
122+
).get_links(extensions=extension_names, extra_links=coll_dict["links"])
119123

120-
return Collection(**coll_with_links)
124+
# remove eodag-specific fields
125+
coll_dict["links"] = all_coll_links
126+
return Collection(**coll_dict)
121127

122128
async def _search_base(self, search_request: BaseSearchPostRequest, request: Request) -> ItemCollection:
123129
eodag_args = prepare_search_base_args(search_request=search_request, model=self.stac_metadata_model)
@@ -237,7 +243,7 @@ async def all_collections(
237243
limit=limit,
238244
q=q,
239245
cql2_json=cql2_json,
240-
sortby=sortby
246+
sortby=sortby,
241247
)
242248
)
243249

@@ -269,7 +275,8 @@ async def all_collections(
269275

270276
first_link = {"body": {"limit": limit, "offset": 0}}
271277

272-
formatted_collections = [self._get_collection(coll, request) for coll in collections]
278+
# format collections
279+
formatted_collections = [self._format_collection(coll, request) for coll in collections]
273280

274281
extension_names = [type(ext).__name__ for ext in self.extensions]
275282

@@ -298,18 +305,14 @@ async def get_collection(self, collection_id: str, request: Request, **kwargs: A
298305
:returns: The collection.
299306
:raises NotFoundError: If the collection does not exist.
300307
"""
301-
collection = cast(Optional[EodagCollection], await asyncio.to_thread(request.app.state.dag.get_collection, id=collection_id))
308+
collection = cast(
309+
Optional[EodagCollection], await asyncio.to_thread(request.app.state.dag.get_collection, id=collection_id)
310+
)
302311

303312
if collection is None:
304313
raise NotFoundError(f"Collection {collection_id} does not exist.")
305314

306-
providers = request.app.state.dag.providers
307-
collection_providers: dict[str, set] = {collection._id: set()}
308-
for p_name, p in providers.items():
309-
if getattr(p.config, "products", None) and collection._id in p.config.products:
310-
collection_providers[collection._id].add(p_name)
311-
312-
return self._get_collection(collection, request, collection_providers)
315+
return self._format_collection(collection, request)
313316

314317
async def item_collection(
315318
self,
@@ -352,10 +355,11 @@ async def item_collection(
352355
)
353356

354357
search_request = self.post_request_model.model_validate(clean)
355-
item_collection = await self._search_base(search_request, request)
358+
item_collection = cast(ItemCollection, await self._search_base(search_request, request))
356359
extension_names = [type(ext).__name__ for ext in self.extensions]
360+
extra_links = item_collection.get("links", [])
357361
links = ItemCollectionLinks(collection_id=collection_id, request=request).get_links(
358-
extensions=extension_names, extra_links=item_collection["links"]
362+
extensions=extension_names, extra_links=extra_links
359363
)
360364
item_collection["links"] = links
361365
return item_collection

stac_fastapi/eodag/dag.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ def init_dag(app: FastAPI) -> None:
7373
"""Init EODataAccessGateway server instance, pre-running all time consuming tasks"""
7474
dag = EODataAccessGateway()
7575

76-
ext_stac_collections = fetch_external_stac_collections(
77-
dag.list_collections()
78-
)
76+
ext_stac_collections = fetch_external_stac_collections(dag.list_collections())
7977

8078
# update eodag collections config from external stac collections
8179
collections = {}

stac_fastapi/eodag/models/item.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from stac_fastapi.types.requests import get_base_url
2727
from stac_fastapi.types.stac import Item
2828
from stac_pydantic.api.version import STAC_API_VERSION
29+
from stac_pydantic.links import Link, Links
2930
from stac_pydantic.shared import Asset
3031

3132
from eodag.api.product._product import EOProduct
@@ -182,11 +183,12 @@ def create_stac_item(
182183
if provider := eodag_args.get("provider", None):
183184
retrieve_body["federation:backends"] = [provider]
184185

186+
extra_links = Links(root=[Link(**link) for link in feature.get("links", [])])
185187
feature["links"] = ItemLinks(
186188
collection_id=product.collection,
187189
item_id=quoted_id,
188190
retrieve_body=retrieve_body,
189191
request=request,
190-
).get_links(extensions=extension_names, extra_links=feature.get("links"), request_json=request_json)
192+
).get_links(extensions=extension_names, extra_links=extra_links, request_json=request_json)
191193

192194
return feature

0 commit comments

Comments
 (0)