-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
137 lines (110 loc) · 3.16 KB
/
Copy pathmain.py
File metadata and controls
137 lines (110 loc) · 3.16 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from fastapi import FastAPI, Request, HTTPException
app = FastAPI()
tasks = [
{
"id": 1,
"title": "Task 0",
"done": True
},
{
"id": 2,
"title": "Task 1",
"done" : True
},
{
"id": 3,
"title": "Task 2",
"done" : False
}
]
###############################################################################
@app.get("/", summary="API information")
async def root():
return {"name": "Task API", "version": "1.0", "endpoints": ["/tasks"] }
@app.get("/health",summary="Check API health")
async def health():
return {"status": "ok"}
###############################################################################
###############################################################################
@app.get("/tasks", summary="Get all tasks")
async def get_tasks():
return tasks
@app.get("/tasks/{id}", summary="Get task by ID")
async def get_task_by_id(id: int):
for task in tasks:
if task["id"] == id:
return task
return {"error": f"Task {id} not found"}
###############################################################################
@app.post("/tasks", status_code=201, summary="Create a new task")
async def create_task(req: Request):
try:
task = await req.json()
except:
raise HTTPException(
status_code=400,
detail="Invalid JSON"
)
if "title" not in task:
raise HTTPException(
status_code=400,
detail="Title is required"
)
if task["title"].strip() == "":
raise HTTPException(
status_code=400,
detail="Title cannot be empty"
)
new_task = {
"id": len(tasks) + 1,
"title": task["title"],
"done": False
}
tasks.append(new_task)
return new_task
###############################################################################
@app.put("/tasks/{id}", summary="Update a task")
async def update_task(id: int, req: Request):
task = None
for t in tasks:
if t["id"] == id:
task = t
break
if task is None:
raise HTTPException(
status_code=404,
detail=f"Task {id} not found"
)
try:
data = await req.json()
except:
raise HTTPException(
status_code=400,
detail="Invalid JSON"
)
if not data:
raise HTTPException(
status_code=400,
detail="Request body cannot be empty"
)
if "title" in data:
if data["title"].strip() == "":
raise HTTPException(
status_code=400,
detail="Title cannot be empty"
)
task["title"] = data["title"]
if "done" in data:
task["done"] = data["done"]
return task
from fastapi import Response
@app.delete("/tasks/{id}", status_code=204, summary="Delete a task")
async def delete_task(id: int):
for task in tasks:
if task["id"] == id:
tasks.remove(task)
return Response(status_code=204)
raise HTTPException(
status_code=404,
detail=f"Task {id} not found"
)