-
Notifications
You must be signed in to change notification settings - Fork 0
/
power_bi_reports.module
743 lines (655 loc) · 22 KB
/
power_bi_reports.module
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
<?php
define('POWER_BI_REPORTS__GROUPS', 'https://api.powerbi.com/v1.0/myorg/groups');
define('POWER_BI_REPORTS__REPORTS_IN_GROUP', 'https://api.powerbi.com/v1.0/myorg/groups/{groupId}/reports');
define('POWER_BI_REPORTS__REPORT_EMBED_TOKEN', 'https://api.powerbi.com/v1.0/myorg/groups/{groupId}/reports/{reportId}/GenerateToken');
/**
* Implements hook_libraries_info().
*/
function power_bi_reports_libraries_info() {
$libraries['powerbi_javascript'] = [
'name' => 'Microsoft/PowerBI-JavaScript',
'vendor url' => 'https://github.com/Microsoft/PowerBI-JavaScript',
'download url' => 'https://github.com/Microsoft/PowerBI-JavaScript',
'version arguments' => [
'file' => 'package.json',
'pattern' => '/"version":\\s+"([0-9\.]+)"/',
'lines' => 10,
],
'files' => [
'js' => ['dist/powerbi.min.js'],
],
];
return $libraries;
}
/**
* Implements hook_menu().
*
* Provides a configuration page.
*/
function power_bi_reports_menu() {
$items['admin/config/services/power-bi-reports'] = [
'title' => 'Power BI Reports',
'access arguments' => ['administer site configuration'], // @TODO
'page callback' => 'drupal_get_form',
'page arguments' => ['power_bi_reports_status_form'],
'file' => 'power_bi_reports.admin.inc',
'description' => 'Power BI Reports status and configuration.',
'type' => MENU_NORMAL_ITEM,
];
$items['admin/config/services/power-bi-reports/status'] = [
'title' => 'Power BI Reports status',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
];
$items['admin/config/services/power-bi-reports/settings'] = [
'title' => 'Settings',
'access arguments' => ['administer site configuration'], // @TODO
'page callback' => 'drupal_get_form',
'page arguments' => ['power_bi_reports_admin_form'],
'file' => 'power_bi_reports.admin.inc',
'description' => 'Configure Power BI Reports module.',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
];
$items['power-bi-reports/get-embed-token/%'] = [
'page callback' => '_power_bi_reports_get_embed_token_callback',
'page arguments' => [2],
'access callback' => TRUE,
'type' => MENU_CALLBACK,
];
return $items;
}
/**
* Implements hook_field_info().
*/
function power_bi_reports_field_info() {
$info = [];
$info['power_bi_reports_report'] = [
'label' => t('Power BI Report'),
'description' => t('Hosts a Power BI Report.'),
'default_widget' => 'power_bi_reports_select_widget',
'default_formatter' => 'power_bi_reports_simple_embed_formatter',
];
return $info;
}
/**
* Implements hook_field_is_empty().
*/
function power_bi_reports_field_is_empty($item, $field) {
return empty($item['report_id']);
}
/**
* Implements hook_field_widget_info().
*/
function power_bi_reports_field_widget_info() {
require_once 'power_bi_reports.widgets.inc';
return _power_bi_reports_field_widget_info();
}
/**
* Implements hook_field_widget_form().
*/
function power_bi_reports_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
require_once 'power_bi_reports.widgets.inc';
return _power_bi_reports_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
}
/**
* Implements hook_field_formatter_info().
*/
function power_bi_reports_field_formatter_info() {
require_once 'power_bi_reports.formatters.inc';
return _power_bi_reports_field_formatter_info();
}
/**
* Implements hook_field_formatter_view().
*/
function power_bi_reports_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
require_once 'power_bi_reports.formatters.inc';
return _power_bi_reports_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
}
function power_bi_reports_is_configured() {
return variable_get('power_bi_reports_tenant_id') &&
variable_get('power_bi_reports_client_id') &&
variable_get('power_bi_reports_username') &&
variable_get('power_bi_reports_password');
}
/**
* Returns HTML for the report rendering.
*
* @param array $variables
* An associative array containing:
* - report_id
* - group_id
* - embed_url
* - delta
*
* @return string
*/
function theme_power_bi_report($variables) {
$report_id = $variables['report_id'];
$group_id = $variables['group_id'];
$embed_url = $variables['embed_url'];
$attributes = [];
$attributes['class'] = 'power-bi-report-wrapper';
$attributes['data-power-bi-report-id'] = $report_id;
$attributes['data-power-bi-group-id'] = $group_id;
$attributes['data-power-bi-embed-url'] = $embed_url;
if (isset($variables['delta'])) {
$attributes['data-delta'] = $variables['delta'];
}
// @see https://stackoverflow.com/a/11427592
$attributes_string = implode(' ', array_map(
function ($v, $k) {
return sprintf("%s=\"%s\"", $k, $v);
},
$attributes,
array_keys($attributes)
));
$output = '<div ' . $attributes_string . '>
<div class="power-bi-report"></div>
<div class="power-bi-report-messages"></div>
</div>';
return $output;
}
/**
* Implements hook_theme().
*/
function power_bi_reports_theme($existing, $type, $theme, $path) {
return [
'power_bi_report' => [
'variables' => [
'report_id' => NULL,
'group_id' => NULL,
'embed_url' => NULL,
'delta' => NULL,
],
],
];
}
function power_bi_reports_get_form_element($form, &$form_state, $default_value = '') {
$power_bi_reports_data = variable_get('power_bi_reports_data', FALSE);
if (!$power_bi_reports_data) {
$power_bi_reports_data = power_bi_reports_sync_reports();
}
$element = [
'#type' => 'select',
'#title' => t('Report'),
'#options' => _power_bi_reports_get_grouped_options($power_bi_reports_data),
'#empty_value' => '',
'#default_value' => $default_value,
];
return $element;
}
/**
* Sync reports.
*
* @return array
*/
function power_bi_reports_sync_reports() {
$groups = [];
$groups_raw = power_bi_reports_get_groups();
foreach ($groups_raw as $group) {
$group_id = $group['id'];
$reports_in_group = power_bi_reports_get_reports_in_group($group_id);
$groups[$group_id] = $group;
$groups[$group_id]['reports'] = $reports_in_group;
}
variable_set('power_bi_reports_data', $groups);
variable_set('power_bi_reports_last_sync_reports', REQUEST_TIME);
return $groups;
}
function _power_bi_reports_get_grouped_options($power_bi_reports_data) {
$options = [];
foreach ($power_bi_reports_data as $group) {
foreach ($group['reports'] as $report) {
$options[$group['name']][$report['id']] = $report['name'];
}
}
return $options;
}
/**
* Gets a valid auth token.
*
* @param bool $force_renew
*
* @return mixed|void
*/
function power_bi_reports_get_auth_token($force_renew = FALSE) {
// First check if there's a non-expired cached auth token.
$access_token = variable_get('power_bi_reports_access_token', [
'expires' => 0,
'token' => '',
]);
$expires = $access_token['expires'];
if (!$force_renew && $expires > REQUEST_TIME) {
// There's a token that hasn't expired, use this.
return $access_token['token'];
}
else {
// Get a new token.
$client = new GuzzleHttp\Client();
$tenant_id = variable_get('power_bi_reports_tenant_id');
$endpoint = "https://login.windows.net/$tenant_id/oauth2/token";
$resource = variable_get('power_bi_reports_resource');
$client_id = variable_get('power_bi_reports_client_id');
$username = variable_get('power_bi_reports_username');
$password = variable_get('power_bi_reports_password');
$r = $client->post(
$endpoint,
[
'form_params' => [
'resource' => $resource,
'client_id' => $client_id,
'grant_type' => 'password',
'username' => $username,
'password' => $password,
'scope' => 'openid',
],
]
);
if ($r->getStatusCode() != 200) {
// @TODO handle error and return
return FALSE;
}
$response_json = json_decode($r->getBody());
$token = [
'expires' => $response_json->expires_on,
'token' => $response_json->access_token,
];
variable_set('power_bi_reports_access_token', $token);
return $token['token'];
}
}
/**
* Menu callback for `power-bi-reports/get-embed-token/%`
*
* Returns an embed token for the provided report.
*
* @param $report_id
*/
function _power_bi_reports_get_embed_token_callback($report_id) {
if (!power_bi_reports_validate_guid($report_id)) {
return drupal_json_output(FALSE);
}
// Check if there's a report with this guid synced in the system.
$report_data = _power_bi_reports_get_report_data($report_id);
if (empty($report_data)) {
return drupal_json_output(FALSE);
}
// First try to get a locally cached embed token.
$embed_token = power_bi_reports_get_local_report_embed_token($report_id);
if ($embed_token) {
return drupal_json_output($embed_token);
}
// Generate an embed token remotely, and then cache it locally.
$group_id = $report_data['group_id'];
$generated = power_bi_reports_generate_report_embed_token($group_id, $report_id);
if (!empty($generated)) {
return drupal_json_output($generated);
}
return drupal_json_output(FALSE);
}
function power_bi_reports_get_local_report_embed_token($report_id, $check_expiration = TRUE) {
$cid = 'power_bi_reports:' . $report_id;
$cache = cache_get($cid);
if ($cache) {
if (!$check_expiration) {
return $cache->data;
}
else {
if (time() <= $cache->expire) {
return $cache->data;
}
}
}
return FALSE;
}
function power_bi_reports_set_local_report_embed_token($report_id, $token, $expiration) {
$cid = 'power_bi_reports:' . $report_id;
$timestamp = strtotime($expiration);
cache_set($cid, $token, 'cache', $timestamp);
}
function power_bi_reports_get_reports_data() {
$power_bi_reports_data = variable_get('power_bi_reports_data', FALSE); // @TODO use drupal_static, here and everywhere else
if (!$power_bi_reports_data) {
$power_bi_reports_data = power_bi_reports_sync_reports();
}
return $power_bi_reports_data;
}
function _power_bi_reports_get_report_data($report_id) {
$power_bi_reports_data = power_bi_reports_get_reports_data();
foreach ($power_bi_reports_data as $group_id => $group) {
foreach ($group['reports'] as $report) {
if ($report['id'] == $report_id) {
return [
'group_id' => $group_id,
'report_data' => $report,
];
}
}
}
return [];
}
/**
* Gets an embed token for a report.
*
* @param $group_id
* @param $report_id
*
* @return bool|string
*/
function power_bi_reports_generate_report_embed_token($group_id, $report_id) {
// Get a token.
$token = power_bi_reports_get_auth_token();
$endpoint = POWER_BI_REPORTS__REPORT_EMBED_TOKEN;
$endpoint = str_replace('{groupId}', $group_id, $endpoint);
$endpoint = str_replace('{reportId}', $report_id, $endpoint);
$body = ['accessLevel' => 'View'];
$headers = ['Authorization' => sprintf("Bearer %s", $token)];
$guzzle = new GuzzleHttp\Client();
$res = $guzzle->post(
$endpoint,
[
'headers' => $headers,
'form_params' => $body,
]
);
if ($res->getStatusCode() == 200) {
$response_contents = $res->getBody()->getContents();
$response_contents_decoded = json_decode($response_contents, TRUE);
$token = $response_contents_decoded['token'];
$expiration = $response_contents_decoded['expiration'];
// Cache it locally
power_bi_reports_set_local_report_embed_token($report_id, $token, $expiration);
return $token;
}
else {
// @TODO handle errors
return FALSE;
}
}
/**
* Helper function: Validates if the provided string is a valid guid.
*
* @see https://en.wikipedia.org/wiki/Universally_unique_identifier
* @see https://stackoverflow.com/a/1253417
*
* @param $guid
*
* @return false|int
*/
function power_bi_reports_validate_guid($guid) {
return preg_match('/^\{?[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}\}?$/', $guid);
}
function power_bi_reports_get_groups() {
// Get a token.
$token = power_bi_reports_get_auth_token();
$endpoint = POWER_BI_REPORTS__GROUPS;
$headers = [
"Authorization" => sprintf("Bearer %s", $token),
];
$guzzle = new GuzzleHttp\Client();
$res = $guzzle->get(
$endpoint,
[
'headers' => $headers,
]
);
if ($res->getStatusCode() == 200) {
$response_contents = $res->getBody()->getContents();
$response_contents_decoded = json_decode($response_contents, TRUE);
// @TODO handle good response!
return $response_contents_decoded['value'];
}
else {
// @TODO handle errors
return FALSE;
}
}
/**
* Implements hook_cron().
*/
function power_bi_reports_cron() {
$cron_enabled = variable_get('power_bi_reports_cron_sync', FALSE);
if ($cron_enabled) {
// Renew the embed tokens.
$power_bi_reports_data = variable_get('power_bi_reports_data', []);
if (!empty($power_bi_reports_data)) {
$queue_embed_tokens = DrupalQueue::get("power_bi_reports_renew_embed_token");
foreach ($power_bi_reports_data as $group_id => $group) {
foreach ($group['reports'] as $report) {
$report_id = $report['id'];
$queue_embed_tokens->createItem(
[
'group_id' => $group_id,
'report_id' => $report_id,
]
);
}
}
}
}
// Sync the reports.
$queue_sync_reports = DrupalQueue::get("power_bi_reports_batch_sync_reports");
$queue_sync_reports->createItem([]);
}
/**
* Implements hook_cron_queue_info().
*/
function power_bi_reports_cron_queue_info() {
$queues['power_bi_reports_renew_embed_token'] = [
'worker callback' => 'power_bi_reports_renew_embed_token_worker',
'time' => 2 * 60, // 2 minutes
];
$queues['power_bi_reports_batch_sync_reports'] = [
'worker callback' => 'power_bi_reports_batch_sync_reports_worker',
'time' => 10 * 60, // 10 minutes
];
return $queues;
}
function power_bi_reports_batch_sync_reports_worker($item) {
$last_sync = variable_get('power_bi_reports_last_sync_reports', REQUEST_TIME);
if ($last_sync < (REQUEST_TIME + 3000)) { // Every 50 minutes
power_bi_reports_batch_sync_reports_init();
}
}
function power_bi_reports_renew_embed_token_worker($data) {
$report_id = $data['report_id'];
$group_id = $data['group_id'];
try {
$token = power_bi_reports_generate_report_embed_token($group_id, $report_id);
// Store some result for post-processing in the finished callback.
$context['results']['tokens'][$report_id] = $token;
} catch (Exception $e) {
$msg = $e->getMessage();
watchdog('power_bi_reports', $msg, [], WATCHDOG_ERROR);
}
}
function power_bi_reports_batch_sync_reports_init() {
$batch = [
'operations' => [
['power_bi_reports_batch_sync_reports', []],
],
'finished' => 'power_bi_reports_batch_sync_reports_finished',
'title' => t('Reports synchronisation'),
'init_message' => t('Reports synchronisation is starting.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Reports synchronisation has encountered an error.'),
];
batch_set($batch);
// batch_process('node/1');
}
function power_bi_reports_renew_embed_tokens_init() {
$batch = [
'operations' => [
['power_bi_reports_renew_embed_tokens', []],
],
'finished' => 'power_bi_reports_renew_embed_tokens_finished',
'title' => t('Renew embed tokens'),
'init_message' => t('Renewing of embed tokens is starting.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Renewing of embed tokens has encountered an error.'),
];
batch_set($batch);
// batch_process('node/1');
}
function power_bi_reports_batch_sync_reports(&$context) {
if (!isset($context['sandbox']['progress'])) {
$groups_raw = power_bi_reports_get_groups();
$context['sandbox']['groups_raw'] = $groups_raw;
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = count($groups_raw);
}
// Get the first group
$group_delta = key($context['sandbox']['groups_raw']);
$group_raw = $context['sandbox']['groups_raw'][$group_delta];
$group_id = $group_raw['id'];
$reports_in_group = power_bi_reports_get_reports_in_group($group_id);
// Remove the group from the array, so that it does not get processed again.
unset($context['sandbox']['groups_raw'][$group_delta]);
// Store some result for post-processing in the finished callback.
$context['results']['groups'][$group_id] = $group_raw;
$context['results']['groups'][$group_id]['reports'] = $reports_in_group;
// Update our progress information.
$context['sandbox']['progress']++;
$context['message'] = t('Syncing group %name (%guid)', [
'%name' => $group_raw['name'],
'%guid' => $group_id,
]);
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
function power_bi_reports_renew_embed_tokens(&$context) {
if (!isset($context['sandbox']['progress'])) {
$reports_data = variable_get('power_bi_reports_data', []);
if (empty($reports_data)) {
$context['results']['error'] = t('You have to sync the reports before you renew the embed tokens.');
$context['finished'] = 1;
return;
}
// Flatten out the list.
$to_sync = [];
foreach ($reports_data as $group_id => $group) {
foreach ($group['reports'] as $report) {
$report_id = $report['id'];
$to_sync[] = [
'group_id' => $group_id,
'report_id' => $report_id,
'report_name' => $report['name'],
];
}
}
$context['sandbox']['to_sync'] = $to_sync;
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = count($to_sync);
}
// Get the first report
$report_delta = key($context['sandbox']['to_sync']);
$report_raw = $context['sandbox']['to_sync'][$report_delta];
$group_id = $report_raw['group_id'];
$report_id = $report_raw['report_id'];
try {
$token = power_bi_reports_generate_report_embed_token($group_id, $report_id);
// Store some result for post-processing in the finished callback.
$context['results']['tokens'][$report_id] = $token;
} catch (Exception $e) {
$context['results']['fails'][] = [
'report' => $report_raw,
'error' => $e->getMessage(),
];
}
// Remove the report from the array, so that it does not get processed again.
unset($context['sandbox']['to_sync'][$report_delta]);
// Update our progress information.
$context['sandbox']['progress']++;
$context['message'] = t('Renewing embed token for report %name (%guid)', [
'%name' => $report_raw['report_name'],
'%guid' => $report_id,
]);
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
function power_bi_reports_batch_sync_reports_finished($success, $results, $operations) {
if ($success) {
variable_set('power_bi_reports_data', $results['groups']);
variable_set('power_bi_reports_last_sync_reports', REQUEST_TIME);
$message = t('Reports successfully synced.');
drupal_set_message($message);
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$message = t('An error occurred while processing %error_operation with arguments: @arguments', [
'%error_operation' => $error_operation[0],
'@arguments' => print_r($error_operation[1], TRUE),
]);
drupal_set_message($message, 'error');
watchdog('power_bi_reports', $message, [], WATCHDOG_ERROR);
}
}
function power_bi_reports_renew_embed_tokens_finished($success, $results, $operations) {
if ($success) {
if (isset($results['error'])) {
drupal_set_message($results['error'], 'error');
watchdog('power_bi_reports', $results['error'], [], WATCHDOG_ERROR);
}
else {
$message = t('Embed tokens successfully renewed.');
drupal_set_message($message);
}
if (isset($results["fails"])) {
$message = t('<p>Warning, the embed tokens for the following reports could not be renewed:</p>');
$message .= '<ul>';
foreach ($results["fails"] as $fail) {
$message .= "<li>{$fail['report']['report_name']} (report ID: {$fail['report']['report_id']} - group ID: {$fail['report']['group_id']})</li>";
}
$message .= '<ul>';
drupal_set_message($message, 'warning');
watchdog('power_bi_reports', $message, [], WATCHDOG_WARNING);
}
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$message = t('An error occurred while processing %error_operation with arguments: @arguments', [
'%error_operation' => $error_operation[0],
'@arguments' => print_r($error_operation[1], TRUE),
]);
drupal_set_message($message, 'error');
watchdog('power_bi_reports', $message, [], WATCHDOG_ERROR);
}
}
function power_bi_reports_get_reports_in_group($group_id) {
// Get a token.
$token = power_bi_reports_get_auth_token();
$endpoint = POWER_BI_REPORTS__REPORTS_IN_GROUP;
$endpoint = str_replace('{groupId}', $group_id, $endpoint);
$headers = [
"Authorization" => sprintf("Bearer %s", $token),
];
$guzzle = new GuzzleHttp\Client();
$res = $guzzle->get(
$endpoint,
[
'headers' => $headers,
]
);
if ($res->getStatusCode() == 200) {
$response_contents = $res->getBody()->getContents();
// @TODO handle good response!
$response_contents_decoded = json_decode($response_contents, TRUE);
// @TODO handle good response!
return $response_contents_decoded['value'];
}
else {
// @TODO handle errors
return FALSE;
}
}