-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI_1.py
More file actions
62 lines (45 loc) · 1.52 KB
/
API_1.py
File metadata and controls
62 lines (45 loc) · 1.52 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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
from uuid import UUID, uuid4
app = FastAPI()
class Task(BaseModel):
id: Optional[UUID] = None
title: str
description: Optional[str] = None
completed: bool = False
tasks = []
@app.post("/tasks/", response_model=Task)
def create_task(task: Task):
task.id = uuid4()
tasks.append(task)
return task
# Correctly defining the root path
@app.get("/tasks/", response_model=List[Task])
def read_tasks():
return tasks
@app.get("/tasks/{task_id}", response_model=Task)
def read_task(task_id: UUID):
for task in tasks:
if tasks.id == task_id:
return task_id
raise HTTPException(status_code=404, detail="Task not founds")
@app.put("/tasks/{task_id}", response_model=Task)
def update_task(task_id: UUID, task_update: Task):
for idx, task in enumerate(tasks):
if task.id == task_id:
updated_task = task.copy(update=task_update.dict(
exclude_unset=True))
task[idx] = updated_task
return updated_task
raise HTTPException(status_code=404, detail="Task not found")
@app.delete("/tasks/{task_id}", response_model=Task)
def delete_task(task_id: UUID):
for idx, task in enumerate:
if task.id == task_id:
return tasks.pop(idx)
raise HTTPException(status_code=404, detail="Task not found")
# Run the api
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)