From 99665c444ff80dc2baa5b38d2444af8ec7c43a3e Mon Sep 17 00:00:00 2001 From: vincentsarago <vincent.sarago@gmail.com> Date: Tue, 25 Jul 2023 10:58:22 +0200 Subject: [PATCH] add Catalog Dependency --- CHANGES.md | 1 + tipg/dependencies.py | 9 +++++++++ tipg/factory.py | 16 ++++------------ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 3b48c7f2..7451f404 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,7 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin - `type` query parameter to filter collections based on their type (`Function` or `Table`) - fixed a small bug in the `tipg_properties` SQL function where the bounds property was not properly transformed to 4326 (author @RemcoMeeuwissen, https://github.com/developmentseed/tipg/pull/87) - added popups to leaflet maps on `items` and `item` page. (author @krishnaglodha & @jackharrhy, https://github.com/developmentseed/tipg/pull/91, https://github.com/developmentseed/tipg/pull/94) +- `catalog_dependency` to retrieve the list of collections (defaults to `tipg.dependencies.CatalogParams`) ### Changed diff --git a/tipg/dependencies.py b/tipg/dependencies.py index 1b2b417a..361f2fd0 100644 --- a/tipg/dependencies.py +++ b/tipg/dependencies.py @@ -56,6 +56,15 @@ def CollectionParams( ) +def CatalogParams(request: Request) -> Catalog: + """Return Collections Catalog.""" + collection_catalog: Catalog = getattr(request.app.state, "collection_catalog", None) + if not collection_catalog: + raise MissingCollectionCatalog("Could not find collections catalog.") + + return collection_catalog + + def accept_media_type( accept: str, mediatypes: List[enums.MediaType] ) -> Optional[enums.MediaType]: diff --git a/tipg/factory.py b/tipg/factory.py index ee31cc8d..a188cb25 100644 --- a/tipg/factory.py +++ b/tipg/factory.py @@ -29,6 +29,7 @@ from tipg import model from tipg.collections import Catalog, Collection from tipg.dependencies import ( + CatalogParams, CollectionParams, ItemsOutputType, OutputType, @@ -42,12 +43,7 @@ properties_query, sortby_query, ) -from tipg.errors import ( - MissingCollectionCatalog, - MissingGeometryColumn, - NoPrimaryKey, - NotFound, -) +from tipg.errors import MissingGeometryColumn, NoPrimaryKey, NotFound from tipg.resources.enums import MediaType from tipg.resources.response import GeoJSONResponse, SchemaJSONResponse from tipg.settings import FeaturesSettings, MVTSettings, TMSSettings @@ -193,6 +189,7 @@ class EndpointsFactory(metaclass=abc.ABCMeta): router: APIRouter = field(default_factory=APIRouter) # collection dependency + catalog_dependency: Callable[..., Catalog] = CatalogParams collection_dependency: Callable[..., Collection] = CollectionParams # Router Prefix is needed to find the path for routes when prefixed @@ -463,14 +460,9 @@ def collections( # noqa: C901 ), ] = None, output_type: Annotated[Optional[MediaType], Depends(OutputType)] = None, + collection_catalog=Depends(self.catalog_dependency), ): """List of collections.""" - collection_catalog: Catalog = getattr( - request.app.state, "collection_catalog", None - ) - if not collection_catalog: - raise MissingCollectionCatalog("Could not find collections catalog.") - collections_list = list(collection_catalog["collections"].values()) limit = limit or 0