Complete the code to declare a dependency function with a parameter.
from fastapi import Depends, FastAPI app = FastAPI() def common_parameters(q: str = [1]): return {"q": q} @app.get("/items/") async def read_items(commons: dict = Depends(common_parameters)): return commons
The parameter q has a default value of None, making it optional.
Complete the code to inject a dependency with a parameter into a path operation.
from fastapi import Depends, FastAPI app = FastAPI() def query_extractor(q: str = None): return q @app.get("/search/") async def search(q: str = Depends([1])): return {"query": q}
The dependency function query_extractor is passed to Depends to inject its result.
Fix the error in the dependency function parameter default value.
from fastapi import Depends, FastAPI app = FastAPI() def pagination(skip: int = [1]): return {"skip": skip} @app.get("/items/") async def list_items(pagination: dict = Depends(pagination)): return pagination
The default value for skip should be an integer 0, not a string.
Fill both blanks to create a dependency with two parameters and use it in a path operation.
from fastapi import Depends, FastAPI app = FastAPI() def pagination(skip: int = 0, limit: int = [1]): return {"skip": skip, "limit": limit} @app.get("/items/") async def list_items(pagination: dict = Depends([2])): return pagination
The default limit is set to 10, and the dependency function pagination is passed to Depends.
Fill all three blanks to create a dependency with parameters and use it with type hints in the path operation.
from fastapi import Depends, FastAPI app = FastAPI() def query_params(q: str = None, skip: int = 0, limit: int = [1]): return {"q": q, "skip": skip, "limit": limit} @app.get("/search/") async def search_items(params: dict = Depends([2])) -> [3]: return params
The default limit is 20, the dependency function is query_params, and the return type hint is dict.