Skip to content

Commit

Permalink
Fix name typos and use pydantic v2 method
Browse files Browse the repository at this point in the history
  • Loading branch information
ddxgz committed Jul 6, 2024
1 parent d2a1fcc commit 8f57f59
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions 03-fastapi/src/worker.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
from fastapi import FastAPI, Request
from pydantic import BaseModel


async def on_fetch(request, env):
import asgi

return await asgi.fetch(app, request, env)


app = FastAPI()


@app.get("/")
async def root():
return {"message": "Hello, World!"}


@app.get("/env")
async def root(req: Request):
async def env(req: Request):
env = req.scope["env"]
return {"message": "Here is an example of getting an environment variable: " + env.MESSAGE}
return {
"message": "Here is an example of getting an environment variable: "
+ env.MESSAGE
}


class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None


@app.post("/items/")
async def create_item(item: Item):
return item


@app.put("/items/{item_id}")
async def create_item(item_id: int, item: Item, q: str | None = None):
result = {"item_id": item_id, **item.dict()}
async def update_item(item_id: int, item: Item, q: str | None = None):
result = {"item_id": item_id, **item.model_dump()}
if q:
result.update({"q": q})
return result


@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
return {"item_id": item_id}

0 comments on commit 8f57f59

Please sign in to comment.