Coverage for docs_src / dependencies / tutorial008c_py310.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from fastapi import Depends, FastAPI, HTTPException 1abcd

2 

3app = FastAPI() 1abcd

4 

5 

6class InternalError(Exception): 1abcd

7 pass 1abcd

8 

9 

10def get_username(): 1abcd

11 try: 1eofghipjklqmn

12 yield "Rick" 1eofghipjklqmn

13 except InternalError: 1efghijklmn

14 print("Oops, we didn't raise again, Britney 😱") 1egrikln

15 

16 

17@app.get("/items/{item_id}") 1abcd

18def get_item(item_id: str, username: str = Depends(get_username)): 1abcd

19 if item_id == "portal-gun": 1eofghipjklqmn

20 raise InternalError( 1egrikln

21 f"The portal gun is too dangerous to be owned by {username}" 

22 ) 

23 if item_id != "plumbus": 1ofhpjqm

24 raise HTTPException( 1fhjm

25 status_code=404, detail="Item not found, there's only a plumbus here" 

26 ) 

27 return item_id 1ospq