Skip to content

Commit

Permalink
Enable search on /items and add queryables links
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Mar 14, 2024
1 parent a1b0633 commit b234f1c
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion stac_fastapi/pgstac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from stac_fastapi.types.core import AsyncBaseCoreClient
from stac_fastapi.types.errors import InvalidQueryParameter, NotFoundError
from stac_fastapi.types.requests import get_base_url
from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection
from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection, LandingPage
from stac_pydantic.links import Relations
from stac_pydantic.shared import MimeTypes

Expand All @@ -36,6 +36,30 @@
@attr.s
class CoreCrudClient(AsyncBaseCoreClient):
"""Client for core endpoints defined by stac."""

async def landing_page(self, **kwargs) -> LandingPage:
"""Landing page.
Called with `GET /`.
Returns:
API landing page, serving as an entry point to the API.
"""
request: Request = kwargs["request"]
base_url = get_base_url(request)
landing_page = await super().landing_page(**kwargs)

if self.extension_is_enabled("FilterExtension"):
landing_page["links"].append(
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/queryables",
"type": "application/schema+json",
"title": "Queryables",
"href": urljoin(base_url, "queryables"),
}
)

return landing_page

async def all_collections(self, request: Request, **kwargs) -> Collections:
"""Read all collections from the database."""
Expand All @@ -55,6 +79,16 @@ async def all_collections(self, request: Request, **kwargs) -> Collections:
collection_id=coll["id"], request=request
).get_links(extra_links=coll.get("links"))

if self.extension_is_enabled("FilterExtension"):
coll["links"].append(
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/queryables",
"type": "application/schema+json",
"title": "Queryables",
"href": urljoin(base_url, f"collections/{coll['id']}/queryables"),
}
)

linked_collections.append(coll)

links = [
Expand Down Expand Up @@ -107,6 +141,17 @@ async def get_collection(
collection_id=collection_id, request=request
).get_links(extra_links=collection.get("links"))

if self.extension_is_enabled("FilterExtension"):
base_url = get_base_url(request)
collection["links"].append(
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/queryables",
"type": "application/schema+json",
"title": "Queryables",
"href": urljoin(base_url, f"collections/{collection_id}/queryables"),
}
)

return Collection(**collection)

async def _get_base_item(
Expand Down Expand Up @@ -277,6 +322,15 @@ async def item_collection(
"token": token,
}

if self.extension_is_enabled("FilterExtension"):
filter_lang = request.query_params.get("filter-lang", None)
filter = request.query_params.get("filter", "").strip()

if len(filter) > 0 and filter_lang == "cql2-text":
ast = parse_cql2_text(filter)
base_args["filter"] = orjson.loads(to_cql2(ast))
base_args["filter-lang"] = "cql2-json"

clean = {}
for k, v in base_args.items():
if v is not None and v != []:
Expand Down

0 comments on commit b234f1c

Please sign in to comment.