|
|
@ -35,6 +35,7 @@ from starlette.applications import Starlette |
|
|
|
from starlette.datastructures import State |
|
|
|
from starlette.exceptions import HTTPException |
|
|
|
from starlette.middleware import Middleware |
|
|
|
from starlette.middleware.base import BaseHTTPMiddleware |
|
|
|
from starlette.middleware.errors import ServerErrorMiddleware |
|
|
|
from starlette.middleware.exceptions import ExceptionMiddleware |
|
|
|
from starlette.requests import Request |
|
|
@ -870,3 +871,35 @@ class FastAPI(Starlette): |
|
|
|
openapi_extra=openapi_extra, |
|
|
|
generate_unique_id_function=generate_unique_id_function, |
|
|
|
) |
|
|
|
|
|
|
|
def websocket_route( |
|
|
|
self, path: str, name: Union[str, None] = None |
|
|
|
) -> Callable[[DecoratedCallable], DecoratedCallable]: |
|
|
|
def decorator(func: DecoratedCallable) -> DecoratedCallable: |
|
|
|
self.router.add_websocket_route(path, func, name=name) |
|
|
|
return func |
|
|
|
|
|
|
|
return decorator |
|
|
|
|
|
|
|
def on_event( |
|
|
|
self, event_type: str |
|
|
|
) -> Callable[[DecoratedCallable], DecoratedCallable]: |
|
|
|
return self.router.on_event(event_type) |
|
|
|
|
|
|
|
def middleware( |
|
|
|
self, middleware_type: str |
|
|
|
) -> Callable[[DecoratedCallable], DecoratedCallable]: |
|
|
|
def decorator(func: DecoratedCallable) -> DecoratedCallable: |
|
|
|
self.add_middleware(BaseHTTPMiddleware, dispatch=func) |
|
|
|
return func |
|
|
|
|
|
|
|
return decorator |
|
|
|
|
|
|
|
def exception_handler( |
|
|
|
self, exc_class_or_status_code: Union[int, Type[Exception]] |
|
|
|
) -> Callable[[DecoratedCallable], DecoratedCallable]: |
|
|
|
def decorator(func: DecoratedCallable) -> DecoratedCallable: |
|
|
|
self.add_exception_handler(exc_class_or_status_code, func) |
|
|
|
return func |
|
|
|
|
|
|
|
return decorator |
|
|
|