-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (54 loc) · 1.83 KB
/
main.py
File metadata and controls
68 lines (54 loc) · 1.83 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
import uvicorn
from fastapi import FastAPI, HTTPException
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel, constr, ValidationError, validator, field_validator
app = FastAPI()
class Task(BaseModel):
name: str
description: str
id: int
status: bool
@field_validator('id')
def check_id(cls, id):
task = [t for t in tasks if t["id"] == id]
if task==None:
raise ValueError('error')
return id
@field_validator('name')
def check_name(cls, id):
task = [t for t in tasks if t["id"] == id]
if len(task["name"]) == 0:
raise ValueError('error')
return id
tasks = [{"name":"sara","description":"ertu","id":5,"status":False}]
@app.get("/{id}")
async def get_task(id:int):
task = [t for t in tasks if t["id"] == id]
return task
@app.post("/task/")
async def add_task(task: Task):
try:
tasks.append(task.dict())
except ValidationError:
raise HTTPException(status_code=400, detail="oops... an error occurred")
return f"Hello {task.name}"
@app.put("/{id}", response_model=Task)
async def update_task(newTask: Task,id:int):
try:
updated=jsonable_encoder(newTask)
task = [t for t in tasks if t["id"] == id]
task= updated
except ValidationError:
raise HTTPException(status_code=400, detail="oops... an error occurred")
return updated
@app.delete("/{id}")
async def delete_task(id:int):
try:
task = [t for t in tasks if t["id"] == id]
deleted=task
del task
except ValidationError:
raise HTTPException(status_code=400, detail="oops... an error occurred")
return deleted
if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1", port=8080)