-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
96 lines (79 loc) · 2.43 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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi import HTTPException
from fastapi.background import BackgroundTasks
from redis_om import get_redis_connection, HashModel, NotFoundError
from starlette.requests import Request
from dotenv import load_dotenv
import requests
import time
import os
load_dotenv()
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=os.getenv("ALLOW_ORIGINS", "*").split(","),
allow_methods=os.getenv("ALLOW_METHODS", "*").split(","),
allow_headers=os.getenv("ALLOW_HEADERS", "*").split(","),
)
redis = get_redis_connection(
host=os.getenv("REDIS_HOST", "localhost"),
port=int(os.getenv("REDIS_PORT", 6379)),
db=int(os.getenv("REDIS_DB", 0)),
decode_responses=True
)
class Order(HashModel):
product_id: str
quantity: int
price: float
fee: float
total: float
status: str
class Meta:
database = redis
model_key_prefix = "order"
def format_order(pk: str):
order = Order.get(pk)
return {
"id": order.pk,
"product_id": order.product_id,
"quantity": order.quantity,
"price": order.price,
"fee": order.fee,
"total": order.total,
"status": order.status
}
@app.get("/orders")
def get_all_orders():
return [format_order(pk) for pk in Order.all_pks()]
@app.get("/orders/{pk}")
def get_order(pk: str):
try:
return Order.get(pk)
except NotFoundError:
raise HTTPException(status_code=404, detail=f"Order with pk {pk} not found")
@app.post("/orders")
async def create_order(request: Request, background_tasks: BackgroundTasks):
data = await request.json()
product_service_url = os.getenv("PRODUCT_SERVICE_URL", "http://localhost:8000")
req = requests.get(f"{product_service_url}/products/" + data["id"])
product = req.json()
fee_percentage = float(os.getenv("FEE_PERCENTAGE", 0.2))
fee = fee_percentage * product["price"]
total = (1 + fee_percentage) * product["price"]
order = Order(
product_id=data["id"],
quantity=data["quantity"],
price=product["price"],
fee=fee,
total=total,
status="pending"
)
order.save()
background_tasks.add_task(order_completed, order)
return order
def order_completed(order: Order):
time.sleep(5)
order.status = "completed"
order.save()
redis.xadd("order_completed", order.dict(), '*')