forked from umb-web/fastapi_exchange
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (55 loc) · 1.94 KB
/
main.py
File metadata and controls
70 lines (55 loc) · 1.94 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
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
)
def convert_currency(amount: float, from_currency: str, to_currency: str):
exchange_rates = {
("USD", "EUR"): 0.92,
("EUR", "USD"): 1.08,
("USD", "COP"): 4104,
("COP", "USD"): 0.00024,
("USD", "GBP"): 0.78,
("GBP", "USD"): 1.28,
("EUR", "COP"): 4441,
("COP", "EUR"): 0.00023,
("EUR", "GBP"): 0.85,
("GBP", "EUR"): 1.18,
("COP", "GBP"): 0.00019,
("GBP", "COP"): 5299,
}
if from_currency == to_currency:
return amount
rate = exchange_rates.get((from_currency, to_currency))
if rate is None:
raise ValueError(f"exchange not available {from_currency} a {to_currency}")
return amount * rate
@app.post("/exchange_currency")
async def exchange(request: Request):
try:
body = await request.json()
amount = body.get("amount")
from_currency = body.get("from_currency")
to_currency = body.get("to_currency")
if amount is None or from_currency is None or to_currency is None:
raise HTTPException(status_code=400, detail="data missing")
total = convert_currency(amount, from_currency, to_currency)
if total is None:
raise HTTPException(status_code=400, detail="currency not available")
return JSONResponse(
content={
"amount": amount,
"from_currency": from_currency,
"to_currency": to_currency,
"total": total,
},
status_code=200,
)
except HTTPException as http_error:
raise http_error
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)