-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOST_HTTP_Method.py
More file actions
119 lines (87 loc) · 3.3 KB
/
POST_HTTP_Method.py
File metadata and controls
119 lines (87 loc) · 3.3 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
from fastapi import FastAPI , Path , HTTPException , Query
from pydantic import BaseModel , computed_field , Field
from typing import Annotated , Literal
from fastapi.responses import JSONResponse
import json
app = FastAPI()
class Patient(BaseModel):
id : str
name: str
city: str
age: Annotated[int , Field(gt = 0 , lt = 100 , description= 'Age of the patient')]
gender: Annotated[Literal['male' , 'female' , 'others'] , Field(... , description= 'Details about patient gender')]
height: float
weight: float
@computed_field
@property
def bmi(self)->float:
bmi = round((self.weight / self.height**2) , 5)
return bmi
@computed_field
@property
def verdict(self)->str:
if self.bmi < 18.5:
return "Underweight"
elif 18.5 <= self.bmi < 24.9:
return "Normal weight"
elif 25 <= self.bmi < 29.9:
return "Overweight"
else:
return "Obese"
def get_data():
with open("Data\patient.json", "r") as file:
data = json.load(file)
return data
def save_data(data):
with open("Data\patient.json", "w") as file:
json.dump(data , file)
@app.post('/create')
def create_patient(patient_data : Patient):
exist_data = get_data()
# check if alreay exist
if patient_data.id in exist_data:
raise HTTPException(status_code=400, detail="Patient already exists in the data")
# if new then add in DB
data = patient_data.model_dump(exclude=['id'])
exist_data[patient_data.id] = data
# save as json file
save_data(exist_data)
return JSONResponse(status_code= 201 , content= {"message" : "patient created sucessfully"})
@app.get("/")
def welcome_msg():
return {"message": "Welcome to the FastAPI application!"}
@app.get("/about")
def about():
return {"message": "This is a simple FastAPI application."}
@app.get("/view")
def view():
try:
return get_data()
except FileNotFoundError:
return {"error": "File not found. Please ensure 'patient.json' exists."}
@app.get("/patient/{patient_id}")
def get_patient(patient_id: str = Path(... , description="The ID of the patient to retrieve" , example= 'P001')):
# load all data
data = get_data()
if patient_id in data:
return data[patient_id]
else:
raise HTTPException(status_code=404, detail="Patient not found")
@app.get("/sort")
def sort_patient(sort_by : str = Query(... , description= "Sort On the basis od Weight, BMI and height" , example= "bmi"),
order :str = Query("asc" , description= "either in asc or in desc order" , examples= ['asc' , 'desc'])):
acceptable_sort_by = ['bmi' , 'height' , 'weight']
if sort_by not in acceptable_sort_by:
return HTTPException(status_code= 400 , detail= "value provided is not right")
try:
all_data = get_data()
is_desc = True
if order == "asc":
is_desc = False
ans = []
for data in all_data.items():
ans.append((data[1][sort_by] , data[1]))
ans = sorted(ans , key= lambda x : x[0] , reverse= is_desc)
return {"sorted_patient" : [i[1] for i in ans]}
except :
raise HTTPException(status_code= 500 , detail= "Something wrong with the system")