Coverage for docs_src/dependencies/tutorial008d.py: 100%
17 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from fastapi import Depends, FastAPI, HTTPException 1abcde
3app = FastAPI() 1abcde
6class InternalError(Exception): 1abcde
7 pass 1abcde
10def get_username(): 1abcde
11 try: 1abcde
12 yield "Rick" 1abcde
13 except InternalError: 1abcde
14 print("We don't swallow the internal error here, we raise again 😎") 1abcde
15 raise 1abcde
18@app.get("/items/{item_id}") 1abcde
19def get_item(item_id: str, username: str = Depends(get_username)): 1abcde
20 if item_id == "portal-gun": 1abcde
21 raise InternalError( 1abcde
22 f"The portal gun is too dangerous to be owned by {username}"
23 )
24 if item_id != "plumbus": 1abcde
25 raise HTTPException( 1abcde
26 status_code=404, detail="Item not found, there's only a plumbus here"
27 )
28 return item_id 1abcde