mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-24 16:29:41 -04:00
📝 Update source examples and docs from Python 3.9 to 3.10 (#14900)
This commit is contained in:
committed by
GitHub
parent
d06ab3f5c7
commit
c9e2277d8b
30
docs_src/dependencies/tutorial008b_py310.py
Normal file
30
docs_src/dependencies/tutorial008b_py310.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
data = {
|
||||
"plumbus": {"description": "Freshly pickled plumbus", "owner": "Morty"},
|
||||
"portal-gun": {"description": "Gun to create portals", "owner": "Rick"},
|
||||
}
|
||||
|
||||
|
||||
class OwnerError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def get_username():
|
||||
try:
|
||||
yield "Rick"
|
||||
except OwnerError as e:
|
||||
raise HTTPException(status_code=400, detail=f"Owner error: {e}")
|
||||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
def get_item(item_id: str, username: str = Depends(get_username)):
|
||||
if item_id not in data:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
item = data[item_id]
|
||||
if item["owner"] != username:
|
||||
raise OwnerError(username)
|
||||
return item
|
||||
Reference in New Issue
Block a user