FastAPI — Intermediate (50+ Coding Exercises)
- Split routes into
auth,users,itemsrouters with/api/v1prefix. - Implement OAuth2 password flow endpoints (
/token) usingOAuth2PasswordBearer(basic). - Implement JWT creation and verification (HS256) and protect
/me. - Implement refresh token flow (store refresh tokens server-side with TTL).
- Implement role-based access control using JWT claims and dependency checks.
- Create a SQLAlchemy (or SQLModel) setup with
UserandItemmodels. - Implement DB session dependency per request with proper cleanup/rollback.
- Implement CRUD for items backed by the database.
- Add Alembic migrations and create an initial migration.
- Add a new indexed column to items and generate migration.
- Implement cursor-based pagination for item listing.
- Implement allowed-field sorting with validation (reject unknown sort fields).
- Implement filter dependency
FilterParamsand apply it to list endpoint. - Implement
Idempotency-Keysupport for create endpoints (store request hash + response). - Implement request validation error handler with consistent error envelope.
- Implement custom exception class and handler for domain errors.
- Add logging middleware for request latency + status code.
- Add request ID middleware and propagate to logs and response header.
- Implement rate limiting per IP using in-memory cache (simple fixed window).
- Implement rate limiting per user when authenticated.
- Implement file uploads streaming to disk and enforce max size.
- Implement background job enqueue stub for sending emails (use
BackgroundTasksfirst). - Implement task queue integration skeleton (Celery/RQ) and switch from background task.
- Implement
StreamingResponsethat streams a generated CSV export of items. - Implement ETag support for GET item endpoints (hash of representation).
- Implement
If-None-Matchhandling returning 304 when unchanged. - Implement caching headers (
Cache-Control) for read-only endpoints. - Implement OpenAPI customization: title, description, servers list.
- Group endpoints by tags and add descriptions/examples.
- Implement dependency overrides for tests (DB session to SQLite memory).
- Add pytest fixtures for app client and DB session.
- Write tests for auth flows (token, protected endpoint, invalid token).
- Write tests for DB-backed CRUD endpoints.
- Write tests for pagination correctness (cursor next page).
- Implement async endpoints that use async DB calls (if using async engine) and test them.
- Add CORS config for SPA: allow origin, methods, headers, credentials properly.
- Add a
/metricsendpoint that returns basic counters (requests, errors) in JSON. - Add a middleware that increments metrics counters.
- Add input sanitization/normalization for user emails (lowercase) and enforce uniqueness.
- Implement password hashing (bcrypt/argon2) and verify on login.
- Implement account lockout after N failed logins (cache-based).
- Implement email verification token generation and verification endpoint.
- Implement a “forgot password” token flow (generate + reset).
- Implement database transaction around multi-step create (user + profile + default settings).
- Implement bulk create items endpoint and validate payload size limit.
- Implement partial update (PATCH) with Pydantic models (optional fields).
- Implement response models that hide internal fields (
response_model_exclude). - Implement dependency that parses common pagination params and reuse it across endpoints.
- Implement content negotiation for returning CSV vs JSON based on query param.
- Implement per-route timeouts (conceptual via middleware / server settings) with stubs.
- Implement structured logging JSON lines output for all requests.
- Implement an integration test that spins up the app and verifies OpenAPI schema includes your routes.