0
0
FastAPIframework~10 mins

Shared 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 shared dependency function.

FastAPI
from fastapi import Depends

def get_db():
    db = "database connection"
    try:
        yield db
    finally:
        print("Close connection")

async def read_items(db = Depends([1])):
    return {"db": db}
Drag options to blanks, or click blank then click option'
Aread_items
BDepends
Cget_db
Ddb
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Depends instead of the function name.
Using the variable name instead of the function name.
2fill in blank
medium

Complete the code to use the shared dependency in two endpoints.

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def get_token():
    return "token123"

@app.get("/items/")
async def read_items(token = Depends([1])):
    return {"token": token}

@app.get("/users/")
async def read_users(token = Depends(get_token)):
    return {"token": token}
Drag options to blanks, or click blank then click option'
Aread_items
BDepends
Ctoken
Dget_token
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Depends instead of the function name.
Using the variable name instead of the function name.
3fill in blank
hard

Fix the error in the shared dependency usage.

FastAPI
from fastapi import Depends

def common_parameters(q: str = None):
    return {"q": q}

async def read_items(params = Depends([1])):
    return params
Drag options to blanks, or click blank then click option'
Acommon_parameters
Bcommon_parameters()
CDepends
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the function inside Depends with parentheses.
Passing Depends itself instead of the function.
4fill in blank
hard

Fill both blanks to create a shared dependency with a class and use it in an endpoint.

FastAPI
from fastapi import Depends, FastAPI

class CommonQuery:
    def __init__(self, q: str = None):
        self.q = q

app = FastAPI()

@app.get("/items/")
async def read_items(commons: CommonQuery = Depends([1])):
    return {"query": commons.[2]
Drag options to blanks, or click blank then click option'
ACommonQuery
BDepends
Cq
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Depends instead of the class name.
Accessing a wrong attribute name.
5fill in blank
hard

Fill all three blanks to create a shared dependency with a function and use it in two endpoints.

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def get_token_header(x_token: str = None):
    if x_token != "fake-super-secret-token":
        raise Exception("Invalid token")
    return x_token

@app.get("/items/")
async def read_items(token: str = Depends([1])):
    return {"token": token}

@app.get("/users/")
async def read_users(token: str = Depends([2])):
    return {"token": token}

# Use the shared dependency function [3] in both endpoints
Drag options to blanks, or click blank then click option'
Aget_token_header
BDepends
Dread_items
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Depends instead of the function name.
Using different or wrong function names.