0
0
FastAPIframework~10 mins

Shared dependencies across routers 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 import the FastAPI class.

FastAPI
from fastapi import [1]
app = [1]()
Drag options to blanks, or click blank then click option'
AAPIRouter
BDepends
CFastAPI
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Depends instead of FastAPI
Using APIRouter to create the main app
2fill in blank
medium

Complete the code to define a dependency function that returns a string.

FastAPI
def common_dependency():
    return [1]
Drag options to blanks, or click blank then click option'
A'shared value'
BTrue
CNone
D42
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a number instead of a string
Returning None which is not useful here
3fill in blank
hard

Fix the error in the router dependency declaration.

FastAPI
from fastapi import APIRouter, Depends

router = APIRouter(
    dependencies=[Depends([1])]
)
Drag options to blanks, or click blank then click option'
Aapp
Bcommon_dependency
CFastAPI
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the app instance instead of the dependency function
Passing a class like FastAPI instead of a function
4fill in blank
hard

Fill both blanks to create a route that uses the shared dependency and returns its value.

FastAPI
@router.get("/shared")
async def read_shared(value: str = [1]([2])):
    return {"value": value}
Drag options to blanks, or click blank then click option'
ADepends
Bcommon_dependency
CRequest
DFastAPI
Attempts:
3 left
💡 Hint
Common Mistakes
Using Request or FastAPI instead of Depends
Not passing the dependency function to Depends
5fill in blank
hard

Fill all three blanks to include the router in the app with a prefix and shared dependencies.

FastAPI
app.include_router(
    router,
    prefix=[1],
    dependencies=[[2]([3])]
)
Drag options to blanks, or click blank then click option'
A"/items"
BDepends
Ccommon_dependency
D"/users"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong prefix string
Not wrapping the dependency function with Depends