-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange.py
123 lines (109 loc) · 3.34 KB
/
exchange.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from flask import Flask, json, request
import datetime
# crudely done to represent exchange rates by date
exchanges = [
{
"source": "USD",
"exchange": 108.429,
"target": "JPY"
},
{
"source": "USD",
"exchange": 108.033,
"target": "JPY",
"date": "2020-04-02"
},
{
"source": "USD",
"exchange": 107.29594444,
"target": "JPY",
"date": "2020-04-01"
},
{
"source": "JPY",
"exchange": 0.009223,
"target": "USD"
},
{
"source": "JPY",
"exchange": 0.009256,
"target": "USD",
"date": "2020-04-02"
},
{
"source": "JPY",
"exchange": 0.00932,
"target": "USD",
"date": "2020-04-01"
},
{
"source": "HKD",
"exchange": 0.105155,
"target": "GBP"
},
{
"source": "HKD",
"exchange": 0.104106,
"target": "GBP",
"date": "2020-04-02"
},
{
"source": "HKD",
"exchange": 0.104185,
"target": "GBP",
"date": "2020-04-01"
}
]
api = Flask(__name__)
@api.route('/exchange', methods=['GET'])
def get_exchanges():
source = request.args.get('source')
target = request.args.get('target')
amount = request.args.get('amount')
exchangeDate = request.args.get('date')
# validation tests
if source is None or amount is None or target is None:
return 'minimum parameters are not set'
_amount = validateAmount(amount)
# TODO optionally compare with list of known exchanges
_source = str(source) if len(str(source)) == 3 else False
_target = str(target) if len(str(target)) == 3 else False
# checks after validation
if exchangeDate is not None:
_exchangeDate = validateDate(exchangeDate)
else:
_exchangeDate = exchangeDate
if _amount == 0:
return 'amount must be greater than 0 to get rate'
if _exchangeDate is False:
return "date must be valid date in YYYY-MM-DD format"
# end validation tests
# looping through our list
if _source is not False and _target is not False:
for e in exchanges:
if _exchangeDate is not None:
if 'date' in e:
if e['date'] == _exchangeDate and e['source'] == _source and e['target'] == _target:
return formatTargetAmount(e['exchange'], _amount)
else:
if 'date' not in e:
if e['source'] == _source and e['target'] == _target:
return formatTargetAmount(e['exchange'], _amount)
return 'no exchanges found'
def validateAmount(amount):
try:
return float(amount)
except ValueError:
return 0
def validateDate(exchangeDate):
try:
date = datetime.datetime.strptime(exchangeDate, '%Y-%m-%d')
date = str(date)
return date.split()[0]
except ValueError:
return False
def formatTargetAmount(exchange, amount):
# TODO retrieve currency format from OER, toggle return via parameter
return str(exchange * amount)
if __name__ == '__main__':
api.run()