Complete the code to import the FastAPI class.
from fastapi import [1] app = [1]()
FastAPI is the main class to create an app instance.
Complete the code to declare a dependency using Depends.
from fastapi import Depends app = FastAPI() def get_token(): return "token123" @app.get("/items") def read_items(token: str = [1](get_token)): return {"token": token}
Depends is used to declare dependencies in FastAPI.
Fix the error in the dependency injection to correctly inject the database session.
from fastapi import Depends app = FastAPI() def get_db(): db = SessionLocal() try: yield db finally: db.close() @app.get("/users") def read_users(db: Session = [1](get_db)): return db.query(User).all()
Depends is required to inject the get_db dependency properly.
Fill both blanks to create a reusable dependency and inject it into the route.
from fastapi import Depends app = FastAPI() def common_parameters(q: str = None, limit: int = 10): return {"q": q, "limit": limit} @app.get("/search") def search(params: dict = [1](common_parameters)): return params if params[[2]] else {"limit": 10}
Depends injects the common_parameters dependency. The key "q" is used to check the query.
Fill all three blanks to declare a dependency with parameters and use it in a route.
from fastapi import Depends app = FastAPI() def pagination(skip: int = 0, limit: int = 10): return {"skip": skip, "limit": limit} @app.get("/items") def get_items(p: dict = [1](pagination)): return {"skip": p[[2]], "limit": p[[3]]}
Depends injects the pagination dependency. The keys "skip" and "limit" access pagination values.
