I want to add an auth_required
decorator to my endpoints.
(Please consider that this question is about decorators, not middleware)
So a simple decorator looks like this:
def auth_required(func):
def wrapper(*args, **kwargs):
if user_ctx.get() is None:
raise HTTPException(...)
return func(*args, **kwargs)
return wrapper
So there are 2 usages:
@auth_required
@router.post(...)
or
@router.post(...)
@auth_required
The first way doesn't work because router.post
creates a router that saved into self.routes
of APIRouter object. The second way doesn't work because it fails to verify pydantic object. For any request model, it says missing args, missing kwargs
.
So my question is - how can I add any decorators to FastAPI endpoints? Should I get into router.routes
and modify the existing endpoint? Or use some functools.wraps
like functions?
How can I add any decorators to FastAPI endpoints?
As you said, you need to use @functools.wraps(...)
--(PyDoc) decorator as,
from functools import wraps
from fastapi import FastAPI
from pydantic import BaseModel
class SampleModel(BaseModel):
name: str
age: int
app = FastAPI()
def auth_required(func):
@wraps(func)
async def wrapper(*args, **kwargs):
return await func(*args, **kwargs)
return wrapper
@app.post("/")
@auth_required # Custom decorator
async def root(payload: SampleModel):
return {"message": "Hello World", "payload": payload}
The main caveat of this method is that you can't access the request
object in the wrapper and I assume it is your primary intention.
If you need to access the request, you must add the argument to the router function as,
from fastapi import Request
@app.post("/")
@auth_required # Custom decorator
async def root(request: Request, payload: SampleModel):
return {"message": "Hello World", "payload": payload}
I am not sure what's wrong with the FastAPI middleware, after all, the @app.middleware(...)
is also a decorator.