Coverage for docs_src / bigger_applications / app_an_py310 / routers / items.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi import APIRouter, Depends, HTTPException 1abc

2 

3from ..dependencies import get_token_header 1abc

4 

5router = APIRouter( 1abc

6 prefix="/items", 

7 tags=["items"], 

8 dependencies=[Depends(get_token_header)], 

9 responses={404: {"description": "Not found"}}, 

10) 

11 

12 

13fake_items_db = {"plumbus": {"name": "Plumbus"}, "gun": {"name": "Portal Gun"}} 1abc

14 

15 

16@router.get("/") 1abc

17async def read_items(): 1abc

18 return fake_items_db 1pqr

19 

20 

21@router.get("/{item_id}") 1abc

22async def read_item(item_id: str): 1abc

23 if item_id not in fake_items_db: 1defghi

24 raise HTTPException(status_code=404, detail="Item not found") 1dfh

25 return {"name": fake_items_db[item_id]["name"], "item_id": item_id} 1egi

26 

27 

28@router.put( 1abc

29 "/{item_id}", 

30 tags=["custom"], 

31 responses={403: {"description": "Operation forbidden"}}, 

32) 

33async def update_item(item_id: str): 1abc

34 if item_id != "plumbus": 1jklmno

35 raise HTTPException( 1kmo

36 status_code=403, detail="You can only update the item: plumbus" 

37 ) 

38 return {"item_id": item_id, "name": "The great Plumbus"} 1jln