0
0
FastAPIframework~10 mins

Global dependencies in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a global dependency in FastAPI.

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI(dependencies=[Depends([1])])

def common_dependency():
    return "common value"
Drag options to blanks, or click blank then click option'
ADepends
Bapp
CFastAPI
Dcommon_dependency
Attempts:
3 left
💡 Hint
Common Mistakes
Using the app instance name instead of the dependency function.
Passing Depends without a function inside.
Confusing FastAPI class with the dependency function.
2fill in blank
medium

Complete the code to add a global dependency that returns a header value.

FastAPI
from fastapi import FastAPI, Depends, Header, HTTPException

app = FastAPI(dependencies=[Depends([1])])

def get_token_header(x_token: str = Header(...)):
    if x_token != "fake-super-secret-token":
        raise HTTPException(status_code=400, detail="Invalid X-Token")
    return x_token
Drag options to blanks, or click blank then click option'
Aget_token_header
BHeader
CHTTPException
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Header or Depends instead of the function name.
Calling the function instead of passing it as a reference.
3fill in blank
hard

Fix the error in the global dependency declaration.

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI(dependencies=[1])

def verify_user():
    pass
Drag options to blanks, or click blank then click option'
ADepends(verify_user)
B[Depends(verify_user)]
C{Depends(verify_user)}
Dverify_user
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Depends() directly without a list.
Using curly braces instead of square brackets.
Passing the function name without Depends().
4fill in blank
hard

Fill both blanks to create a global dependency that logs each request.

FastAPI
from fastapi import FastAPI, Depends
import logging

logging.basicConfig(level=logging.INFO)

def log_request():
    logging.info("Request received")

app = FastAPI(dependencies=[Depends([1])])

@app.get("/items/")
def read_items(log=Depends([2])):
    return {"message": "Items list"}
Drag options to blanks, or click blank then click option'
Alog_request
Blogging
CDepends
Dread_items
Attempts:
3 left
💡 Hint
Common Mistakes
Using Depends instead of the function name.
Using different function names in the two places.
Passing the function call instead of the function reference.
5fill in blank
hard

Fill all three blanks to create a global dependency that checks a token and a user role.

FastAPI
from fastapi import FastAPI, Depends, Header, HTTPException

app = FastAPI(dependencies=[Depends([1])])

def verify_token(x_token: str = Header(...)):
    if x_token != "secret-token":
        raise HTTPException(status_code=400, detail="Invalid token")
    return x_token

def verify_role(token: str = Depends(verify_token)):
    if token != "secret-token":
        raise HTTPException(status_code=403, detail="Not authorized")
    return True

@app.get("/secure-data")
def secure_data(role=Depends([2])):
    return {"access": "granted"}

app.dependencies = [Depends([3])]
Drag options to blanks, or click blank then click option'
Averify_token
Bverify_role
CDepends
DHTTPException
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up verify_token and verify_role in blanks.
Using Depends or HTTPException instead of function names.
Not matching the dependency chain correctly.