Skip to content

Commit

Permalink
remove response_model module, update openapi schema
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-maschler committed Apr 5, 2024
1 parent 5bdd615 commit eea9c82
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 211 deletions.
84 changes: 75 additions & 9 deletions stac_fastapi/api/stac_fastapi/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from fastapi import APIRouter, FastAPI
from fastapi.openapi.utils import get_openapi
from fastapi.params import Depends
from stac_pydantic import Collection, Item, ItemCollection
from stac_pydantic.api import ConformanceClasses, LandingPage
from stac_pydantic import api
from stac_pydantic.api.collections import Collections
from stac_pydantic.api.version import STAC_API_VERSION
from stac_pydantic.shared import MimeTypes
from starlette.responses import JSONResponse, Response

from stac_fastapi.api.errors import DEFAULT_STATUS_CODES, add_exception_handlers
Expand Down Expand Up @@ -127,8 +127,16 @@ def register_landing_page(self):
name="Landing Page",
path="/",
response_model=(
LandingPage if self.settings.enable_response_models else None
api.LandingPage if self.settings.enable_response_models else None
),
responses={
200: {
"content": {
MimeTypes.json.value: {},
},
"model": api.LandingPage,
},
},
response_class=self.response_class,
response_model_exclude_unset=False,
response_model_exclude_none=True,
Expand All @@ -148,8 +156,16 @@ def register_conformance_classes(self):
name="Conformance Classes",
path="/conformance",
response_model=(
ConformanceClasses if self.settings.enable_response_models else None
api.ConformanceClasses if self.settings.enable_response_models else None
),
responses={
200: {
"content": {
MimeTypes.json.value: {},
},
"model": api.ConformanceClasses,
},
},
response_class=self.response_class,
response_model_exclude_unset=True,
response_model_exclude_none=True,
Expand All @@ -168,7 +184,15 @@ def register_get_item(self):
self.router.add_api_route(
name="Get Item",
path="/collections/{collection_id}/items/{item_id}",
response_model=Item if self.settings.enable_response_models else None,
response_model=api.Item if self.settings.enable_response_models else None,
responses={
200: {
"content": {
MimeTypes.geojson.value: {},
},
"model": api.Item,
},
},
response_class=GeoJSONResponse,
response_model_exclude_unset=True,
response_model_exclude_none=True,
Expand All @@ -189,10 +213,18 @@ def register_post_search(self):
name="Search",
path="/search",
response_model=(
(ItemCollection if not fields_ext else None)
(api.ItemCollection if not fields_ext else None)
if self.settings.enable_response_models
else None
),
responses={
200: {
"content": {
MimeTypes.geojson.value: {},
},
"model": api.ItemCollection,
},
},
response_class=GeoJSONResponse,
response_model_exclude_unset=True,
response_model_exclude_none=True,
Expand All @@ -213,10 +245,18 @@ def register_get_search(self):
name="Search",
path="/search",
response_model=(
(ItemCollection if not fields_ext else None)
(api.ItemCollection if not fields_ext else None)
if self.settings.enable_response_models
else None
),
responses={
200: {
"content": {
MimeTypes.geojson.value: {},
},
"model": api.ItemCollection,
},
},
response_class=GeoJSONResponse,
response_model_exclude_unset=True,
response_model_exclude_none=True,
Expand All @@ -238,6 +278,14 @@ def register_get_collections(self):
response_model=(
Collections if self.settings.enable_response_models else None
),
responses={
200: {
"content": {
MimeTypes.json.value: {},
},
"model": Collections,
},
},
response_class=self.response_class,
response_model_exclude_unset=True,
response_model_exclude_none=True,
Expand All @@ -256,7 +304,17 @@ def register_get_collection(self):
self.router.add_api_route(
name="Get Collection",
path="/collections/{collection_id}",
response_model=Collection if self.settings.enable_response_models else None,
response_model=api.Collection
if self.settings.enable_response_models
else None,
responses={
200: {
"content": {
MimeTypes.json.value: {},
},
"model": api.Collection,
},
},
response_class=self.response_class,
response_model_exclude_unset=True,
response_model_exclude_none=True,
Expand Down Expand Up @@ -286,8 +344,16 @@ def register_get_item_collection(self):
name="Get ItemCollection",
path="/collections/{collection_id}/items",
response_model=(
ItemCollection if self.settings.enable_response_models else None
api.ItemCollection if self.settings.enable_response_models else None
),
responses={
200: {
"content": {
MimeTypes.geojson.value: {},
},
"model": api.ItemCollection,
},
},
response_class=GeoJSONResponse,
response_model_exclude_unset=True,
response_model_exclude_none=True,
Expand Down
38 changes: 17 additions & 21 deletions stac_fastapi/api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from stac_pydantic import Collection, Item
from stac_pydantic.api.utils import link_factory

from stac_fastapi.types import core, response_model
from stac_fastapi.types import core, stac
from stac_fastapi.types.core import NumType
from stac_fastapi.types.search import BaseSearchPostRequest

Expand Down Expand Up @@ -67,9 +67,9 @@ def TestCoreClient(collection_dict, item_dict):
class CoreClient(core.BaseCoreClient):
def post_search(
self, search_request: BaseSearchPostRequest, **kwargs
) -> response_model.ItemCollection:
return response_model.ItemCollection(
type="FeatureCollection", features=[response_model.Item(**item_dict)]
) -> stac.ItemCollection:
return stac.ItemCollection(
type="FeatureCollection", features=[stac.Item(**item_dict)]
)

def get_search(
Expand All @@ -81,30 +81,26 @@ def get_search(
datetime: Optional[Union[str, datetime]] = None,
limit: Optional[int] = 10,
**kwargs,
) -> response_model.ItemCollection:
return response_model.ItemCollection(
type="FeatureCollection", features=[response_model.Item(**item_dict)]
) -> stac.ItemCollection:
return stac.ItemCollection(
type="FeatureCollection", features=[stac.Item(**item_dict)]
)

def get_item(
self, item_id: str, collection_id: str, **kwargs
) -> response_model.Item:
return response_model.Item(**item_dict)
def get_item(self, item_id: str, collection_id: str, **kwargs) -> stac.Item:
return stac.Item(**item_dict)

def all_collections(self, **kwargs) -> response_model.Collections:
return response_model.Collections(
collections=[response_model.Collection(**collection_dict)],
def all_collections(self, **kwargs) -> stac.Collections:
return stac.Collections(
collections=[stac.Collection(**collection_dict)],
links=[
{"href": "test", "rel": "root"},
{"href": "test", "rel": "self"},
{"href": "test", "rel": "parent"},
],
)

def get_collection(
self, collection_id: str, **kwargs
) -> response_model.Collection:
return response_model.Collection(**collection_dict)
def get_collection(self, collection_id: str, **kwargs) -> stac.Collection:
return stac.Collection(**collection_dict)

def item_collection(
self,
Expand All @@ -114,9 +110,9 @@ def item_collection(
limit: int = 10,
token: str = None,
**kwargs,
) -> response_model.ItemCollection:
return response_model.ItemCollection(
type="FeatureCollection", features=[response_model.Item(**item_dict)]
) -> stac.ItemCollection:
return stac.ItemCollection(
type="FeatureCollection", features=[stac.Item(**item_dict)]
)

return CoreClient
Loading

0 comments on commit eea9c82

Please sign in to comment.