lqs
d20566fe07
Some checks failed
Test / lint (push) Has been cancelled
Test / test (pydantic-v1, 3.10) (push) Has been cancelled
Test / test (pydantic-v1, 3.11) (push) Has been cancelled
Test / test (pydantic-v1, 3.12) (push) Has been cancelled
Test / test (pydantic-v1, 3.8) (push) Has been cancelled
Test / test (pydantic-v1, 3.9) (push) Has been cancelled
Test / test (pydantic-v2, 3.10) (push) Has been cancelled
Test / test (pydantic-v2, 3.11) (push) Has been cancelled
Test / test (pydantic-v2, 3.12) (push) Has been cancelled
Test / test (pydantic-v2, 3.8) (push) Has been cancelled
Test / test (pydantic-v2, 3.9) (push) Has been cancelled
Test / coverage-combine (push) Has been cancelled
Test / check (push) Has been cancelled
Issue Manager / issue-manager (push) Has been cancelled
Label Approved / label-approved (push) Has been cancelled
31 lines
735 B
Python
31 lines
735 B
Python
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
|