How does async __call__ work in HTTPBearer and where is it invoked? #13336
-
First Check
Commit to Help
Example Codefrom typing import Annotated
from fastapi import Depends
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
class ValidateHTTPBearer(HTTPBearer):
async def validate_parse_token(self, token: str):
...
async def validate_current_user(self, parsed_token: dict):
...
async def __call__(self, request: Request):
...
parsed_token = await self.validate_parse_token("token")
await self.validate_current_user(parsed_token)
...
WhoAreYou = Depends(ValidateHTTPBearer())
CurrentUser = Annotated[HTTPAuthorizationCredentials, WhoAreYou] DescriptionI have seen HTTPBearer class in fastapi and it uses Async call method and then use dependency injection. I have created a sub-class using HTTPBearer having additional validation logic at dependency level. Do we invoke in call that inherits async class like
If token is valid check then validate user Operating SystemLinux Operating System DetailsFedora 41 FastAPI Version0.115.7 Pydantic Version2.10 Python Version3.12 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment · 2 replies
-
The answer for the first question: FastAPI analyzes parameters of the endpoint function and executes I think it's a mistake in Regarding your second question - I didn't get it. Could you clarify? Do you want to achieve something like this?
|
Beta Was this translation helpful? Give feedback.
So, as I understand, this is what you needed: