-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (107 loc) · 4.72 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import time
import os
from flask import Flask
from flask_restful import Resource, Api, reqparse, abort
from dotenv import load_dotenv
from rates import *
from requests import post
# Load .env file variables in app
load_dotenv()
# .env variables
RATES_CACHE_TIMEOUT = os.getenv('RATES_CACHE_TIMEOUT')
RATES_API_ACCESS_KEY = os.getenv('RATES_API_ACCESS_KEY')
ENV = os.getenv('ENV')
PORT = os.getenv('PORT')
# Currencies with rates data object
RATES_DATA = get_rates(ENV, RATES_API_ACCESS_KEY)
webapp = Flask(__name__)
api = Api(webapp)
class ServerTestApi(Resource):
# test server is running
def get(self):
print("Debug: Server test")
return {'server': 'running', 'app': 'currency converter'}
class RatesApi(Resource):
# get list of currencies
def get(self):
print("Debug: Currencies requested.")
return RATES_DATA
# add new currency and rate
def post(self):
args = parser.parse_args()
print("Debug: Add new currency with rate.")
# Currency name should be uppercase and 3 chars in length
new_currency = args["new_currency"].upper()[0:3]
new_rate = args["new_rate"]
try:
new_rate = float(new_rate)
except ValueError as e:
abort(400, error="Rate must be float.")
if currency_exists(RATES_DATA, new_currency) is True:
abort(400, error=f"Currency '{new_currency}' already exists.")
add_currency_rate(RATES_DATA, new_currency, new_rate)
return {"success": "true", "result": {new_currency: new_rate, "rates": RATES_DATA["rates"]}}
# update existing currency with new rate
def put(self):
args = parser.parse_args()
print("Debug: Update currency with new rate.")
# Currency name should be uppercase and 3 chars in length
currency = args["currency"].upper()[0:3]
new_rate = args["new_rate"]
try:
new_rate = float(new_rate)
except ValueError as e:
abort(400, error="Rate must be float.")
if currency_exists(RATES_DATA, currency) is not True:
abort(400, error=f"Currency '{currency}' does not exists.")
update_currency_rate(RATES_DATA, currency, new_rate)
return {"success": "true", "result": {currency: new_rate, "rates": RATES_DATA["rates"]}}, 201
# delete existing currency
def delete(self):
args = parser.parse_args()
print("Debug: Delete currency.")
currency = args["currency"].upper()
if currency_exists(RATES_DATA, currency) is not True:
abort(400, error=f"Currency '{currency}' does not exists.")
delete_currency(RATES_DATA, currency)
return {"success": "true", "result": {"rates": RATES_DATA["rates"]}}, 204
class RatesResetApi(Resource):
# get list of currencies
def post(self):
print("Debug: Reset currencies list.")
RATES_DATA = get_rates(ENV, RATES_API_ACCESS_KEY)
return RATES_DATA
class RatesConvertApi(Resource):
def get(self, from_value, from_currency, to_currency):
global RATES_DATA
if get_rates_latest_timestamp(RATES_DATA) + int(RATES_CACHE_TIMEOUT) < int(time.time()):
print("Debug: Cache timeout. Rates updated.")
RATES_DATA = get_rates(ENV, RATES_API_ACCESS_KEY)
from_currency = from_currency.upper()[0:3]
to_currency = to_currency.upper()[0:3]
# Check for from_value to be number
try:
from_value = float(from_value)
except ValueError as e:
abort(400, error="Money must be number.")
if currency_exists(RATES_DATA, from_currency) is not True:
abort(400, error=f"Currency from '{from_currency}' does not exist.")
if currency_exists(RATES_DATA, to_currency) is not True:
abort(400, error=f"Currency to '{to_currency}' does not exist.")
# convert from one currency to other with exchange rates
to_value = convert_rates(RATES_DATA, from_value, from_currency, to_currency)
# send result to client
return {"success": "true", "result": {from_currency: from_value, to_currency: to_value}}
api.add_resource(ServerTestApi, "/")
api.add_resource(RatesApi, "/rates")
api.add_resource(RatesResetApi, "/rates/reset")
api.add_resource(RatesConvertApi, "/convert/<from_value>/<from_currency>/<to_currency>")
parser = reqparse.RequestParser()
parser.add_argument("currency")
parser.add_argument("from_value", type=float, help="Money value must be number.")
parser.add_argument("from_currency")
parser.add_argument("to_currency")
parser.add_argument("new_currency")
parser.add_argument("new_rate", type=float, help="Rate value must be float.")
if __name__ == "__main__":
webapp.run(debug=True, host="0.0.0.0", port=PORT)