Coverage for docs_src/dependencies/tutorial008b_an_py39.py: 100%
19 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
1from typing import Annotated 1abcd
3from fastapi import Depends, FastAPI, HTTPException 1abcd
5app = FastAPI() 1abcd
8data = { 1abcd
9 "plumbus": {"description": "Freshly pickled plumbus", "owner": "Morty"},
10 "portal-gun": {"description": "Gun to create portals", "owner": "Rick"},
11}
14class OwnerError(Exception): 1abcd
15 pass 1abcd
18def get_username(): 1abcd
19 try: 1imejnfkoglph
20 yield "Rick" 1imejnfkoglph
21 except OwnerError as e: 1menfogph
22 raise HTTPException(status_code=400, detail=f"Owner error: {e}") 1efgh
25@app.get("/items/{item_id}") 1abcd
26def get_item(item_id: str, username: Annotated[str, Depends(get_username)]): 1abcd
27 if item_id not in data: 1imejnfkoglph
28 raise HTTPException(status_code=404, detail="Item not found") 1mnop
29 item = data[item_id] 1iejfkglh
30 if item["owner"] != username: 1iejfkglh
31 raise OwnerError(username) 1efgh
32 return item 1ijkl