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