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

18 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1from fastapi import Depends, FastAPI, HTTPException 1abcdefg

2from typing_extensions import Annotated 1abcdefg

3 

4app = FastAPI() 1abcdefg

5 

6 

7class InternalError(Exception): 1abcdefg

8 pass 1abcdefg

9 

10 

11def get_username(): 1abcdefg

12 try: 1CvhiDwjkExlmFynoGzpqHArsIBtu

13 yield "Rick" 1CvhiDwjkExlmFynoGzpqHArsIBtu

14 except InternalError: 1vhiwjkxlmynozpqArsBtu

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

16 raise 1hijklmnopqrstu

17 

18 

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

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

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

22 raise InternalError( 1hijklmnopqrstu

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

24 ) 

25 if item_id != "plumbus": 1CvDwExFyGzHAIB

26 raise HTTPException( 1vwxyzAB

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

28 ) 

29 return item_id 1CDEFGHI