Skip to content

Commit

Permalink
Update stac-fastapi and stac-fastapi-pgstac (#181)
Browse files Browse the repository at this point in the history
* deps: update to stac-fastapi v2.4.8

* fix: update stac-fastapi-pgstac
  • Loading branch information
gadomski authored Jun 27, 2023
1 parent 8043ed2 commit b0471ea
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 74 deletions.
55 changes: 30 additions & 25 deletions pcstac/pcstac/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def inject_item_links(self, item: Item, request: Request) -> Item:
settings.back_pressures.collections.req_per_sec,
settings.back_pressures.collections.inc_ms,
)
async def all_collections(self, **kwargs: Any) -> Collections:
async def all_collections(self, request: Request, **kwargs: Any) -> Collections:
"""Read collections from the database and inject PQE links.
Called with `GET /collections`.
Expand All @@ -118,10 +118,9 @@ async def all_collections(self, **kwargs: Any) -> Collections:
Collections.
"""
_super: CoreCrudClient = super()
_request = kwargs["request"]

async def _fetch() -> Collections:
collections = await _super.all_collections(**kwargs)
collections = await _super.all_collections(request=request, **kwargs)
render_configs = get_all_render_configs()
modified_collections = []
for col in collections.get("collections", []):
Expand All @@ -136,20 +135,22 @@ async def _fetch() -> Collections:
pass
else:
modified_collections.append(
self.inject_collection_extras(col, _request, render_config)
self.inject_collection_extras(col, request, render_config)
)
collections["collections"] = modified_collections
return collections

return await cached_result(_fetch, CACHE_KEY_COLLECTIONS, kwargs["request"])
return await cached_result(_fetch, CACHE_KEY_COLLECTIONS, request)

@rate_limit(CACHE_KEY_COLLECTION, settings.rate_limits.collection)
@back_pressure(
CACHE_KEY_COLLECTION,
settings.back_pressures.collection.req_per_sec,
settings.back_pressures.collection.inc_ms,
)
async def get_collection(self, collection_id: str, **kwargs: Any) -> Collection:
async def get_collection(
self, collection_id: str, request: Request, **kwargs: Any
) -> Collection:
"""Get collection by id and inject PQE links.
Called with `GET /collections/{collection_id}`.
Expand All @@ -162,7 +163,6 @@ async def get_collection(self, collection_id: str, **kwargs: Any) -> Collection:
Collection.
"""
_super: CoreCrudClient = super()
_request = kwargs["request"]

async def _fetch() -> Collection:
try:
Expand All @@ -173,13 +173,15 @@ async def _fetch() -> Collection:
if render_config and render_config.hidden:
raise NotFoundError

result = await _super.get_collection(collection_id, **kwargs)
result = await _super.get_collection(
collection_id, request=request, **kwargs
)
except NotFoundError:
raise NotFoundError(f"No collection with id '{collection_id}' found!")
return self.inject_collection_extras(result, _request, render_config)
return self.inject_collection_extras(result, request, render_config)

cache_key = f"{CACHE_KEY_COLLECTION}:{collection_id}"
return await cached_result(_fetch, cache_key, kwargs["request"])
return await cached_result(_fetch, cache_key, request)

@rate_limit(CACHE_KEY_SEARCH, settings.rate_limits.search)
@back_pressure(
Expand All @@ -188,7 +190,7 @@ async def _fetch() -> Collection:
settings.back_pressures.search.inc_ms,
)
async def _search_base(
self, search_request: PCSearch, **kwargs: Any
self, search_request: PCSearch, request: Request, **kwargs: Any
) -> ItemCollection:
"""Cross catalog search (POST).
Called with `POST /search`.
Expand All @@ -198,10 +200,11 @@ async def _search_base(
ItemCollection containing items which match the search criteria.
"""
_super: CoreCrudClient = super()
request = kwargs["request"]

async def _fetch() -> ItemCollection:
result = await _super._search_base(search_request, **kwargs)
result = await _super._search_base(
search_request, request=request, **kwargs
)

# Remove context extension until we fully support it.
result.pop("context", None)
Expand Down Expand Up @@ -231,19 +234,16 @@ async def _fetch() -> ItemCollection:

hashed_search = hash(search_json)
cache_key = f"{CACHE_KEY_SEARCH}:{hashed_search}"
return await cached_result(_fetch, cache_key, kwargs["request"])
return await cached_result(_fetch, cache_key, request)

async def landing_page(self, **kwargs: Any) -> LandingPage:
async def landing_page(self, request: Request, **kwargs: Any) -> LandingPage:
_super: CoreCrudClient = super()

async def _fetch() -> LandingPage:
landing = await _super.landing_page(**kwargs)
# Remove once
# https://github.com/stac-utils/stac-fastapi/issues/334 is fixed.
del landing["stac_extensions"]
landing = await _super.landing_page(request=request, **kwargs)
return landing

return await cached_result(_fetch, CACHE_KEY_LANDING_PAGE, kwargs["request"])
return await cached_result(_fetch, CACHE_KEY_LANDING_PAGE, request)

@rate_limit(CACHE_KEY_ITEMS, settings.rate_limits.items)
@back_pressure(
Expand All @@ -254,6 +254,7 @@ async def _fetch() -> LandingPage:
async def item_collection(
self,
collection_id: str,
request: Request,
limit: Optional[int] = None,
token: Optional[str] = None,
**kwargs: Any,
Expand All @@ -262,27 +263,31 @@ async def item_collection(

async def _fetch() -> ItemCollection:
return await _super.item_collection(
collection_id, limit=limit, token=token, **kwargs
collection_id, request=request, limit=limit, token=token, **kwargs
)

cache_key = f"{CACHE_KEY_ITEMS}:{collection_id}:limit:{limit}:token:{token}"
return await cached_result(_fetch, cache_key, kwargs["request"])
return await cached_result(_fetch, cache_key, request)

@rate_limit(CACHE_KEY_ITEM, settings.rate_limits.item)
@back_pressure(
CACHE_KEY_ITEM,
settings.back_pressures.item.req_per_sec,
settings.back_pressures.item.inc_ms,
)
async def get_item(self, item_id: str, collection_id: str, **kwargs: Any) -> Item:
async def get_item(
self, item_id: str, collection_id: str, request: Request, **kwargs: Any
) -> Item:
_super: CoreCrudClient = super()

async def _fetch() -> Item:
item = await _super.get_item(item_id, collection_id, **kwargs)
item = await _super.get_item(
item_id, collection_id, request=request, **kwargs
)
return item

cache_key = f"{CACHE_KEY_ITEM}:{collection_id}:{item_id}"
return await cached_result(_fetch, cache_key, kwargs["request"])
return await cached_result(_fetch, cache_key, request)

@classmethod
def create(
Expand Down
2 changes: 1 addition & 1 deletion pcstac/pcstac/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pcstac.filter import PCFiltersClient

API_VERSION = "1.2"
STAC_API_VERSION = "v1.0.0-rc.1"
STAC_API_VERSION = "v1.0.0"

API_LANDING_PAGE_ID = "microsoft-pc"
API_TITLE = "Microsoft Planetary Computer STAC API"
Expand Down
Loading

0 comments on commit b0471ea

Please sign in to comment.