forked from mikeghen/python_api_samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookerapi.py
491 lines (425 loc) · 16.8 KB
/
lookerapi.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# -*- coding: UTF-8 -*-
import requests
from pprint import pprint as pp
import json
import re
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
class LookerApi(object):
def __init__(self, token, secret, host):
self.token = token
self.secret = secret
self.host = host
self.session = requests.Session()
self.session.verify = False
self.auth()
def auth(self):
url = '{}{}'.format(self.host,'login')
params = {'client_id':self.token,
'client_secret':self.secret}
r = self.session.post(url,params=params)
access_token = r.json().get('access_token')
# print access_token
self.session.headers.update({'Authorization': 'token {}'.format(access_token)})
# POST /dashboards/{dashboard_id}/prefetch
def create_prefetch(self, dashboard_id, ttl):
url = '{}{}/{}/prefetch'.format(self.host,'dashboards',dashboard_id)
params = json.dumps({'ttl':ttl,
})
print url
print params
r = self.session.post(url,data=params)
pp(r.request.url)
pp(r.request.body)
pp(r.json())
# GET
def get_dashboard(self, dashboard_id,fields=''):
url = '{}{}/{}'.format(self.host,'dashboards',dashboard_id)
params = {"fields":fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# PATCH
def update_dashboard(self,dashboard_id,body={}):
url = '{}{}/{}'.format(self.host,'dashboards',dashboard_id)
body = json.dumps(body)
print " --- updating dashboard --- "
r = self.session.patch(url,data=body)
if r.status_code == requests.codes.ok:
return r.json()
def get_look_info(self,look_id,fields=''):
url = '{}{}/{}'.format(self.host,'looks',look_id)
print url
params = {"fields":fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET /queries/
def get_query(self,query_id,fields=''):
url = '{}{}/{}'.format(self.host,'queries',query_id)
print url
params = {"fields":fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# POST /queries/
def create_query(self,query_body, fields=[]):
url = '{}{}'.format(self.host,'queries')
# print url
params = json.dumps(query_body)
print " --- creating query --- "
r = self.session.post(url,data=params, params = json.dumps({"fields": fields}))
# print r.text
# print r.status_code
if r.status_code == requests.codes.ok:
return r.json()
#GET queries/run/
def run_query(self,query_id):
url = '{}{}/{}/run/json'.format(self.host,'queries',query_id)
# print url
params = {}
print " --- running query --- "
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
#GET queries/run/
def run_inline_query(self,body={}):
url = '{}{}/run/json'.format(self.host,'queries')
# print url
params = json.dumps(body)
# print " --- running query --- "
r = self.session.post(url,data=params)
# print url
# print r.status_code
# print r
if r.status_code == requests.codes.ok:
return r.json()
# GET /looks/<look_id>/run/<format>
def get_look(self,look_id, format='json', limit=500):
url = '{}{}/{}/run/{}'.format(self.host,'looks',look_id, format)
print url
params = {limit:100000}
r = self.session.get(url,params=params, stream=True)
if r.status_code == requests.codes.ok:
return r.json()
# PATCH /looks/<look_id>
def update_look(self,look_id,body,fields=''):
url = '{}{}/{}'.format(self.host,'looks',look_id)
body = json.dumps(body)
params = {"fields":fields}
print " --- updating look --- "
r = self.session.patch(url,data=body,params=params)
if r.status_code == requests.codes.ok:
return r.json()
def download_look(self,look_id, format='xlsx'):
url = '{}{}/{}/run/{}'.format(self.host,'looks',look_id, format)
params = {}
r = self.session.get(url,params=params, stream=True)
print r.status_code
if r.status_code == requests.codes.ok:
image_name = 'test2.xlsx'
with open(image_name, 'wb') as f:
for chunk in r:
f.write(chunk)
else:
print r.json()
return 'done'
def create_look(self,look_body):
url = '{}{}'.format(self.host,'looks')
print url
params = json.dumps(look_body)
r = self.session.post(url,data=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET /users
def get_all_users(self):
url = '{}{}'.format(self.host,'users')
# print "Grabbing Users " + url
params = {}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET /users/id
def get_user(self,id=""):
url = '{}{}{}'.format(self.host,'users/',id)
# print "Grabbing User(s) " + str(id)
# print url
params = {}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# PATCH /users/id
def update_user(self,id="",body={}):
url = '{}{}{}'.format(self.host,'users/',id)
# print "Grabbing User(s) " + str(id)
print url
params = json.dumps(body)
r = self.session.patch(url,data=params)
if r.status_code == requests.codes.ok:
return r.json()
# DELETE /users/id
def delete_user(self,id="",body={}):
url = '{}{}{}'.format(self.host,'users/',id)
# print "Grabbing User(s) " + str(id)
# print url
# params = json.dumps(body)
r = self.session.delete(url)
if r.status_code == requests.codes.ok:
return r.json()
# GET /user
def get_current_user(self):
url = '{}{}'.format(self.host,'user')
params = {}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# PUT /users/{user_id}/roles
def set_user_role(self,id="", body={}):
url = '{}{}{}{}'.format(self.host,'users/',id,'/roles')
# print "Grabbing User(s) " + str(id)
# print url
params = json.dumps(body)
r = self.session.post(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET /users/{user_id}/roles
def get_user_role(self,id=""):
url = '{}{}{}{}'.format(self.host,'users/',id,'/roles')
# print "Grabbing User(s) " + str(id)
# print url
r = self.session.get(url,params={})
if r.status_code == requests.codes.ok:
return r.json()
def get_roles(self):
url = '{}{}'.format(self.host,'roles')
# print "Grabbing role(s) "
# print url
r = self.session.get(url,params={})
if r.status_code == requests.codes.ok:
return r.json()
# PATCH /users/{user_id}/access_filters/{access_filter_id}
def update_access_filter(self, user_id = 0, access_filter_id = 0, body={}):
url = '{}{}/{}/{}/{}'.format(self.host,'users',user_id,'access_filters',access_filter_id)
params = json.dumps(body)
r = self.session.patch(url,data=params)
return r.json()
def create_access_filter(self, user_id = 0, body={}):
url = '{}{}/{}/{}'.format(self.host,'users',user_id,'access_filters')
params = json.dumps(body)
r = self.session.post(url,data=params)
return r.json()
# GET /users/me
def get_me(self):
url = '{}{}'.format(self.host,'user')
print "Grabbing Myself: " + url
params = {}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET /lookml_models/
def get_models(self,fields={}):
url = '{}{}'.format(self.host,'lookml_models')
# print url
params = fields
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET /lookml_models/{{NAME}}
def get_model(self,model_name="",fields={}):
url = '{}{}/{}'.format(self.host,'lookml_models', model_name)
print url
params = fields
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET /lookml_models/{{NAME}}/explores/{{NAME}}
def get_explore(self,model_name=None,explore_name=None,fields={}):
url = '{}{}/{}/{}/{}'.format(self.host,'lookml_models', model_name, 'explores', explore_name)
# print url
params = fields
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
#GET /scheduled_plans/dashboard/{dashboard_id}
def get_dashboard_schedule(self,dashboard_id=0):
url = '{}{}/{}/{}'.format(self.host,'scheduled_plans', 'dashboard', dashboard_id)
# print url
r = self.session.get(url)
if r.status_code == requests.codes.ok:
return r.json()
#GET /scheduled_plans
def get_all_schedules(self, user_id=False):
url = '{}{}'.format(self.host,'scheduled_plans')
# print url
params = {'user_id':user_id}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
#GET /scheduled_plans/look/{dashboard_id}
def get_look_schedule(self,look_id=0):
url = '{}{}/{}/{}'.format(self.host,'scheduled_plans', 'look', look_id)
# print url
r = self.session.get(url)
if r.status_code == requests.codes.ok:
return r.json()
# GET /datagroups
def get_datagroups(self):
url = '{}{}'.format(self.host,'datagroups')
r = self.session.get(url)
if r.status_code == requests.codes.ok:
return r.json()
#PATCH /scheduled_plans/{scheduled_plan_id}
def update_schedule(self, plan_id, body={}):
url = '{}{}/{}'.format(self.host,'scheduled_plans',plan_id)
params = json.dumps(body)
# print url
# print params
r = self.session.patch(url,data=params)
# pp(r.request.url)
# pp(r.request.body)
return r.json()
def sql_runner(self):
connection_id = "looker"
sql = "select * from events limit 10"
body = {}
body['sql'] = sql
body['connection_id'] = connection_id
url = '{}{}'.format(self.host,'sql_queries')
params = json.dumps(body)
r = self.session.post(url,data=params)
slug = r.json()['slug']
url = '{}{}/{}'.format(self.host,'sql_queries', slug)
g = self.session.get(url)
print url
return g.json()
#DELETE /scheduled_plans/{scheduled_plan_id}
def delete_schedule(self, plan_id):
url = '{}{}/{}'.format(self.host,'scheduled_plans', plan_id)
# print url
r = self.session.delete(url)
if r.status_code == requests.codes.ok:
return r.json()
#DELETE /looks/{look_id}
def delete_look(self,look_id,fields=''):
url = '{}{}/{}'.format(self.host,'looks',look_id)
print url
params = {"fields":fields}
r = self.session.delete(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
#DELETE /dashboards/{dashboard_id}
def delete_dashboard(self,dashboard_id,fields=''):
url = '{}{}/{}'.format(self.host,'dashboards',dashboard_id)
print url
params = {"fields":fields}
r = self.session.delete(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET /dashboards/dashboard_filters/{dashboard_id}
def get_dashboard_dashboard_filters(self,dashboard_id,fields=''):
url = '{}{}/{}/{}'.format(self.host,'dashboards',dashboard_id,'dashboard_filters')
print url
params = {"fields":fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# PATCH /dashboard_filters/{dashboard_filter_id}
def update_dashboard_filter(self,dashboard_filter_id,model_name,fields=''):
url = '{}{}/{}'.format(self.host,'dashboard_filters',dashboard_filter_id)
print url
body = json.dumps({'model': model_name})
params = {"fields":fields}
r = self.session.patch(url,data=body,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# POST /groups/{group_id}/users
def add_users_to_group(self,group_id,user_id):
url = '{}{}/{}/{}'.format(self.host,'groups',group_id,'users')
print url
params = json.dumps({'user_id': user_id})
r = self.session.post(url,data=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET spaces
def get_all_spaces(self,fields=''):
url = '{}{}'.format(self.host,'spaces')
print url
params = {'fields':fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET content_metadata_access
def get_all_content_metadata_access(self,content_metadata_id,fields=''):
url = '{}{}'.format(self.host,'content_metadata_access')
print url
params = {'content_metadata_id':content_metadata_id,'fields':fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# DELETE content_metadata_access
def delete_content_metadata(self,content_metadata_access_id):
url = '{}{}/{}'.format(self.host,'content_metadata_access',content_metadata_access_id)
print url
r = self.session.delete(url)
if r.status_code == requests.codes.ok:
return r.json()
#GET /groups/{group_id}
def get_all_groups(self,fields=''):
url = '{}{}'.format(self.host,'groups')
print url
params = {"fields":fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
#GET /groups/{group_id}
def get_group(self,group_id,fields=''):
url = '{}{}/{}'.format(self.host,'groups',group_id)
print url
params = {"fields":fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
#POST /groups
def create_group(self,group_name,fields=''):
url = '{}{}'.format(self.host,'groups')
print url
params = json.dumps({'name': group_name})
r = self.session.post(url,data=params,params=json.dumps({"fields": fields}))
if r.status_code == requests.codes.ok:
return r.json()
# /groups/{group_id}/groups
def create_group_in_group(self,parent_group_id,child_group_id):
url = '{}{}/{}/{}'.format(self.host,'groups',parent_group_id,'groups')
print url
params = json.dumps({'group_id': child_group_id})
r = self.session.post(url,data=params)
if r.status_code == requests.codes.ok:
return "successful addition of group " + str(parent_group_id)
# POST /users/{user_id}/credentials_email
def create_users_email_credentials(self,user_id,email_address):
url = '{}{}/{}/{}'.format(self.host,'users',user_id,'credentials_email')
print url
params = json.dumps({'email': email_address})
r = self.session.post(url,data=params)
if r.status_code == requests.codes.ok:
return r.json()
def get_users_email_credentials(self,user_id,fields):
url = '{}{}/{}/{}'.format(self.host,'users',user_id,'credentials_email')
print url
params = {"fields":fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
def get_users_saml_credentials(self,user_id,fields):
url = '{}{}/{}/{}'.format(self.host,'users',user_id,'credentials_saml')
print url
params = {"fields":fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
def delete_users_saml_credentials(self,user_id):
url = '{}{}/{}/{}'.format(self.host,'users',user_id,'credentials_saml')
print url
r = self.session.delete(url)
if r.status_code == requests.codes.ok:
return r.json()