|
|
|
@ -3,23 +3,22 @@ from typing import Annotated, Union |
|
|
|
from fastapi import FastAPI, Header, HTTPException |
|
|
|
from pydantic import BaseModel |
|
|
|
|
|
|
|
|
|
|
|
class Item(BaseModel): |
|
|
|
id: str |
|
|
|
title: str |
|
|
|
description: Union[str, None] = None |
|
|
|
|
|
|
|
|
|
|
|
fake_secret_token = "coneofsilence" |
|
|
|
|
|
|
|
fake_db: dict[str, Item] = { |
|
|
|
"foo": Item(id="foo", title="Foo", description="There goes my hero"), |
|
|
|
"bar": Item(id="bar", title="Bar", description="The bartenders"), |
|
|
|
fake_db = { |
|
|
|
"foo": {"id": "foo", "title": "Foo", "description": "There goes my hero"}, |
|
|
|
"bar": {"id": "bar", "title": "Bar", "description": "The bartenders"}, |
|
|
|
} |
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
|
|
|
class Item(BaseModel): |
|
|
|
id: str |
|
|
|
title: str |
|
|
|
description: Union[str, None] = None |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/items/{item_id}", response_model=Item) |
|
|
|
async def read_main(item_id: str, x_token: Annotated[str, Header()]): |
|
|
|
if x_token != fake_secret_token: |
|
|
|
@ -35,5 +34,5 @@ async def create_item(item: Item, x_token: Annotated[str, Header()]): |
|
|
|
raise HTTPException(status_code=400, detail="Invalid X-Token header") |
|
|
|
if item.id in fake_db: |
|
|
|
raise HTTPException(status_code=409, detail="Item already exists") |
|
|
|
fake_db[item.id] = item |
|
|
|
fake_db[item.id] = item.model_dump() |
|
|
|
return item |
|
|
|
|