class User(BaseModel):
name: str
token: str
fake_db = [
User(name='foo', token='a1'),
User(name='bar', token='a2')
]
async def get_user_by_token(token: str = Header()):
for user in fake_db:
if user.token == token:
return user
else:
raise HTTPException(status_code=401, detail='Invalid token')
@router.get(path='/test_a', summary='Test route A')
async def test_route_a(user: User = Depends(get_user_by_token)):
return {'name': user.name}
@router.get(path='/test_b', summary='Test route B')
async def test_route_a(user: User = Depends(get_user_by_token)):
return {'name': user.name}
I would like to avoid code duplication. Is it possible to somehow set the line user: User = Depends(get_user_by_token)
for the entire router? At the same time, I need the user
object to be available in each method.
It is very important that the openapi says that you need to specify a header with a token for the method.