Reputation: 13
I'm trying to access my database with fast API. I'm having some problems getting access to it if done it outside of an endpoint.
I'm trying to access the DB and retrieve the allowed emails. I can do that in the get/post (/refresh) endpoints, but not in the route (/token), it throws the error: ''Depends' object has no attribute 'query''
From what I understand the depends can only be solved in an API route, so I tried to create a new post endpoint and call it from the route. The result is that the request gets stuck...
@router.route('/login')
async def login(request: Request):
#REQUEST GET STUCK IN PENDING
await requests.post(url="http://localhost:8000/auth" + '/set-users')
redirect_uri = FRONTEND_URL # This creates the url for our /auth endpoint
return await oauth.google.authorize_redirect(request, redirect_uri)
@router.post('/set-users')
def set_useres(database: Session = Depends(db_handler.get_db)):
emails = brand_operation_handler.get_all_users(db=database)
print(emails)
allowed_email_in_user_table_setter(emails)
return ''
@router.route('/token')
async def auth(request: Request, database: Session = Depends(db_handler.get_db)): #, database: Session = Depends(db_handler.get_db)
try:
access_token = await oauth.google.authorize_access_token(request)
except OAuthError:
raise CREDENTIALS_EXCEPTION
user_data = await oauth.google.parse_id_token(request, access_token)
#emails = brand_operation_handler.get_all_users(db=database)
# allowed_email_in_user_table_setter(emails)
if (valid_email_from_db(user_data['email'])): #or valid_email_from_user_table(user_data['email'])
return JSONResponse({
'result': True,
'access_token': create_token(user_data['email']),
'refresh_token': create_refresh_token(user_data['email']),
'user_data': user_data
})
raise CREDENTIALS_EXCEPTION
@router.post('/refresh')
async def refresh(request: Request, database: Session = Depends(db_handler.get_db)):
#THIS WORKS
emails = brand_operation_handler.get_all_users(db=database)
print(emails)
allowed_email_in_user_table_setter(emails)
try:
# Only accept post requests
if request.method == 'POST':
form = await request.json()
if form.get('grant_type') == 'refresh_token':
token = form.get('refresh_token')
payload = decode_token(token)
# Check if token is not expired
if datetime.utcfromtimestamp(payload.get('exp')) > datetime.utcnow():
email = payload.get('sub')
# Validate email
# database: Session = Depends(db_handler.get_db)
# get_user_table(database)
if valid_email_from_db(email): #or valid_email_from_user_table(email)
# Create and return token
return JSONResponse({'result': True, 'access_token': create_token(email)})
except Exception:
raise CREDENTIALS_EXCEPTION
raise CREDENTIALS_EXCEPTION
Upvotes: 1
Views: 1771
Reputation: 3011
The reason Depends
is not working is, as @MatsLindh is asking about, that you are calling router.route
directly. That is the underlying method in Starlette, meaning you bypass all FastAPI specific functionality, like Depends
. Rewrite it using router.get
and it should work.
Upvotes: 0