-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dataset_APIs.py
417 lines (236 loc) · 11.2 KB
/
Dataset_APIs.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
'''
Functions to pull/send data to dataflow APIs.
Contact: [email protected]
Thanks to Ben Watt at datalineo.com for the Microsoft Authentication code.
https://www.datalineo.com/post/power-bi-rest-api-with-python-and-microsoft-authentication-library-msal
'''
import msal
import requests
import json
import Dataflow_APIs
import time
'''
resultFunction
I made this function so that I could assign "result" a value from another program.
The result variable is needed and used by almost every function and only needs
to be pulled once. When referring to Dataflow_APIs in Dataset_GUI, I call a
function from Dataflow_APIs that pulls the result (token) using msal. In order to
inititialize "result" in Dataset_APIs, I needed to make this function so it can be
used in this program. As opposed to calling for another token within Dataset_APIs.
'''
def resultFunction(result1):
global result
result = result1
def connect(email,pword,client_id,authority_url):
global app,result,guiLoginMsg
username = str(email)
password = str(pword)
scope = ["https://analysis.windows.net/powerbi/api/.default"]
url_groups = 'https://api.powerbi.com/v1.0/myorg/groups'
# --------------------------------------------------
# Use MSAL to grab a token
# --------------------------------------------------
app = msal.PublicClientApplication(client_id, authority=authority_url)
result = app.acquire_token_by_username_password(username=username,password=password,scopes=scope)
if 'access_token' in result:
print("Successful login")
guiLoginMsg = "Successful login "
else:
guiLoginMsg = "Wrong login credentials"
print(result.get("error"))
print(result.get("error_description"))
return result
'''
makeDatasetDict()
Returns a dictionary of datasets from "My Workspace". name : id
https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/getdatasets
'''
def makeDatasetDict():
global datasetDict
datasetDict = dict()
url = 'https://api.powerbi.com/v1.0/myorg/datasets'
if 'access_token' in result:
access_token = result['access_token']
header = {'Content-Type':'application/json','Authorization': f'Bearer {access_token}'}
api_out = requests.get(url=url, headers=header)
else:
print(result.get("error"))
print(result.get("error_description"))
initDict = api_out.json()
initDict = initDict['value']
for dic in initDict: # iterating through the list of dictionaries
ID = dic['id']
name = dic['name']
datasetDict[name] = ID
return datasetDict
'''
refreshDataset(datasetId)
Triggers a refresh for the specified dataset from "My Workspace".
In Shared capacities this call is limited to eight times per day (including refreshes executed via Scheduled Refresh)
In Premium capacities this call is not limited in number of times per day, but only by the available resources in the capacity,
hence if overloaded, the refresh execution may be throttled until the load is reduced. If this throttling exceeds 1 hour, the refresh will fail.
POST https://api.powerbi.com/v1.0/myorg/datasets/{datasetId}/refreshes
https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/refreshdataset
# NOTE: This function will only work for a dataset in My Workspace !
# refreshDataset2() let's you choose the workspace (groupId) that you need to refresh from.
'''
testDataset = 'Item Maintenance Process Report beta'
def refreshDataset(datasetId):
url = f"https://api.powerbi.com/v1.0/myorg/datasets/{datasetId}/refreshes"
if 'access_token' in result:
access_token = result['access_token']
header = {'Content-Type':'application/json','Authorization': f'Bearer {access_token}'}
data = {"refreshRequest":"y"}
api_out = requests.post(url=url, json=data, headers=header)
if api_out.status_code == 202:
print(f"Successful refresh of dataset: {testDataset} <> {datasetId}")
return api_out.status_code
else:
print(result.get("error"))
print(result.get("error_description"))
'''
refreshDataset2(groupId, datasetId)
Triggers a refresh for the specified dataset (datasetId) from the specified workspace (groupId).
https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/refreshdatasetingroup
# POST https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/refreshes
'''
testDataset2 = 'xx'
def refreshDataset2(groupId, datasetId):
global refreshMsg
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
url = f"https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/refreshes"
if 'access_token' in result:
access_token = result['access_token']
header = {'Content-Type':'application/json','Authorization': f'Bearer {access_token}'}
data = {"refreshRequest":"y"}
api_out = requests.post(url=url, json=data, headers=header)
if api_out.status_code == 202:
print(f"Successful refresh of dataset: {datasetId}")
refreshMsg = f"Successful Refresh @ {current_time} local time"
else:
code = str(api_out.status_code)
refreshMsg = f"Failed to refresh. Status code {code}"
print(api_out.status_code)
return api_out.status_code
else:
refreshMsg = "No access token in result"
print(result.get("error"))
print(result.get("error_description"))
'''
getDatasetsInGroup(groupId)
Returns a dictionary of datasets from the specified groupId in the format name : id
# GET https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets
https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/getdatasetsingroup
'''
def getDatasetsInGroup(groupId):
url = f"https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets"
if 'access_token' in result:
access_token = result['access_token']
header = {'Content-Type':'application/json','Authorization': f'Bearer {access_token}'}
api_out = requests.get(url=url, headers=header)
if api_out.status_code == 200:
print("Success")
jsonDict = api_out.json()
jsonDict = jsonDict['value']
else:
print(api_out.status_code)
print(result.get("error"))
print(result.get("error_description"))
datasetDict1 = dict()
for dic in jsonDict: # iterating through the list of dictionaries and making a new one with name : id
ID = dic['id']
name = dic['name']
datasetDict1[name] = ID
return datasetDict1
'''
https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/updaterefreshscheduleingroup
PATCH https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/refreshSchedule
'''
def updateRefSched(groupId,datasetId,refDays,refTimes):
global schedMsg
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
url = f"https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/refreshSchedule"
if 'access_token' in result:
access_token = result['access_token']
header = {'Content-Type':'application/json','Authorization': f'Bearer {access_token}'}
data = {
"value": {
"days": refDays,
"times": refTimes,
"localTimeZoneId": "UTC",
}
}
api_out = requests.patch(url=url, json=data, headers=header)
if api_out.status_code == 200:
schedMsg = f"Refresh Schedule Updated Successfully @ {current_time} local time"
print(f"Successful refresh schedule update of dataset: <> {datasetId}")
else:
code = str(api_out.status_code)
schedMsg = f"Failed. Code {code} "
return api_out.status_code
else:
schedMsg = "No token in result"
print(result.get("error"))
print(result.get("error_description"))
'''
Returns the refresh history of the specified dataset from the specified workspace.
https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/getrefreshhistoryingroup
GET https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/refreshes
or
GET https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/refreshes?$top={$top}
'''
def getRefHistory(groupId,datasetId,numRecords):
url = f"https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/refreshes?$top={numRecords}"
if 'access_token' in result:
access_token = result['access_token']
header = {'Content-Type':'application/json','Authorization': f'Bearer {access_token}'}
api_out = requests.get(url=url, headers=header)
if api_out.status_code == 200:
print("Success")
jsonDict = api_out.json()
print()
print("--"*100)
jsonDict = jsonDict['value']
else:
print(api_out.status_code)
print(result.get("error"))
print(result.get("error_description"))
refHisStr = ''
for dic in jsonDict: # iterating through the list of dictionaries and making a new one with name : id
refHisStr = refHisStr + "==========="*10 + '\n' + "==========="*10 + '\n'
for key in dic:
tempKeyValPair = str(key) + ' = ' + str(dic[key]) + '\n'
refHisStr = refHisStr + tempKeyValPair
return refHisStr
'''
https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/getrefreshscheduleingroup
GET https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/refreshSchedule
'''
def getRefSched(groupId,datasetId):
url = f"https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/refreshSchedule"
if 'access_token' in result:
access_token = result['access_token']
header = {'Content-Type':'application/json','Authorization': f'Bearer {access_token}'}
api_out = requests.get(url=url, headers=header)
if api_out.status_code == 200:
print("Success")
jsonDict = api_out.json()
print()
del jsonDict['@odata.context']
print(jsonDict)
else:
print(api_out.status_code)
print(result.get("error"))
print(result.get("error_description"))
refSchedStr = ''
for key in jsonDict:
tempKeyValPair = str(key) + ' = ' + str(jsonDict[key]) + '\n'
refSchedStr = refSchedStr + tempKeyValPair
return refSchedStr
def main():
'''
Test functions...
'''
#main()