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