0
0
FastAPIframework~10 mins

Class-based 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 class-based dependency in FastAPI.

FastAPI
from fastapi import Depends, FastAPI

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

app = FastAPI()

@app.get("/items/")
async def read_items(commons: CommonQueryParams = Depends([1])):
    return {"q": commons.q}
Drag options to blanks, or click blank then click option'
ADepends(CommonQueryParams)
BCommonQueryParams()
CCommonQueryParams
DDepends(CommonQueryParams())
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an instance of the class instead of the class itself.
Wrapping Depends around an instance instead of the class.
2fill in blank
medium

Complete the code to inject a class-based dependency into a path operation.

FastAPI
from fastapi import Depends, FastAPI

class User:
    def __init__(self, username: str):
        self.username = username

app = FastAPI()

@app.get("/users/me")
async def read_user(user: User = Depends([1])):
    return {"username": user.username}
Drag options to blanks, or click blank then click option'
AUser
BUser()
CDepends(User())
DDepends(User)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an instance instead of the class.
Using Depends with an instance instead of the class.
3fill in blank
hard

Fix the error in the class-based dependency usage.

FastAPI
from fastapi import Depends, FastAPI

class Settings:
    def __init__(self):
        self.debug = True

app = FastAPI()

@app.get("/settings")
async def get_settings(settings: Settings = Depends([1])):
    return {"debug": settings.debug}
Drag options to blanks, or click blank then click option'
ASettings
BSettings()
CDepends(Settings())
DDepends(Settings)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an instance instead of the class.
Wrapping Depends around an instance.
4fill in blank
hard

Fill both blanks to create a class-based dependency that accepts a query parameter and use it in a path operation.

FastAPI
from fastapi import Depends, FastAPI

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

app = FastAPI()

@app.get("/search")
async def search(params: QueryParams = Depends([1])):
    return {"query": params.[2]
Drag options to blanks, or click blank then click option'
AQueryParams
Bquery
Cq
DQueryParams()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an instance instead of the class.
Using the wrong attribute name for the query parameter.
5fill in blank
hard

Fill all three blanks to create a class-based dependency with two parameters and use them in a path operation.

FastAPI
from fastapi import Depends, FastAPI

class Pagination:
    def __init__(self, skip: int = 0, limit: int = 10):
        self.skip = skip
        self.limit = limit

app = FastAPI()

@app.get("/items")
async def list_items(pagination: Pagination = Depends([1])):
    return {"skip": pagination.[2], "limit": pagination.[3]
Drag options to blanks, or click blank then click option'
APagination
Bskip
Climit
DPagination()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an instance instead of the class.
Using incorrect attribute names for pagination.