-
First Check
Commit to Help
Example Codeclass APIRouter(FastAPIRouter):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.route_class = LoggingRoute
def api_route(
self, path: str, *, include_in_schema: bool = True, **kwargs: Any
) -> Callable[[DecoratedCallable], DecoratedCallable]:
if path.endswith("/"):
path = path[:-1]
add_path = super().api_route(
path, include_in_schema=include_in_schema, **kwargs
)
alternate_path = path + "/"
add_alternate_path = super().api_route(
alternate_path, include_in_schema=False, **kwargs
)
def decorator(func: DecoratedCallable) -> DecoratedCallable:
add_alternate_path(func)
return add_path(func)
return decorator
class LoggingRoute(APIRoute):
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()
async def custom_route_handler(request: Request) -> Response:
#send json input body to s3 ... in a background task from here
response = await original_route_handler(request)
#send json output body to s3 ... in a background task from here DescriptionHey all, In my fastapi application, I have a custom API Router that I use to support the trailing slash (code found from the community). Thanks for your help Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.95.2 Pydantic Version1.10.2 Python Version3.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
What is the type of You may be able to do it like starlette describes here https://www.starlette.io/background/ |
Beta Was this translation helpful? Give feedback.
What is the type of
response
in your custom route handler?You may be able to do it like starlette describes here https://www.starlette.io/background/