はじめに
REST API を介して、Yellowfin からコンテンツをエクスポートおよびインポートする方法を 3 回に分けて紹介したいと思います。
第 1 回 『Yellowfin REST API エクスポートできるコンテンツの確認』
第 2 回 『Yellowfin REST API コンテンツのエクスポート』
第 3 回 『Yellowfin REST API コンテンツのインポート』
今回は第 1 回 『Yellowfin REST API エクスポートできるコンテンツの確認』です。
アクセストークンの取得
REST API を介してリソースにアクセスする場合、必ずアクセストークンが必要となります。
トークンの扱いに関しては、『Yellowfin 情報参照プログラム PHP + REST API でロール / ユーザー / グループなどの情報を取得』の「アクセストークンの取得」の個所をご確認ください。
エクスポート可能なコンテンツ一覧の取得
アクセストークンを取得したことを前提に、コンテンツ一覧取得に関する処理内容は以下の通りです。
ちなみに、トークン取得も含めた処理全容は後述します。ご安心ください。
ヘッダー情報を変数に取り込むまでは、他の REST API を使った POST 処理と同様です。HEADER や BODY に必要な情報に関しては、開発者用ドキュメントの Get the exportable content of the passed content type(s) で詳細をご確認ください。
以下のサンプルコードでは、cURL を使って実際の処理を実行し、結果はプログラムを実行するコンソールに表示されます。
//エクスポート可能なコンテンツの参照
//API
$url3 = 'http://localhost:8080/api/rpc/import-export/get-export-content';
//ヘッダー
$header3 = array(
'Authorization:YELLOWFIN ts='.$time.', nonce=123, token='.$accessToken,
'Accept:application/vnd.yellowfin.api-v2+json',
'Content-Type:application/json;charset=UTF-8'
);
//ボディー
$refertData = '[]';
//cURLで処理実行
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_HTTPHEADER, $header3);
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch3, CURLOPT_POSTFIELDS, $refertData);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch3, CURLOPT_URL, $url3);
$result3 = curl_exec($ch3);
$result3 = json_decode($result3);
$json_result3 = json_encode($result3, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
echo($json_result3);
curl_close($ch3);
出力結果
コンソールからプログラムを実行します。
PS> php exportable-contents.php
プログラムを実行すると、エクスポート可能なコンテンツ一覧情報が、JSON 配列で返ってきます。
その中から 1 つコンテンツを抽出してみます。
{
"resourceName": "export",
"resourceDescription": "エクスポートするコンテンツ",
"resourceId": 108024,
"resourceUUID": "1e22bd4b-bc0a-433a-ba8c-cac239ff5297",
"resourceType": "REPORT",
"resourceOrgId": 1
},
JSON ファイルの各項目の説明です。
| 項目 | 説明 |
|---|---|
| resourceName | リソースの名前 |
| resourceDescription | 中身を説明する任意の文面 |
| resourceId | Yellowfin 内部でリソースを一意で認識するための ID |
| resourceUUID | Yellowfin 外部に対して公開するリソースの ID |
| resourceSubType | REPORT, DASHBOARD などリソース種類 |
| resourceOrgId | リソースが属するクライアント組織の ID |
コンテンツをエクスポートする際のボディー情報に、上記 JSON 情報が必要となってきます。
エクスポート処理は、次回の記事で紹介します。
プログラム全容:exportable-contents.php
エクスポート可能なコンテンツ一覧を取得するためのプログラム全容は以下の通りです。
<?php
//変数の宣言
$adminId = 'admin@yellowfin.com.au';
$adminPassword = 'test';
$time = ceil(microtime(true)*1000);
$url = 'http://localhost:8080/api/refresh-tokens';
$url2 = 'http://localhost:8080/api/access-tokens';
$refreshToken = "";
$accessToken = "";
//リフレッシュトークン
$header = array(
'Authorization:YELLOWFIN ts='.$time.', nonce=123',
'Accept:application/vnd.yellowfin.api-v2+json',
'Content-Type:application/json;charset=UTF-8'
);
$body = array(
'userName'=>$adminId,
'password'=>$adminPassword
);
$body_json = json_encode($body, JSON_PRETTY_PRINT);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$json_result = json_decode($result);
$refreshToken = $json_result->securityToken;
curl_close($ch);
//アクセストークン
$header2 = array(
'Authorization:YELLOWFIN ts='.$time.', nonce=123, token='.$refreshToken,
'Accept:application/vnd.yellowfin.api-v2+json',
'Content-Type:application/json'
);
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_HTTPHEADER, $header2);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch2, CURLOPT_POSTFIELDS, '');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_URL, $url2);
curl_close($ch);
$result2 = curl_exec($ch2);
$json_result2 = json_decode($result2);
$accessToken=$json_result2->securityToken;
//エクスポート可能なコンテンツの参照
//API
$url3='http://localhost:8080/api/rpc/import-export/get-export-content';
//ヘッダー
$header3 = array(
'Authorization:YELLOWFIN ts='.$time.', nonce=123, token='.$accessToken,
'Accept:application/vnd.yellowfin.api-v2+json',
'Content-Type:application/json;charset=UTF-8'
);
//ボディー
$refertData = '[]';
//cURLで処理実行
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_HTTPHEADER, $header3);
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch3, CURLOPT_POSTFIELDS, $refertData);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch3, CURLOPT_URL, $url3);
$result3 = curl_exec($ch3);
$result3 = json_decode($result3);
$json_result3 = json_encode($result3, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
echo($json_result3);
curl_close($ch3);
?>
最後に
REST API で必要な情報は、REST API 経由で取得します。例えば、Yellowfin が内部でコンテンツを一意に認識するための ID 情報などは、通常 GUI からは確認できません。
ということで、インポートエクスポート処理に関する記事は長くなりそうなので、3 回に分けてシリーズ化してみました。
皆様、良いデータ分析を! See you then! Cheers!!