mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-31 18:20:45 -05:00
* ✨ Add Default and DefaultPlaceholder data structures to handle defaults and overrides * ✨ Add utils to get values by priority handling DefaultPlaceholders * ✨ Add support for top-level parameters in FastAPI, APIRouter, include_router including: prefix, tags, dependencies, deprecated, include_in_schema, responses, default_response_class, callbacks * ♻️ Update openapi utils to handle DefaultPlaceholder for response_class * 📝 Update bigger-application example code to use top-level params and showcase them in APIRouter, FastAPI, include_router * 📝 Update docs for Bigger Applications, include diagrams, top-level params * 🔥 Simplify code and docs for callbacks as default_response_class is no longer required * 📝 Add docs for top-level dependencies, in FastAPI() * 📝 Add docs reference to top-level dependencies in docs for decorator * ✅ Update/increase tests for Bigger Applications including shared parameters * ✅ Add tests for top-level dependencies in FastAPI() * ✅ Add tests for internal DefaultPlaceholder * ✅ Update/increase tests for callbacks with top-level parameters * ✅ Add LOTS of tests covering branches and cases for shared parameters in top-level FastAPI, path operations, include_router, APIRouter, its path operations, nested include_router, nested APIRouter, and its path operations * 🎨 Format/reorder parameters for consistency in FastAPI, APIRouter, include_router
39 lines
1011 B
Python
39 lines
1011 B
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
from ..dependencies import get_token_header
|
|
|
|
router = APIRouter(
|
|
prefix="/items",
|
|
tags=["items"],
|
|
dependencies=[Depends(get_token_header)],
|
|
responses={404: {"description": "Not found"}},
|
|
)
|
|
|
|
|
|
fake_items_db = {"plumbus": {"name": "Plumbus"}, "gun": {"name": "Portal Gun"}}
|
|
|
|
|
|
@router.get("/")
|
|
async def read_items():
|
|
return fake_items_db
|
|
|
|
|
|
@router.get("/{item_id}")
|
|
async def read_item(item_id: str):
|
|
if item_id not in fake_items_db:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return {"name": fake_items_db[item_id]["name"], "item_id": item_id}
|
|
|
|
|
|
@router.put(
|
|
"/{item_id}",
|
|
tags=["custom"],
|
|
responses={403: {"description": "Operation forbidden"}},
|
|
)
|
|
async def update_item(item_id: str):
|
|
if item_id != "plumbus":
|
|
raise HTTPException(
|
|
status_code=403, detail="You can only update the item: plumbus"
|
|
)
|
|
return {"item_id": item_id, "name": "The great Plumbus"}
|