-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
60 lines (39 loc) · 1.28 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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Dict
import uvicorn
from atm_service import ATMService
app = FastAPI()
atm_service = ATMService()
class WithdrawalRequest(BaseModel):
amount: float
class RefillRequest(BaseModel):
money: Dict[str, int]
class InventoryResponse(BaseModel):
result: Dict[str, Dict[str, int]]
@app.post("/atm/withdrawal")
def withdraw_money(request: WithdrawalRequest):
result, status_code = atm_service.withdraw_money(request.amount)
if status_code != 200:
raise HTTPException(status_code=status_code, detail=result)
return result
@app.post("/atm/refill")
def refill_money(request: RefillRequest):
result, status_code = atm_service.refill_money(request.money)
if status_code != 200:
raise HTTPException(status_code=status_code, detail=result)
return result
@app.get("/atm/inventory", response_model=InventoryResponse)
def get_inventory():
result = atm_service.get_inventory()
return result
@app.get("/atm/total")
def get_total():
result = atm_service.get_total()
return result
@app.get("/atm/maintenance")
def maintenance():
atm_service.restart()
return "full"
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)