Skip to content

Commit

Permalink
forward StacApi.title, StacApi.api_version and `Stac.Api.descript…
Browse files Browse the repository at this point in the history
…ion` to the FastAPI application
  • Loading branch information
vincentsarago committed Jan 30, 2025
1 parent 2fbcfc0 commit 813ec91
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
- added `query.QueryConformanceClasses` Enum
- added `SortConformanceClasses` Enum

- removed `StacApi.customize_openapi` method
- reordered `StacApi` attributes (moved `title`, `api_version` and `description` before `app`)

### Added

* forward `StacApi.title`, `StacApi.api_version` and `Stac.Api.description` to the FastAPI application

## [4.0.1] - 2025-01-23

### Changed
Expand Down
51 changes: 17 additions & 34 deletions stac_fastapi/api/stac_fastapi/api/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Fastapi app creation."""


from typing import Any, Dict, List, Optional, Tuple, Type, Union
from typing import Dict, List, Optional, Tuple, Type, Union

import attr
from brotli_asgi import BrotliMiddleware
from fastapi import APIRouter, FastAPI
from fastapi.openapi.utils import get_openapi
from fastapi.params import Depends
from stac_pydantic import api
from stac_pydantic.api.collections import Collections
Expand Down Expand Up @@ -71,19 +70,6 @@ class StacApi:
exceptions: Dict[Type[Exception], int] = attr.ib(
default=attr.Factory(lambda: DEFAULT_STATUS_CODES)
)
app: FastAPI = attr.ib(
default=attr.Factory(
lambda self: FastAPI(
openapi_url=self.settings.openapi_url,
docs_url=self.settings.docs_url,
redoc_url=None,
root_path=self.settings.root_path,
),
takes_self=True,
),
converter=update_openapi,
)
router: APIRouter = attr.ib(default=attr.Factory(APIRouter))
title: str = attr.ib(
default=attr.Factory(
lambda self: self.settings.stac_fastapi_title, takes_self=True
Expand All @@ -100,6 +86,22 @@ class StacApi:
lambda self: self.settings.stac_fastapi_description, takes_self=True
)
)
app: FastAPI = attr.ib(
default=attr.Factory(
lambda self: FastAPI(
openapi_url=self.settings.openapi_url,
docs_url=self.settings.docs_url,
redoc_url=None,
root_path=self.settings.root_path,
title=self.title,
version=self.api_version,
description=self.description,
),
takes_self=True,
),
converter=update_openapi,
)
router: APIRouter = attr.ib(default=attr.Factory(APIRouter))
search_get_request_model: Type[BaseSearchGetRequest] = attr.ib(
default=BaseSearchGetRequest
)
Expand Down Expand Up @@ -388,22 +390,6 @@ def register_core(self):
self.register_get_collection()
self.register_get_item_collection()

def customize_openapi(self) -> Optional[Dict[str, Any]]:
"""Customize openapi schema."""
if self.app.openapi_schema:
return self.app.openapi_schema

openapi_schema = get_openapi(
title=self.title,
version=self.api_version,
description=self.description,
routes=self.app.routes,
servers=self.app.servers,
)

self.app.openapi_schema = openapi_schema
return self.app.openapi_schema

def add_health_check(self):
"""Add a health check."""
mgmt_router = APIRouter(prefix=self.app.state.router_prefix)
Expand Down Expand Up @@ -467,9 +453,6 @@ def __attrs_post_init__(self):
# register exception handlers
add_exception_handlers(self.app, status_codes=self.exceptions)

# customize openapi
self.app.openapi = self.customize_openapi

# add middlewares
if self.middlewares and self.app.middleware_stack is not None:
raise RuntimeError("Cannot add middleware after an application has started")
Expand Down
27 changes: 27 additions & 0 deletions stac_fastapi/api/tests/test_app_prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,30 @@ def test_api_prefix_with_root_path(TestCoreClient, prefix):

resp = client.get(prefix + expected_path)
assert resp.status_code == 200


def test_api_fastapi_option(TestCoreClient):
api_settings = ApiSettings(
stac_fastapi_title="stac-fastapi-tests",
stac_fastapi_description="test for stac-fastapi",
stac_fastapi_version="0.1.0dev",
)

api = StacApi(
settings=api_settings,
client=TestCoreClient(),
)

with TestClient(api.app, base_url="http://stac.io") as client:
landing = client.get("/")
assert landing.status_code == 200
body = landing.json()
assert body["title"] == "stac-fastapi-tests"
assert body["description"] == "test for stac-fastapi"

service_desc = client.get("/api")
assert service_desc.status_code == 200
body = service_desc.json()["info"]
assert body["title"] == "stac-fastapi-tests"
assert body["description"] == "test for stac-fastapi"
assert body["version"] == "0.1.0dev"

0 comments on commit 813ec91

Please sign in to comment.