Coverage for docs_src / dependencies / tutorial008c_py310.py: 100%
16 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 fastapi import Depends, FastAPI, HTTPException 1abc
3app = FastAPI() 1abc
6class InternalError(Exception): 1abc
7 pass 1abc
10def get_username(): 1abc
11 try: 1dmefgnhijokl
12 yield "Rick" 1dmefgnhijokl
13 except InternalError: 1defghijkl
14 print("Oops, we didn't raise again, Britney 😱") 1dfgijl
17@app.get("/items/{item_id}") 1abc
18def get_item(item_id: str, username: str = Depends(get_username)): 1abc
19 if item_id == "portal-gun": 1dmefgnhijokl
20 raise InternalError( 1dfgijl
21 f"The portal gun is too dangerous to be owned by {username}"
22 )
23 if item_id != "plumbus": 1menhok
24 raise HTTPException( 1ehk
25 status_code=404, detail="Item not found, there's only a plumbus here"
26 )
27 return item_id 1mno