forked from pixegami/simple-fastapi-example
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmain.py
More file actions
36 lines (23 loc) · 670 Bytes
/
main.py
File metadata and controls
36 lines (23 loc) · 670 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
text: str = None
is_done: bool = False
items = []
@app.get("/")
def root():
return {"Hello": "World"}
@app.post("/items")
def create_item(item: Item):
items.append(item)
return items
@app.get("/items", response_model=list[Item])
def list_items(limit: int = 10):
return items[0:limit]
@app.get("/items/{item_id}", response_model=Item)
def get_item(item_id: int) -> Item:
if item_id < len(items):
return items[item_id]
else:
raise HTTPException(status_code=404, detail=f"Item {item_id} not found")