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