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

18 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1from fastapi import Depends, FastAPI, HTTPException 1abcdef

2from typing_extensions import Annotated 1abcdef

3 

4app = FastAPI() 1abcdef

5 

6 

7class InternalError(Exception): 1abcdef

8 pass 1abcdef

9 

10 

11def get_username(): 1abcdef

12 try: 1ysghztijAuklBvmnCwopDxqr

13 yield "Rick" 1ysghztijAuklBvmnCwopDxqr

14 except InternalError: 1sghtijuklvmnwopxqr

15 print("We don't swallow the internal error here, we raise again 😎") 1ghijklmnopqr

16 raise 1ghijklmnopqr

17 

18 

19@app.get("/items/{item_id}") 1abcdef

20def get_item(item_id: str, username: Annotated[str, Depends(get_username)]): 1abcdef

21 if item_id == "portal-gun": 1ysghztijAuklBvmnCwopDxqr

22 raise InternalError( 1ghijklmnopqr

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

24 ) 

25 if item_id != "plumbus": 1ysztAuBvCwDx

26 raise HTTPException( 1stuvwx

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

28 ) 

29 return item_id 1yzABCD