| 1 |
from fastapi import APIRouter, Depends, HTTPException |
| 2 |
|
| 3 |
from ..dependencies import get_token_header |
| 4 |
|
| 5 |
router = APIRouter( |
| 6 |
prefix="/items", |
| 7 |
tags=["items"], |
| 8 |
dependencies=[Depends(get_token_header)], |
| 9 |
responses={404: {"description": "Not found"}}, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
fake_items_db = {"plumbus": {"name": "Plumbus"}, "gun": {"name": "Portal Gun"}} |
| 14 |
|
| 15 |
|
| 16 |
@router.get("/") |
| 17 |
async def read_items(): |
| 18 |
return fake_items_db |
| 19 |
|
| 20 |
|
| 21 |
@router.get("/{item_id}") |
| 22 |
async def read_item(item_id: str): |
| 23 |
if item_id not in fake_items_db: |
| 24 |
raise HTTPException(status_code=404, detail="Item not found") |
| 25 |
return {"name": fake_items_db[item_id]["name"], "item_id": item_id} |
| 26 |
|
| 27 |
|
| 28 |
@router.put( |
| 29 |
"/{item_id}", |
| 30 |
tags=["custom"], |
| 31 |
responses={403: {"description": "Operation forbidden"}}, |
| 32 |
) |
| 33 |
async def update_item(item_id: str): |
| 34 |
if item_id != "plumbus": |
| 35 |
raise HTTPException( |
| 36 |
status_code=403, detail="You can only update the item: plumbus" |
| 37 |
) |
| 38 |
return {"item_id": item_id, "name": "The great Plumbus"} |