Created
June 23, 2021 14:30
-
-
Save Gaarv/8be4828b53283413fb1f72cfab2edc65 to your computer and use it in GitHub Desktop.
FastAPI prevent 307 redirect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any, Callable | |
from fastapi import APIRouter as FastAPIRouter | |
from fastapi.types import DecoratedCallable | |
class APIRouter(FastAPIRouter): | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment