├── .gitignore ├── requirements.txt ├── README.md └── main.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | fastapi 2 | uvicorn -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple FastAPI Example 2 | 3 | This is a really basic example of a FastAPI application. 4 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI, HTTPException 2 | from pydantic import BaseModel 3 | 4 | app = FastAPI() 5 | 6 | 7 | class Item(BaseModel): 8 | text: str = None 9 | is_done: bool = False 10 | 11 | 12 | items = [] 13 | 14 | 15 | @app.get("/") 16 | def root(): 17 | return {"Hello": "World"} 18 | 19 | 20 | @app.post("/items") 21 | def create_item(item: Item): 22 | items.append(item) 23 | return items 24 | 25 | 26 | @app.get("/items", response_model=list[Item]) 27 | def list_items(limit: int = 10): 28 | return items[0:limit] 29 | 30 | 31 | @app.get("/items/{item_id}", response_model=Item) 32 | def get_item(item_id: int) -> Item: 33 | if item_id < len(items): 34 | return items[item_id] 35 | else: 36 | raise HTTPException(status_code=404, detail=f"Item {item_id} not found") 37 | --------------------------------------------------------------------------------