-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
49 lines (38 loc) · 1.29 KB
/
app.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
from flask import Flask, jsonify
app = Flask(__name__)
exchange_rates = {
"TWD": {
"TWD": 1,
"JPY": 3.669,
"USD": 0.03281
},
"JPY": {
"TWD": 0.26956,
"JPY": 1,
"USD": 0.00885
},
"USD": {
"TWD": 30.444,
"JPY": 111.801,
"USD": 1
}
}
@app.route('/exchange', methods=['POST'])
def exchange():
data = request.get_json()
if not data or 'source' not in data or 'target' not in data or 'amount' not in data:
return jsonify({'msg': 'Invalid request data. Please provide source, target, and amount.', 'amount': 0.0}), 400
source = data['source'].upper()
target = data['target'].upper()
amount = float(data['amount'])
if source not in exchange_rates or target not in exchange_rates:
return jsonify({'msg': 'Invalid currency code provided.', 'amount': 0.0}), 400
if source == target:
return jsonify({'msg': 'Same currency conversion not supported.', 'amount': amount}), 400
conversion_rate = exchange_rates[source][target]
converted_amount = amount * conversion_rate
# Round the converted amount to two decimal places
converted_amount = round(converted_amount, 2)
return jsonify({'msg': 'Exchange successful!', 'amount': converted_amount}), 200
if __name__ == '__main__':
app.run(debug=True)