In my application, I have started using FastAPI Custom Routes to log requests and responses and add transactional headers. However, I can't use the new route class for the SQLAdmin routes because I cannot set the route class.
The ability to pass route_class
as an argument to the Admin.__init__()
method would be a nice way to fix this issue.
I have experimented with this and I have found that while it "works" for the most part, it causes an issue with the login_required
decorator.
def login_required(func: Callable[..., Any]) -> Callable[..., Any]:
"""Decorator to check authentication of Admin routes.
If no authentication backend is setup, this will do nothing.
"""
@functools.wraps(func)
async def wrapper_decorator(*args: Any, **kwargs: Any) -> Any:
admin, request = args[0], args[1]
auth_backend = admin.authentication_backend
if auth_backend is not None:
response = await auth_backend.authenticate(request)
if response and isinstance(response, Response):
return response
return await func(*args, **kwargs)
return wrapper_decorator
The decorator expects two args
but when using subclasses of fastapi.APIRoute
, the request
argument is passed as a kwarg
instead.
I previously used custom middleware to achieve this. I am trying to move away from it because it caused a few issues with asynchronous execution of Background Tasks. For now, I have kept the middleware just on the SQLAdmin router. This allows me to still use background tasks elsewhere and keep logs of SQLAdmin calls. I do have to support two logging mechanisms, which isn't ideal.
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too