-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix name typos and use pydantic v2 method
- Loading branch information
Showing
1 changed file
with
16 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
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} |