Skip to content

Commit 36ded7f

Browse files
committed
move Client-specific get_queryables implementation to Client class
1 parent 45c29e7 commit 36ded7f

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

pystac_client/client.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,54 @@ def _warn_about_fallback(self, *args: str) -> None:
335335
warnings.warn(DoesNotConformTo(*args), stacklevel=2)
336336
warnings.warn(FallbackToPystac(), stacklevel=2)
337337

338+
def get_queryables(self, *collections: Optional[str]) -> Dict[str, Any]:
339+
"""Return all queryables, or limit to those of specified collections.
340+
341+
Queryables from multiple collections are unioned together, except in the case
342+
when the same queryable key has a different definition, in which case that key
343+
is dropped.
344+
345+
Output is a dictionary that can be used in ``jsonshema.validate``
346+
Args:
347+
*collections: The IDs of the items to include.
348+
349+
Return:
350+
Dict[str, Any]: Dictionary containing queryable fields
351+
"""
352+
if not collections:
353+
return super().get_queryables()
354+
355+
col = self.get_collection(collections[0])
356+
assert isinstance(col, CollectionClient)
357+
response = col.get_queryables()
358+
addProps = response.get("additionalProperties", None)
359+
for collection in collections[1:]:
360+
col = self.get_collection(collection)
361+
assert isinstance(col, CollectionClient)
362+
col_resp = col.get_queryables()
363+
364+
# Ensure schema and additionalProperties consistency
365+
assert response["$schema"] == col_resp["$schema"], (
366+
f"$schema inconsistency between {response['$id']} "
367+
f"and {col_resp['$id']} ('{response['$schema']}' "
368+
f"!= '{col_resp['$schema']}')."
369+
)
370+
assert addProps == col_resp.get("additionalProperties", None), (
371+
"'additionalProperties' varies across collections specified"
372+
f" ({collections})"
373+
)
374+
375+
# drop queryables if their keys match, but the descriptions differ
376+
for k in set(col_resp["properties"].keys()).intersection(
377+
response["properties"].keys()
378+
):
379+
if col_resp["properties"][k] != response["properties"][k]:
380+
col_resp["properties"].pop(k)
381+
response["properties"].pop(k)
382+
response["properties"].update(col_resp["properties"])
383+
384+
return response
385+
338386
@lru_cache()
339387
def get_collection(self, collection_id: str) -> Union[Collection, CollectionClient]:
340388
"""Get a single collection from this Catalog/API

pystac_client/mixins.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,14 @@ def _get_href(self, rel: str, link: Optional[pystac.Link], endpoint: str) -> str
3232
class QueryablesMixin(BaseMixin):
3333
"""Mixin for adding support for /queryables endpoint"""
3434

35-
def get_queryables(self, *collections: Optional[str]) -> Dict[str, Any]:
36-
"""Return all queryables, or limit to those of specified collections.
37-
38-
Queryables from multiple collections are unioned together, except in the case when the same queryable key has a different definition, in which case that key is dropped.
35+
def get_queryables(self) -> Dict[str, Any]:
36+
"""Return all queryables.
3937
4038
Output is a dictionary that can be used in ``jsonshema.validate``
41-
Args:
42-
*collections: The IDs of the items to include.
4339
4440
Return:
4541
Dict[str, Any]: Dictionary containing queryable fields
4642
"""
47-
if collections and isinstance(self, pystac.Catalog):
48-
response = self.get_collection(collections[0]).get_queryables()
49-
response.pop("$id")
50-
for collection in collections[1:]:
51-
col_resp = self.get_collection(collection).get_queryables()
52-
response["properties"].update(col_resp["properties"])
53-
return response
54-
5543
if self._stac_io is None:
5644
raise APIError("API access is not properly configured")
5745

0 commit comments

Comments
 (0)