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

17 statements  

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

1from typing import Annotated 1abcde

2 

3from fastapi import Depends, FastAPI, HTTPException 1abcde

4 

5app = FastAPI() 1abcde

6 

7 

8class InternalError(Exception): 1abcde

9 pass 1abcde

10 

11 

12def get_username(): 1abcde

13 try: 1fughivjklwmnoxpqryst

14 yield "Rick" 1fughivjklwmnoxpqryst

15 except InternalError: 1fghijklmnopqrst

16 print("Oops, we didn't raise again, Britney 😱") 1fhiklnoqrt

17 

18 

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

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

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

22 raise InternalError( 1fhiklnoqrt

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

24 ) 

25 if item_id != "plumbus": 1ugvjwmxpys

26 raise HTTPException( 1gjmps

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

28 ) 

29 return item_id 1uvwxy