-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
107 lines (87 loc) · 3.06 KB
/
main.py
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
# importing all the required libraries
from typing import List
from fastapi import FastAPI, HTTPException, Request
import uvicorn
from helpers import build_data
from datetime import datetime, timedelta
from time import sleep
import logging
from models import Item, OrderItem
# time to update data
TIME_TO_UPDATE_DATA = timedelta(days=1)
LAST_UPDATE = datetime.now()
data = build_data()
#function to update data
def update_data():
global data
data = build_data()
print("Updated Data")
# initializing the App
app = FastAPI()
# updating data time to time
@app.middleware("http")
async def updating_data_if_needed(request: Request, call_next):
global LAST_UPDATE
if datetime.now() - LAST_UPDATE > TIME_TO_UPDATE_DATA:
update_data()
LAST_UPDATE = datetime.now()
response = await call_next(request)
return response
# defining routes
# route for fetching all items in the given category
@app.get("/drinks")
def get_drinks():
response = list(data["drinks"].values())
return {"drinks" : response}
# route for fetching by ID
@app.get("/drink/{id}")
def get_drink_by_id(id: int):
if not data["drinks"].get(id):
return HTTPException(404, "No drinks with that ID found")
return data["drinks"].get(id)
# route for fetching all items in the given category
@app.get("/desserts")
def get_desserts():
response = list(data["desserts"].values())
return {"desserts" : response}
# route for fetching by ID
@app.get("/dessert/{id}")
def get_dessert_by_id(id: int):
if not data["desserts"].get(id):
return HTTPException(404, "No dessert with that ID found")
return data["desserts"].get(id)
# route for fetching all items in the given category
@app.get("/pizzas")
def get_pizzas():
response = list(data["pizzas"].values())
return {"pizzas" : response}
# route for fetching by ID
@app.get("/pizza/{id}")
def get_pizza_by_id(id: int):
if not data["pizzas"].get(id):
return HTTPException(404, "No pizza with that ID found")
return data["pizzas"].get(id)
# route for placing an order and getting a price
@app.post("/orders")
def place_order(drinks: List[OrderItem], desserts: List[OrderItem], pizzas: List[OrderItem]):
# checking if valid info is provided or not
for drink in drinks:
if not data["drinks"].get(drink.id):
return HTTPException(404, "item(s) not found")
for dessert in desserts:
if not data["desserts"].get(dessert.id):
return HTTPException(404, "item(s) not found")
for pizza in pizzas:
if not data["pizzas"].get(pizza.id):
return HTTPException(404, "item(s) not found")
total_price = 0
# calculating the price
for drink in drinks:
total_price += data["drinks"][drink.id]["price"] * drink.quantity
for dessert in desserts:
total_price += data["desserts"][dessert.id]["price"] * dessert.quantity
for pizza in pizzas:
total_price += data["pizzas"][pizza.id]["price"] * pizza.quantity
return {"price": total_price}
if __name__ == "__main__":
uvicorn.run(f"{__name__}:app", reload=True)