Skip to content

Commit d70174d

Browse files
better handle sync/async health check functions (#865)
1 parent 17a1a49 commit d70174d

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

CHANGES.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [Unreleased]
44

5+
## [6.1.1] - 2025-11-23
6+
7+
### Fixed
8+
9+
- better support async/sync health check functions
10+
511
## [6.1.0] - 2025-10-30
612

713
### Fixed
@@ -666,7 +672,8 @@ Full changelog: https://stac-utils.github.io/stac-fastapi/migrations/v3.0.0/#cha
666672

667673
* First PyPi release!
668674

669-
[Unreleased]: <https://github.com/stac-utils/stac-fastapi/compare/6.1.0..main>
675+
[Unreleased]: <https://github.com/stac-utils/stac-fastapi/compare/6.1.1..main>
676+
[6.1.1]: <https://github.com/stac-utils/stac-fastapi/compare/6.1.0..6.1.1>
670677
[6.1.0]: <https://github.com/stac-utils/stac-fastapi/compare/6.0.0..6.1.0>
671678
[6.0.0]: <https://github.com/stac-utils/stac-fastapi/compare/5.2.1..6.0.0>
672679
[5.2.1]: <https://github.com/stac-utils/stac-fastapi/compare/5.2.0..5.2.1>

stac_fastapi/api/stac_fastapi/api/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Fastapi app creation."""
22

33

4+
import inspect
45
from typing import Awaitable, Callable, Dict, List, Optional, Tuple, Type, Union
56

67
import attr
78
from brotli_asgi import BrotliMiddleware
89
from fastapi import APIRouter, FastAPI
910
from fastapi.params import Depends
1011
from stac_pydantic import api
11-
from stac_pydantic.api.collections import Collections
1212
from stac_pydantic.shared import MimeTypes
1313
from stac_pydantic.version import STAC_VERSION
1414
from starlette.middleware import Middleware
@@ -32,6 +32,7 @@
3232
add_direct_response,
3333
add_route_dependencies,
3434
create_async_endpoint,
35+
sync_to_async,
3536
)
3637
from stac_fastapi.types.config import ApiSettings, Settings
3738
from stac_fastapi.types.core import AsyncBaseCoreClient, BaseCoreClient
@@ -277,14 +278,14 @@ def register_get_collections(self) -> None:
277278
name="Get Collections",
278279
path="/collections",
279280
response_model=(
280-
Collections if self.settings.enable_response_models else None
281+
api.Collections if self.settings.enable_response_models else None
281282
),
282283
responses={
283284
200: {
284285
"content": {
285286
MimeTypes.json.value: {},
286287
},
287-
"model": Collections,
288+
"model": api.Collections,
288289
},
289290
},
290291
response_class=self.response_class,
@@ -411,7 +412,9 @@ async def ping():
411412
},
412413
response_class=self.response_class,
413414
methods=["GET"],
414-
endpoint=self.health_check,
415+
endpoint=self.health_check
416+
if inspect.iscoroutinefunction(self.health_check)
417+
else sync_to_async(self.health_check),
415418
)
416419
self.app.include_router(mgmt_router, tags=["Liveliness/Readiness"])
417420

stac_fastapi/api/tests/test_app.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
from typing import List, Optional, Union
23

34
import attr
@@ -578,3 +579,34 @@ def health_check(request: Request):
578579
"version": "0.1.0",
579580
},
580581
}
582+
583+
async def async_health_check(request: Request):
584+
await asyncio.sleep(1)
585+
return {
586+
"status": "UP",
587+
"database": {
588+
"status": "UP",
589+
"version": "0.1.0",
590+
},
591+
}
592+
593+
test_app = app.StacApi(
594+
settings=ApiSettings(),
595+
client=AsyncTestCoreClient(),
596+
health_check=async_health_check,
597+
)
598+
599+
with TestClient(test_app.app) as client:
600+
resp = client.get("/_mgmt/ping")
601+
assert resp.status_code == 200
602+
assert resp.json() == {"message": "PONG"}
603+
604+
resp = client.get("/_mgmt/health")
605+
assert resp.status_code == 200
606+
assert resp.json() == {
607+
"status": "UP",
608+
"database": {
609+
"status": "UP",
610+
"version": "0.1.0",
611+
},
612+
}

0 commit comments

Comments
 (0)