Created
March 4, 2025 14:18
-
-
Save Vasiliy566/3c4118b135c8bc53f178395d4123a1c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uvicorn | |
from fastapi import FastAPI, HTTPException | |
from pydantic import BaseModel | |
app = FastAPI() | |
class Item(BaseModel): | |
name: str | |
price: float | |
storage = { | |
1: Item(name="phone", price=9999.999), | |
2: Item(name="Plane", price=10000000.0) | |
} | |
@app.get("/") | |
def read_root(): | |
return {"Hello": "FastAPI"} | |
@app.get("/items/") | |
def get_item(item_id: int) -> Item: | |
if item_id not in storage: | |
raise HTTPException(status_code=404, detail='Not found.') | |
return storage[item_id] | |
@app.post("/items/") | |
def create_item(item: Item) -> int: | |
item_id = max(storage.keys()) + 1 if storage else 1 | |
storage[max(storage.keys()) + 1] = item | |
return item_id | |
uvicorn.run(app, host="0.0.0.0", port=8888) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment