-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
151 lines (99 loc) · 4.08 KB
/
__init__.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
import requests
class Popily:
def __init__(self, token, url=None):
self.auth_header = {'Authorization': 'Token ' + token}
self.BASE_URL = 'https://popily.com'
if url:
self.BASE_URL = url
def _pack_filters(self, filters):
packed_str = ''
for i,f in enumerate(filters):
op = 'eq'
if 'op' in f:
op = f['op']
filter_str = ''
if i > 0:
filter_str += '__'
filter_str += f['column'] + '!' + op + '!' + ','.join(f['values'])
packed_str += filter_str
return packed_str
def _assign_editables(self, data_dict, kwargs):
editables = [
'title',
'x_label',
'y_label',
'z_label',
'category_order',
'time_interval',
'refresh_rate'
]
for key in editables:
if key in kwargs:
data_dict[key] = kwargs[key]
return data_dict
def add_source(self, **kwargs):
endpoint = self.BASE_URL + '/api/sources/'
post_data = {}
for key in ['columns', 'title', 'description']:
if key in kwargs:
post_data[key] = kwargs[key]
if 'url' in kwargs:
post_data['url'] = kwargs['url']
r = requests.post(endpoint, json=post_data,
headers=self.auth_header)
elif 'file_obj' in kwargs:
r = requests.post(endpoint, data=post_data,
files=kwargs['file_obj'], headers=self.auth_header)
elif 'connection_string' in kwargs:
post_data['connection_string'] = kwargs['connection_string']
post_data['query'] = kwargs['query']
r = requests.post(endpoint, json=post_data,
headers=self.auth_header)
else:
raise('url or file_obj is required')
return r.json()
def get_sources(self):
endpoint = self.BASE_URL + '/api/sources/'
r = requests.get(endpoint, headers=self.auth_header)
return r.json()
def get_source(self, source_id):
endpoint = self.BASE_URL + '/api/sources/' + str(source_id) + '/'
r = requests.get(endpoint, headers=self.auth_header)
return r.json()
def get_insights(self, source_id, **kwargs):
endpoint = self.BASE_URL + '/api/insights/'
payload = {'source': source_id}
for key in ['columns', 'insight_types', 'insight_actions']:
if key in kwargs:
payload[key] = ','.join(kwargs[key])
if 'filters' in kwargs:
payload['filters'] = self._pack_filters(kwargs['filters'])
if 'full' in kwargs:
payload['full'] = kwargs['full']
if 'single' in kwargs:
payload['single'] = kwargs['single']
for key in ['full', 'height', 'width']:
if key in kwargs:
payload[key] = kwargs[key]
payload = self._assign_editables(payload, kwargs)
r = requests.get(endpoint, headers=self.auth_header,params=payload)
return r.json()
def get_insight(self, insight_id, **kwargs):
endpoint = self.BASE_URL + '/api/insights/' + str(insight_id) + '/'
payload = {}
if 'filters' in kwargs:
payload['filters'] = self._pack_filters(kwargs['filters'])
for key in ['full', 'height', 'width']:
if key in kwargs:
payload[key] = kwargs[key]
payload = self._assign_editables(payload, kwargs)
r = requests.get(endpoint, headers=self.auth_header, params=payload)
return r.json()
def customize_insight(self, insight_id, **kwargs):
endpoint = self.BASE_URL + '/api/insights/' + str(insight_id) + '/'
post_data = {}
post_data = self._assign_editables(post_data, kwargs)
if 'filters' in kwargs:
post_data['filters'] = self._pack_filters(kwargs['filters'])
r = requests.put(endpoint, json=post_data, headers=self.auth_header)
return r.json()