/
json_ds.py
284 lines (209 loc) · 8.63 KB
/
json_ds.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
import datetime
import logging
from urllib.parse import urljoin
import yaml
from funcy import compact, project
from redash.query_runner import (
TYPE_BOOLEAN,
TYPE_DATETIME,
TYPE_FLOAT,
TYPE_INTEGER,
TYPE_STRING,
BaseHTTPQueryRunner,
register,
)
class QueryParseError(Exception):
pass
def parse_query(query):
# TODO: copy paste from Metrica query runner, we should extract this into a utility
query = query.strip()
if query == "":
raise QueryParseError("Query is empty.")
try:
params = yaml.safe_load(query)
return params
except ValueError as e:
logging.exception(e)
error = str(e)
raise QueryParseError(error)
TYPES_MAP = {
str: TYPE_STRING,
bytes: TYPE_STRING,
int: TYPE_INTEGER,
float: TYPE_FLOAT,
bool: TYPE_BOOLEAN,
datetime.datetime: TYPE_DATETIME,
}
def _get_column_by_name(columns, column_name):
for c in columns:
if "name" in c and c["name"] == column_name:
return c
return None
def _get_type(value):
return TYPES_MAP.get(type(value), TYPE_STRING)
def add_column(columns, column_name, column_type):
if _get_column_by_name(columns, column_name) is None:
columns.append({"name": column_name, "friendly_name": column_name, "type": column_type})
def _apply_path_search(response, path, default=None):
if path is None:
return response
path_parts = path.split(".")
path_parts.reverse()
while len(path_parts) > 0:
current_path = path_parts.pop()
if current_path in response:
response = response[current_path]
elif default is not None:
return default
else:
raise Exception("Couldn't find path {} in response.".format(path))
return response
def _normalize_json(data, path):
if not data:
return None
data = _apply_path_search(data, path)
if isinstance(data, dict):
data = [data]
return data
def _sort_columns_with_fields(columns, fields):
if fields:
columns = compact([_get_column_by_name(columns, field) for field in fields])
return columns
# TODO: merge the logic here with the one in MongoDB's queyr runner
def parse_json(data, fields):
rows = []
columns = []
for row in data:
parsed_row = {}
for key in row:
if isinstance(row[key], dict):
for inner_key in row[key]:
column_name = "{}.{}".format(key, inner_key)
if fields and key not in fields and column_name not in fields:
continue
value = row[key][inner_key]
add_column(columns, column_name, _get_type(value))
parsed_row[column_name] = value
else:
if fields and key not in fields:
continue
value = row[key]
add_column(columns, key, _get_type(value))
parsed_row[key] = row[key]
rows.append(parsed_row)
columns = _sort_columns_with_fields(columns, fields)
return {"rows": rows, "columns": columns}
class JSON(BaseHTTPQueryRunner):
requires_url = False
base_url_title = "Base URL"
@classmethod
def configuration_schema(cls):
return {
"type": "object",
"properties": {
"base_url": {"type": "string", "title": cls.base_url_title},
"username": {"type": "string", "title": cls.username_title},
"password": {"type": "string", "title": cls.password_title},
},
"secret": ["password"],
"order": ["base_url", "username", "password"],
}
def __init__(self, configuration):
super(JSON, self).__init__(configuration)
self.syntax = "yaml"
def test_connection(self):
pass
def run_query(self, query, user):
query = parse_query(query)
data, error = self._run_json_query(query)
if error is not None:
return None, error
if data:
return data, None
return None, "Got empty response from '{}'.".format(query["url"])
def _run_json_query(self, query):
if not isinstance(query, dict):
raise QueryParseError("Query should be a YAML object describing the URL to query.")
if "url" not in query:
raise QueryParseError("Query must include 'url' option.")
method = query.get("method", "get")
request_options = project(query, ("params", "headers", "data", "auth", "json", "verify"))
fields = query.get("fields")
path = query.get("path")
if "pagination" in query:
pagination = RequestPagination.from_config(self.configuration, query["pagination"])
else:
pagination = None
if isinstance(request_options.get("auth", None), list):
request_options["auth"] = tuple(request_options["auth"])
elif self.configuration.get("username") or self.configuration.get("password"):
request_options["auth"] = (self.configuration.get("username"), self.configuration.get("password"))
if method not in ("get", "post"):
raise QueryParseError("Only GET or POST methods are allowed.")
if fields and not isinstance(fields, list):
raise QueryParseError("'fields' needs to be a list.")
results, error = self._get_all_results(query["url"], method, path, pagination, **request_options)
return parse_json(results, fields), error
def _get_all_results(self, url, method, result_path, pagination, **request_options):
"""Get all results from a paginated endpoint."""
base_url = self.configuration.get("base_url")
url = urljoin(base_url, url)
results = []
has_more = True
while has_more:
response, error = self._get_json_response(url, method, **request_options)
has_more = False
result = _normalize_json(response, result_path)
if result:
results.extend(result)
if pagination:
has_more, url, request_options = pagination.next(url, request_options, response)
return results, error
def _get_json_response(self, url, method, **request_options):
response, error = self.get_response(url, http_method=method, **request_options)
result = response.json() if error is None else {}
return result, error
class RequestPagination:
def next(self, url, request_options, response):
"""Checks the response for another page.
Returns:
has_more, next_url, next_request_options
"""
return False, None, request_options
@staticmethod
def from_config(configuration, pagination):
if not isinstance(pagination, dict) or not isinstance(pagination.get("type"), str):
raise QueryParseError("'pagination' should be an object with a `type` property")
if pagination["type"] == "url":
return UrlPagination(pagination)
elif pagination["type"] == "token":
return TokenPagination(pagination)
raise QueryParseError("Unknown 'pagination.type' {}".format(pagination["type"]))
class UrlPagination(RequestPagination):
def __init__(self, pagination):
self.path = pagination.get("path", "_links.next.href")
if not isinstance(self.path, str):
raise QueryParseError("'pagination.path' should be a string")
def next(self, url, request_options, response):
next_url = _apply_path_search(response, self.path, "")
if not next_url:
return False, None, request_options
next_url = urljoin(url, next_url)
return True, next_url, request_options
class TokenPagination(RequestPagination):
def __init__(self, pagination):
self.fields = pagination.get("fields", ["next_page_token", "page_token"])
if not isinstance(self.fields, list) or len(self.fields) != 2:
raise QueryParseError("'pagination.fields' should be a list of 2 field names")
def next(self, url, request_options, response):
next_token = _apply_path_search(response, self.fields[0], "")
if not next_token:
return False, None, request_options
params = request_options.get("params", {})
# prevent infinite loop that can happen if self.fields[1] is wrong
if next_token == params.get(self.fields[1]):
raise Exception("{} did not change; possible misconfiguration".format(self.fields[0]))
params[self.fields[1]] = next_token
request_options["params"] = params
return True, url, request_options
register(JSON)