forked from tim-hub/sanic-currency-exchange-rates-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
340 lines (276 loc) · 10.3 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import fcntl
import itertools
from datetime import datetime
from decimal import Decimal
from os import getenv
from defusedxml import ElementTree
import requests
import ujson
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from gino import Gino
from gino.dialects.asyncpg import JSONB
from sanic import Sanic
from sanic.response import json, redirect
from constants import HISTORIC_RATES_URL, LAST_90_DAYS_RATES_URL, BASE_CURRENCY
from utils import parse_database_url, cors
app = Sanic(__name__)
app.config.update(
parse_database_url(
url=getenv("DATABASE_URL", "postgresql://localhost/exchangerates")
)
)
# Database
db = Gino()
class ExchangeRates(db.Model):
__tablename__ = "exchange_rates"
date = db.Column(db.Date(), primary_key=True)
rates = db.Column(JSONB())
def __repr__(self):
return "Rates [{}]".format(self.date)
async def update_rates(historic=False):
r = requests.get(HISTORIC_RATES_URL if historic else LAST_90_DAYS_RATES_URL)
envelope = ElementTree.fromstring(r.content)
namespaces = {
"gesmes": "http://www.gesmes.org/xml/2002-08-01",
"eurofxref": "http://www.ecb.int/vocabulary/2002-08-01/eurofxref",
}
data = envelope.findall("./eurofxref:Cube/eurofxref:Cube[@time]", namespaces)
for d in data:
time = datetime.strptime(d.attrib["time"], "%Y-%m-%d").date()
rates = await ExchangeRates.get(time)
if not rates:
await ExchangeRates.create(
date=time,
rates={
c.attrib["currency"]: Decimal(c.attrib["rate"]) for c in list(d)
},
)
async def to_exchange_rates(request, date=None):
if request.method == "HEAD":
return json("")
dt = datetime.now()
if date:
try:
dt = datetime.strptime(date, "%Y-%m-%d")
except ValueError as e:
return json({"error": "{}".format(e)}, status=400)
if dt < datetime(1999, 1, 4):
return json(
{"error": "There is no data for dates older then 1999-01-04."},
status=400,
)
exchange_rates = (
await ExchangeRates.query.where(ExchangeRates.date <= dt.date())
.order_by(ExchangeRates.date.desc())
.gino.first()
)
rates = exchange_rates.rates
# Base
base = BASE_CURRENCY
if "base" in request.raw_args and request.raw_args["base"] != "EUR":
base = request.raw_args["base"]
if base in rates:
base_rate = Decimal(rates[base])
rates = {
currency: Decimal(rate) / base_rate for currency, rate in rates.items()
}
rates["EUR"] = Decimal(1) / base_rate
else:
return json(
{"error": "Base '{}' is not supported.".format(base)}, status=400
)
# Symbols
if "symbols" in request.args:
symbols = list(
itertools.chain.from_iterable(
[symbol.split(",") for symbol in request.args["symbols"]]
)
)
if all(symbol in rates for symbol in symbols):
rates = {symbol: rates[symbol] for symbol in symbols}
else:
return json(
{
"error": "Symbols '{}' are invalid for date {}.".format(
",".join(symbols), dt.date()
)
},
status=400,
)
return json(
{"base": base, "date": exchange_rates.date.strftime("%Y-%m-%d"), "rates": rates}
)
async def exchange_rates_history(request):
if request.method == "HEAD":
return json("")
if "start_at" in request.raw_args:
try:
start_at = datetime.strptime(request.raw_args["start_at"], "%Y-%m-%d")
except ValueError as e:
return json(
{"error": "start_at parameter format", "exception": "{}".format(e)},
status=400,
)
else:
return json({"error": "missing start_at parameter"})
if "end_at" in request.raw_args:
try:
end_at = datetime.strptime(request.raw_args["end_at"], "%Y-%m-%d")
except ValueError as e:
return json(
{"error": "end_at parameter format", "exception": "{}".format(e)},
status=400,
)
else:
return json({"error": "missing end_at parameter"})
exchange_rates = (
await ExchangeRates.query.where(ExchangeRates.date >= start_at.date())
.where(ExchangeRates.date <= end_at.date())
.order_by(ExchangeRates.date.asc())
.gino.all()
)
base = BASE_CURRENCY
historic_rates = {}
for er in exchange_rates:
rates = er.rates
if "base" in request.raw_args and request.raw_args["base"] != "EUR":
base = request.raw_args["base"]
if base in rates:
base_rate = Decimal(rates[base])
rates = {
currency: Decimal(rate) / base_rate
for currency, rate in rates.items()
}
rates["EUR"] = Decimal(1) / base_rate
else:
return json(
{"error": "Base '{}' is not supported.".format(base)}, status=400
)
# Symbols
if "symbols" in request.args:
symbols = list(
itertools.chain.from_iterable(
[symbol.split(",") for symbol in request.args["symbols"]]
)
)
if all(symbol in rates for symbol in symbols):
rates = {symbol: rates[symbol] for symbol in symbols}
else:
return json(
{"error": "Symbols '{}' are invalid.".format(",".join(symbols))},
status=400,
)
historic_rates[er.date] = rates
return json({"base": base, "start_at": start_at.date().isoformat(), "end_at": end_at.date().isoformat(),
"rates": historic_rates})
@app.listener("before_server_start")
async def initialize_scheduler(app, loop):
await db.set_bind(getenv("DATABASE_URL", "postgresql://localhost/exchangerates"),
json_serializer=ujson.dumps,
json_deserializer=ujson.loads
)
# # Check that tables exist
await db.gino.create_all()
print('set up db')
# Schedule exchangerate updates
try:
_ = open("scheduler.lock", "w")
fcntl.lockf(_.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
scheduler = AsyncIOScheduler()
scheduler.start()
# Updates lates 90 days data
scheduler.add_job(update_rates, "interval", hours=1)
# Fill up database with rates
count = await db.func.count(ExchangeRates.date).gino.scalar()
scheduler.add_job(update_rates, kwargs={"historic": True})
print('set up scheduler for fetch rates')
except BlockingIOError:
pass
@app.middleware("request")
async def force_ssl(request):
if request.headers.get("X-Forwarded-Proto") == "http":
return redirect(request.url.replace("http://", "https://", 1), status=301)
@app.middleware("request")
async def force_naked_domain(request):
if request.host.startswith("www."):
return redirect(request.url.replace("www.", "", 1), status=301)
@app.route("/latest", methods=["GET", "HEAD"])
@cors()
async def exchange_rates(request, date=None):
return await to_exchange_rates(request, date)
@app.route("/<date>", methods=["GET", "HEAD"])
@cors()
async def exchange_rates(request, date=None):
return await to_exchange_rates(request, date)
@app.route("/api/latest", methods=["GET", "HEAD"])
@cors()
async def exchange_rates(request, date=None):
return await to_exchange_rates(request, date)
@app.route("/api/<date>", methods=["GET", "HEAD"])
@cors()
async def exchange_rates(request, date=None):
return await to_exchange_rates(request, date)
@app.route("/history", methods=["GET", "HEAD"])
@cors()
async def exchange_history(request):
return await exchange_rates_history(request)
@app.route("/api/history", methods=["GET", "HEAD"])
@cors()
async def exchange_history(request):
return await exchange_rates_history(request)
# api.ExchangeratesAPI.io
@app.route("/", methods=["GET"])
async def index(request):
home = {
"help": [
{
"description": "Get the latest foreign exchange rates.",
"type": "GET",
"url": "/latest"
},
{
"description": "Get historical rates for any day since 1999-01-04.",
"type": "GET",
"url": "/2018-03-26"
},
{
"description": "Rates are quoted against the Euro by default. Quote against a different currency by setting the base parameter in your request.",
"type": "GET",
"url": "/latest?base=USD"
},
{
"description": "Request specific exchange rates by setting the symbols parameter.",
"type": "GET",
"url": "/latest?symbols=USD,GBP"
},
{
"description": "Get historical rates for a time period.",
"type": "GET",
"url": "/history?start_at=2018-01-01&end_at=2018-09-01"
},
{
"description": "Limit results to specific exchange rates to save bandwidth with the symbols parameter.",
"type": "GET",
"url": "/history?start_at=2018-01-01&end_at=2018-09-01&symbols=ILS,JPY"
},
{
"description": "Quote the historical rates against a different currency.",
"type": "GET",
"url": "/history?start_at=2018-01-01&end_at=2018-09-01&base=USD"
}
],
"git_repo": "https://tim.bai.uno/currency"
}
return json(home, escape_forward_slashes=False)
# Website
# @app.route("/", methods=["GET", "HEAD"])
# async def index(request):
# if request.method == "HEAD":
# return html("")
# return await file("./templates/index.html")
# Static content
app.static("/static", "./static")
app.static("/robots.txt", "./static/robots.txt")
# app.static("/favicon.ico", "./static/favicon.ico")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, workers=2)