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