Coverage for docs_src/bigger_applications/app_an/routers/items.py: 100%
17 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from fastapi import APIRouter, Depends, HTTPException 1abcde
3from ..dependencies import get_token_header 1abcde
5router = APIRouter( 1abcde
6 prefix="/items",
7 tags=["items"],
8 dependencies=[Depends(get_token_header)],
9 responses={404: {"description": "Not found"}},
10)
13fake_items_db = {"plumbus": {"name": "Plumbus"}, "gun": {"name": "Portal Gun"}} 1abcde
16@router.get("/") 1abcde
17async def read_items(): 1abcde
18 return fake_items_db 1abcde
21@router.get("/{item_id}") 1abcde
22async def read_item(item_id: str): 1abcde
23 if item_id not in fake_items_db: 1abcde
24 raise HTTPException(status_code=404, detail="Item not found") 1abcde
25 return {"name": fake_items_db[item_id]["name"], "item_id": item_id} 1abcde
28@router.put( 1abcde
29 "/{item_id}",
30 tags=["custom"],
31 responses={403: {"description": "Operation forbidden"}},
32)
33async def update_item(item_id: str): 1abcde
34 if item_id != "plumbus": 1abcde
35 raise HTTPException( 1abcde
36 status_code=403, detail="You can only update the item: plumbus"
37 )
38 return {"item_id": item_id, "name": "The great Plumbus"} 1abcde