-
Create a virtual environment:
python -m venv .venv
Activate the virtual environment:
- On macOS/Linux:
source .venv/bin/activate- On Windows use
.venv\Scripts\activate
-
Install FastAPI and Uvicorn:
pip install fastapi uvicorn
-
Run the FastAPI app:
uvicorn main:app --reload
-
File:
main.pyfrom fastapi import FastAPI, Depends, Query from typing import Optional from fastapi.exceptions import HTTPException app = FastAPI() # Dependency function def get_query_param(q: Optional[str] = None): return q # Endpoint using the dependency @app.get("/search") def search(query: str = Depends(get_query_param)): return {"query": query}
-
Questions
-
Question 1.1: What will be the response of the following request?
GET /search?q=FastAPI -
Question 1.2: What will be returned if no q parameter is provided?
-
Question 1.3: Change the
get_query_paramfunction so that it raises an HTTPException if the q parameter is missing.
-
-
File:
main.pydef get_fake_db(): yield {"users": ["Alice", "Bob", "Charlie"]} @app.get("/users") def read_users(db: dict = Depends(get_fake_db)): return {"users": db["users"]}
-
Questions:
-
Question 2.1: Explain what
yielddoes in this context. -
Question 2.2: Refactor
get_fake_db()so it logs "Opening DB" before yielding and "Closing DB" after.Hint: Use try/finally.
-
-
File:
main.pydef get_current_user(token: str = Query(...)): if token == "secret": return "authenticated_user" raise HTTPException(status_code=401, detail="Invalid or missing token") @app.get("/profile") def read_profile(user: str = Depends(get_current_user)): return {"user": user}
-
Questions:
-
Question 3.1: What will the endpoint
/profilereturn if called with:GET /profile?token=secret -
Question 3.2: What if token is missing or incorrect?
-
Question 3.3: Change the function to accept tokens only from a custom header "X-Token" instead of a query parameter.
-
- Files:
-
routers/users.pyfrom fastapi import APIRouter, Depends from dependencies.auth import get_current_user router = APIRouter() @router.get("/me") def read_users(user:str = Depends(get_current_user)): return {"users": ["Alice", "Bob", "Charlie"]}
-
dependencies/auth.pyfrom fastapi import FastAPI, Depends, Query, HTTPException def get_current_user(token: str = Query(...)): if token == "secret": return "authenticated_user" raise HTTPException(status_code=401, detail="Invalid or missing token")
-
- Questions:
-
Question 4.1: Describe what the
/meendpoint does inrouters/users.py. -
Question 4.2: Modify the
read_usersfunction so it returns a personalized message using the user returned by the dependency.- Example response: { "user": "authenticated_user", "users": ["Alice", "Bob", "Charlie"] }
-
Question 4.3: Update
main.pyto include the router defined inrouters/users.pyunder the prefix/users.
-
-
Integrate all examples into a single FastAPI project structure:
/project main.py routers/ users.py dependencies/ auth.py -
Tasks:
-
In
main.py, include all necessary imports and router registration. -
Ensure
/profileand/users/meboth use the sameget_current_userdependency fromdependencies/auth.py. -
Add one additional endpoint
/users/searchthat filters user names based on a query string, e.g.:GET /users/search?q=Al→ returns["Alice"]
-