Coverage for docs_src/dependencies/tutorial008c_an.py: 100%
17 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 fastapi import Depends, FastAPI, HTTPException 1abcdefg
2from typing_extensions import Annotated 1abcdefg
4app = FastAPI() 1abcdefg
7class InternalError(Exception): 1abcdefg
8 pass 1abcdefg
11def get_username(): 1abcdefg
12 try: 1hCijkDlmnEopqFrstGuvwHxyzIAB
13 yield "Rick" 1hCijkDlmnEopqFrstGuvwHxyzIAB
14 except InternalError: 1hijklmnopqrstuvwxyzAB
15 print("Oops, we didn't raise again, Britney 😱") 1hjkmnpqstvwyzB
18@app.get("/items/{item_id}") 1abcdefg
19def get_item(item_id: str, username: Annotated[str, Depends(get_username)]): 1abcdefg
20 if item_id == "portal-gun": 1hCijkDlmnEopqFrstGuvwHxyzIAB
21 raise InternalError( 1hjkmnpqstvwyzB
22 f"The portal gun is too dangerous to be owned by {username}"
23 )
24 if item_id != "plumbus": 1CiDlEoFrGuHxIA
25 raise HTTPException( 1iloruxA
26 status_code=404, detail="Item not found, there's only a plumbus here"
27 )
28 return item_id 1CDEFGHI