-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter.py
66 lines (58 loc) · 1.91 KB
/
adapter.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
from bridge import Bridge
class Adapter:
base_url = 'https://min-api.cryptocompare.com/data/price'
from_params = ['base', 'from', 'coin']
to_params = ['quote', 'to', 'market']
def __init__(self, input):
self.id = input.get('id', '1')
self.request_data = input.get('data')
if self.validate_request_data():
self.bridge = Bridge()
self.set_params()
self.create_request()
else:
self.result_error('No data provided')
def validate_request_data(self):
if self.request_data is None:
return False
if self.request_data == {}:
return False
return True
def set_params(self):
for param in self.from_params:
self.from_param = self.request_data.get(param)
if self.from_param is not None:
break
for param in self.to_params:
self.to_param = self.request_data.get(param)
if self.to_param is not None:
break
def create_request(self):
try:
params = {
'fsym': self.from_param,
'tsyms': self.to_param,
}
response = self.bridge.request(self.base_url, params)
data = response.json()
self.result = data[self.to_param]
data['result'] = self.result
self.result_success(data)
except Exception as e:
self.result_error(e)
finally:
self.bridge.close()
def result_success(self, data):
self.result = {
'jobRunID': self.id,
'data': data,
'result': self.result,
'statusCode': 200,
}
def result_error(self, error):
self.result = {
'jobRunID': self.id,
'status': 'errored',
'error': f'There was an error: {error}',
'statusCode': 500,
}