|
| 1 | +import copy |
| 2 | +import urllib.parse |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from geojson_pydantic.geometries import Geometry |
| 6 | +from starlette.requests import Request |
| 7 | + |
| 8 | +from stac_fastapi.api.models import BaseSearchPostRequest |
| 9 | +from stac_fastapi.types.core import AsyncBaseCoreClient |
| 10 | +from stac_fastapi.types.errors import NotFoundError |
| 11 | +from stac_fastapi.types.rfc3339 import DateTimeType |
| 12 | +from stac_fastapi.types.stac import BBox, Collection, Collections, Item, ItemCollection |
| 13 | + |
| 14 | + |
| 15 | +class Client(AsyncBaseCoreClient): # type: ignore |
| 16 | + """A stac-fastapi-geoparquet client.""" |
| 17 | + |
| 18 | + async def all_collections(self, *, request: Request, **kwargs: Any) -> Collections: |
| 19 | + return Collections(collections=request.app.state.collections) |
| 20 | + |
| 21 | + async def get_collection( |
| 22 | + self, *, request: Request, collection_id: str, **kwargs: Any |
| 23 | + ) -> Collection: |
| 24 | + if collection := next( |
| 25 | + (c for c in request.app.state.collections if c["id"] == collection_id), None |
| 26 | + ): |
| 27 | + return Collection(**collection) |
| 28 | + else: |
| 29 | + raise NotFoundError(f"Collection does not exist: {collection_id}") |
| 30 | + |
| 31 | + async def get_item( |
| 32 | + self, *, request: Request, item_id: str, collection_id: str, **kwargs: Any |
| 33 | + ) -> Item: |
| 34 | + item_collection = request.app.state.client.search( |
| 35 | + request.app.state.href, ids=[item_id], collections=[collection_id], **kwargs |
| 36 | + ) |
| 37 | + if len(item_collection["features"]) == 1: |
| 38 | + return Item(**item_collection["features"][0]) |
| 39 | + else: |
| 40 | + raise NotFoundError( |
| 41 | + f"Item does not exist: {item_id} in collection {collection_id}" |
| 42 | + ) |
| 43 | + |
| 44 | + async def get_search( |
| 45 | + self, |
| 46 | + *, |
| 47 | + request: Request, |
| 48 | + collections: list[str] | None = None, |
| 49 | + ids: list[str] | None = None, |
| 50 | + bbox: BBox | None = None, |
| 51 | + intersects: Geometry | None = None, |
| 52 | + datetime: DateTimeType | None = None, |
| 53 | + limit: int | None = 10, |
| 54 | + offset: int | None = 0, |
| 55 | + **kwargs: str, |
| 56 | + ) -> ItemCollection: |
| 57 | + search = BaseSearchPostRequest( |
| 58 | + collections=collections, |
| 59 | + ids=ids, |
| 60 | + bbox=bbox, |
| 61 | + intersects=intersects, |
| 62 | + datetime=datetime, |
| 63 | + limit=limit, |
| 64 | + ) |
| 65 | + return await self.search( |
| 66 | + request=request, search=search, offset=offset, **kwargs |
| 67 | + ) |
| 68 | + |
| 69 | + async def item_collection( |
| 70 | + self, |
| 71 | + *, |
| 72 | + request: Request, |
| 73 | + bbox: BBox | None = None, |
| 74 | + datetime: DateTimeType | None = None, |
| 75 | + limit: int = 10, |
| 76 | + offset: int = 0, |
| 77 | + **kwargs: str, |
| 78 | + ) -> ItemCollection: |
| 79 | + search = BaseSearchPostRequest( |
| 80 | + bbox=bbox, datetime=datetime, limit=limit, offset=offset |
| 81 | + ) |
| 82 | + return await self.search(request=request, search=search, **kwargs) |
| 83 | + |
| 84 | + async def post_search( |
| 85 | + self, search_request: BaseSearchPostRequest, *, request: Request, **kwargs: Any |
| 86 | + ) -> ItemCollection: |
| 87 | + return await self.search(search=search_request, request=request, **kwargs) |
| 88 | + |
| 89 | + async def search( |
| 90 | + self, |
| 91 | + *, |
| 92 | + request: Request, |
| 93 | + search: BaseSearchPostRequest, |
| 94 | + **kwargs: Any, |
| 95 | + ) -> ItemCollection: |
| 96 | + search_dict = search.model_dump(exclude_none=True) |
| 97 | + search_dict.update(**kwargs) |
| 98 | + item_collection = request.app.state.client.search( |
| 99 | + request.app.state.href, |
| 100 | + **search_dict, |
| 101 | + ) |
| 102 | + num_items = len(item_collection["features"]) |
| 103 | + limit = int(search_dict.get("limit", None) or num_items) |
| 104 | + offset = int(search_dict.get("offset", None) or 0) |
| 105 | + |
| 106 | + if limit <= num_items: |
| 107 | + next_search = copy.deepcopy(search_dict) |
| 108 | + next_search["limit"] = limit |
| 109 | + next_search["offset"] = offset + limit |
| 110 | + else: |
| 111 | + next_search = None |
| 112 | + |
| 113 | + links = [] |
| 114 | + url = request.url_for("Search") |
| 115 | + if next_search: |
| 116 | + if request.method == "GET": |
| 117 | + links.append( |
| 118 | + { |
| 119 | + "href": str(url) + "?" + urllib.parse.urlencode(next_search), |
| 120 | + "rel": "next", |
| 121 | + "type": "application/geo+json", |
| 122 | + "method": "GET", |
| 123 | + } |
| 124 | + ) |
| 125 | + else: |
| 126 | + links.append( |
| 127 | + { |
| 128 | + "href": str(url), |
| 129 | + "rel": "next", |
| 130 | + "type": "application/geo+json", |
| 131 | + "method": "POST", |
| 132 | + "body": next_search, |
| 133 | + } |
| 134 | + ) |
| 135 | + |
| 136 | + item_collection["links"] = links |
| 137 | + return ItemCollection(**item_collection) |
0 commit comments