From 6ee7c792fb148a860b67c015fa6f6598b25a315c Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Sun, 23 Nov 2025 22:00:48 +0100 Subject: [PATCH] better handle sync/async health check functions --- CHANGES.md | 9 ++++++- stac_fastapi/api/stac_fastapi/api/app.py | 11 +++++--- stac_fastapi/api/tests/test_app.py | 32 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a19d9eb9a..bbc7a13a4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,12 @@ ## [Unreleased] +## [6.1.1] - 2025-11-23 + +### Fixed + +- better support async/sync health check functions + ## [6.1.0] - 2025-10-30 ### Fixed @@ -666,7 +672,8 @@ Full changelog: https://stac-utils.github.io/stac-fastapi/migrations/v3.0.0/#cha * First PyPi release! -[Unreleased]: +[Unreleased]: +[6.1.1]: [6.1.0]: [6.0.0]: [5.2.1]: diff --git a/stac_fastapi/api/stac_fastapi/api/app.py b/stac_fastapi/api/stac_fastapi/api/app.py index a52343dd4..69a705a91 100644 --- a/stac_fastapi/api/stac_fastapi/api/app.py +++ b/stac_fastapi/api/stac_fastapi/api/app.py @@ -1,6 +1,7 @@ """Fastapi app creation.""" +import inspect from typing import Awaitable, Callable, Dict, List, Optional, Tuple, Type, Union import attr @@ -8,7 +9,6 @@ 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 @@ -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 @@ -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, @@ -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"]) diff --git a/stac_fastapi/api/tests/test_app.py b/stac_fastapi/api/tests/test_app.py index 5500303ef..07dccb772 100644 --- a/stac_fastapi/api/tests/test_app.py +++ b/stac_fastapi/api/tests/test_app.py @@ -1,3 +1,4 @@ +import asyncio from typing import List, Optional, Union import attr @@ -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", + }, + }