-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.py
39 lines (30 loc) · 998 Bytes
/
bridge.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
import os
import quandl
from flask import current_app
from cache import cache
class Bridge:
def __init__(self):
if 'QUANDL_API_KEY' in current_app.config:
quandl.ApiConfig.api_key = current_app.config['QUANDL_API_KEY']
else:
quandl.ApiConfig.api_key = os.environ['QUANDL_API_KEY']
@cache.memoize()
def request(self, data):
"""
:param data: JSON Object to feed Quandl API request
:return: JSON object with the response from he API
"""
current_app.logger.debug("Cache missed")
try:
rows = 1
if 'rows' in data:
rows = data.get('rows')
df = quandl.get(dataset=data.get('dataset'), rows=rows)
if df['Value'].size == 1:
return df['Value'].get(0)
else:
return df['Value'].to_list()
except Exception as e:
raise e
def __repr__(self):
return f'{"Bridge()"}'