-
Notifications
You must be signed in to change notification settings - Fork 116
Open
Description
stac-fastapi/stac_fastapi/api/stac_fastapi/api/routes.py
Lines 134 to 161 in a383f27
| def add_direct_response(app: FastAPI) -> None: | |
| """ | |
| Setup FastAPI application's endpoints to return Response Object directly, avoiding | |
| Pydantic validation and FastAPI (slow) serialization. | |
| ref: https://gist.github.com/Zaczero/00f3a2679ebc0a25eb938ed82bc63553 | |
| """ | |
| def wrap_endpoint(endpoint: Callable, cls: Type[Response]): | |
| @functools.wraps(endpoint) | |
| async def wrapper(*args, **kwargs): | |
| content = await endpoint(*args, **kwargs) | |
| return content if isinstance(content, Response) else cls(content) | |
| return wrapper | |
| for route in app.routes: | |
| if not isinstance(route, APIRoute): | |
| continue | |
| response_class = route.response_class | |
| if isinstance(response_class, DefaultPlaceholder): | |
| response_class = response_class.value | |
| if issubclass(response_class, Response): | |
| route.endpoint = wrap_endpoint(route.endpoint, response_class) | |
| route.dependant = get_dependant(path=route.path_format, call=route.endpoint) | |
| route.app = request_response(route.get_route_handler()) |
Zaczero
Metadata
Metadata
Assignees
Labels
No labels