Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- better support async/sync health check functions

### Removed

- support for python 3.9 and 3.10
Expand Down
11 changes: 7 additions & 4 deletions stac_fastapi/api/stac_fastapi/api/app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Fastapi app creation."""


import inspect
from typing import Awaitable, Callable, Dict, List, Optional, Tuple, Type, Union

import attr
from brotli_asgi import BrotliMiddleware
from fastapi import APIRouter, FastAPI
from fastapi.params import Depends
from stac_pydantic import api
from stac_pydantic.api.collections import Collections
from stac_pydantic.shared import MimeTypes
from stac_pydantic.version import STAC_VERSION
from starlette.middleware import Middleware
Expand All @@ -32,6 +32,7 @@
add_direct_response,
add_route_dependencies,
create_async_endpoint,
sync_to_async,
)
from stac_fastapi.types.config import ApiSettings, Settings
from stac_fastapi.types.core import AsyncBaseCoreClient, BaseCoreClient
Expand Down Expand Up @@ -277,14 +278,14 @@ def register_get_collections(self) -> None:
name="Get Collections",
path="/collections",
response_model=(
Collections if self.settings.enable_response_models else None
api.Collections if self.settings.enable_response_models else None
),
responses={
200: {
"content": {
MimeTypes.json.value: {},
},
"model": Collections,
"model": api.Collections,
},
},
response_class=self.response_class,
Expand Down Expand Up @@ -411,7 +412,9 @@ async def ping():
},
response_class=self.response_class,
methods=["GET"],
endpoint=self.health_check,
endpoint=self.health_check
if inspect.iscoroutinefunction(self.health_check)
else sync_to_async(self.health_check),
)
self.app.include_router(mgmt_router, tags=["Liveliness/Readiness"])

Expand Down
32 changes: 32 additions & 0 deletions stac_fastapi/api/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from typing import List, Optional, Union

import attr
Expand Down Expand Up @@ -578,3 +579,34 @@ def health_check(request: Request):
"version": "0.1.0",
},
}

async def async_health_check(request: Request):
await asyncio.sleep(1)
return {
"status": "UP",
"database": {
"status": "UP",
"version": "0.1.0",
},
}

test_app = app.StacApi(
settings=ApiSettings(),
client=AsyncTestCoreClient(),
health_check=async_health_check,
)

with TestClient(test_app.app) as client:
resp = client.get("/_mgmt/ping")
assert resp.status_code == 200
assert resp.json() == {"message": "PONG"}

resp = client.get("/_mgmt/health")
assert resp.status_code == 200
assert resp.json() == {
"status": "UP",
"database": {
"status": "UP",
"version": "0.1.0",
},
}