0
0
FastAPIframework~10 mins

Sub-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 simple dependency function in FastAPI.

FastAPI
from fastapi import Depends, FastAPI

app = FastAPI()

def common_parameters():
    return {"q": "fastapi"}

@app.get("/items/")
async def read_items(params: dict = Depends([1])):
    return params
Drag options to blanks, or click blank then click option'
Acommon_parameters
BDepends
CFastAPI
Dread_items
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Depends instead of the function name.
Passing the FastAPI app instance.
Using the endpoint function name as dependency.
2fill in blank
medium

Complete the code to declare a sub-dependency function that depends on another dependency.

FastAPI
from fastapi import Depends, FastAPI

app = FastAPI()

def common_parameters():
    return {"q": "fastapi"}

def query_extractor(params: dict = Depends([1])):
    return params.get("q")

@app.get("/search/")
async def search(q: str = Depends(query_extractor)):
    return {"query": q}
Drag options to blanks, or click blank then click option'
AFastAPI
Bcommon_parameters
Csearch
Dquery_extractor
Attempts:
3 left
💡 Hint
Common Mistakes
Using the sub-dependency function itself inside Depends().
Passing the endpoint function name as dependency.
Passing the FastAPI app instance.
3fill in blank
hard

Fix the error in the sub-dependency declaration by completing the blank.

FastAPI
from fastapi import Depends, FastAPI

app = FastAPI()

def common_parameters():
    return {"q": "fastapi"}

def query_extractor(params: dict = Depends([1])):
    return params.get("q")

@app.get("/items/")
async def read_items(q: str = Depends(query_extractor)):
    return {"query": q}
Drag options to blanks, or click blank then click option'
Aread_items
Bquery_extractor
Ccommon_parameters
DFastAPI
Attempts:
3 left
💡 Hint
Common Mistakes
Referencing the sub-dependency itself inside Depends().
Using the endpoint function name inside Depends().
4fill in blank
hard

Fill both blanks to create a sub-dependency that extracts a header and then use it in the endpoint.

FastAPI
from fastapi import Depends, FastAPI, Header

app = FastAPI()

def user_agent_header(user_agent: str = [1]):
    return user_agent

@app.get("/headers/")
async def read_headers(user_agent: str = Depends([2])):
    return {"User-Agent": user_agent}
Drag options to blanks, or click blank then click option'
AHeader("User-Agent")
Buser_agent_header
CDepends(user_agent_header)
DHeader
Attempts:
3 left
💡 Hint
Common Mistakes
Using Depends() inside the sub-dependency parameter default.
Passing Depends(user_agent_header) instead of the function name in the endpoint.
5fill in blank
hard

Fill all three blanks to create nested dependencies where the endpoint depends on a sub-dependency that depends on another dependency.

FastAPI
from fastapi import Depends, FastAPI

app = FastAPI()

def common_parameters():
    return {"token": "abc123"}

def verify_token(params: dict = Depends([1])):
    token = params.get([2])
    if token != "abc123":
        raise Exception("Invalid token")
    return token

@app.get("/secure-data/")
async def secure_data(token: str = Depends([3])):
    return {"token": token}
Drag options to blanks, or click blank then click option'
Acommon_parameters
B"token"
Cverify_token
Dsecure_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using the endpoint function name inside Depends().
Using the wrong key string for the token.
Referencing the sub-dependency inside itself.