GVAmaresh
commited on
Commit
·
0932d40
1
Parent(s):
61727d3
dev: to check working
Browse files
app.py
CHANGED
@@ -1,11 +1,60 @@
|
|
1 |
-
|
2 |
import uvicorn
|
3 |
|
|
|
|
|
|
|
|
|
4 |
app = FastAPI()
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
@app.get("/")
|
7 |
def read_root():
|
8 |
-
return {"message": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
if __name__ == "__main__":
|
11 |
uvicorn.run(app, host="0.0.0.0", port=1200)
|
|
|
1 |
+
|
2 |
import uvicorn
|
3 |
|
4 |
+
from fastapi import FastAPI, HTTPException
|
5 |
+
from pydantic import BaseModel
|
6 |
+
from typing import List
|
7 |
+
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
class Item(BaseModel):
|
11 |
+
id: int
|
12 |
+
name: str
|
13 |
+
description: str
|
14 |
+
price: float
|
15 |
+
quantity: int
|
16 |
+
|
17 |
+
items_db: List[Item] = [
|
18 |
+
Item(id=1, name="Laptop", description="A powerful laptop", price=1200.50, quantity=10),
|
19 |
+
Item(id=2, name="Smartphone", description="A high-end smartphone", price=800.00, quantity=20),
|
20 |
+
Item(id=3, name="Headphones", description="Noise-canceling headphones", price=150.00, quantity=50),
|
21 |
+
]
|
22 |
+
|
23 |
@app.get("/")
|
24 |
def read_root():
|
25 |
+
return {"message": "Welcome to the FastAPI application!"}
|
26 |
+
|
27 |
+
@app.get("/items/", response_model=List[Item])
|
28 |
+
def get_items():
|
29 |
+
"""
|
30 |
+
Get all items.
|
31 |
+
"""
|
32 |
+
return items_db
|
33 |
+
|
34 |
+
@app.get("/items/{item_id}", response_model=Item)
|
35 |
+
def get_item(item_id: int):
|
36 |
+
"""
|
37 |
+
Get an item by its ID.
|
38 |
+
"""
|
39 |
+
item = next((item for item in items_db if item.id == item_id), None)
|
40 |
+
if not item:
|
41 |
+
raise HTTPException(status_code=404, detail="Item not found")
|
42 |
+
return item
|
43 |
+
|
44 |
+
@app.post("/items/", response_model=Item)
|
45 |
+
def create_item(item: Item):
|
46 |
+
"""
|
47 |
+
Add a new item.
|
48 |
+
"""
|
49 |
+
if any(existing_item.id == item.id for existing_item in items_db):
|
50 |
+
raise HTTPException(status_code=400, detail="Item with this ID already exists")
|
51 |
+
items_db.append(item)
|
52 |
+
return item
|
53 |
+
|
54 |
+
@app.get("/health")
|
55 |
+
def health_check():
|
56 |
+
return {"status": "OK"}
|
57 |
+
|
58 |
|
59 |
if __name__ == "__main__":
|
60 |
uvicorn.run(app, host="0.0.0.0", port=1200)
|