Source: AnalyticsClient.js

  1. /*$Id$*/
  2. const https = require('https');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const querystring = require('querystring');
  6. const request = require('request');
  7. const analyticsURI = "@@ANALYTICS_URL";
  8. const accountsURI = "@@ACCOUNTS_URL";
  9. const clientVersion = "2.5.0";
  10. class AnalyticsClient
  11. {
  12. constructor(clientId, clientSecret, refreshToken)
  13. {
  14. this.clientId = clientId;
  15. this.clientSecret = clientSecret;
  16. this.refreshToken = refreshToken;
  17. this.accessToken = null;
  18. }
  19. /**
  20. * Returns list of all accessible organizations.
  21. * @method getOrgs
  22. * @returns {JSONArray} Organization list.
  23. * @throws {Error} If the request failed due to some error.
  24. */
  25. async getOrgs()
  26. {
  27. var uriPath = "/restapi/v2/orgs";
  28. var header = {};
  29. var result = await this.handleV2Request(uriPath, "GET", null, header);
  30. return result.orgs;
  31. }
  32. /**
  33. * Returns list of all accessible workspaces.
  34. * @method getWorkspaces
  35. * @returns {JSONArray} Workspace list.
  36. * @throws {Error} If the request failed due to some error.
  37. */
  38. async getWorkspaces()
  39. {
  40. var uriPath = "/restapi/v2/workspaces";
  41. var header = {};
  42. var result = await this.handleV2Request(uriPath, "GET", null, header);
  43. return result;
  44. }
  45. /**
  46. * Returns list of owned workspaces.
  47. * @method getOwnedWorkspaces
  48. * @returns {JSONArray} Workspace list.
  49. * @throws {Error} If the request failed due to some error.
  50. */
  51. async getOwnedWorkspaces()
  52. {
  53. var uriPath = "/restapi/v2/workspaces/owned";
  54. var header = {};
  55. var result = await this.handleV2Request(uriPath, "GET", null, header);
  56. return result.workspaces;
  57. }
  58. /**
  59. * Returns list of shared workspaces.
  60. * @method getSharedWorkspaces
  61. * @returns {JSONArray} Workspace list.
  62. * @throws {Error} If the request failed due to some error.
  63. */
  64. async getSharedWorkspaces(config={})
  65. {
  66. var uriPath = "/restapi/v2/workspaces/shared";
  67. var header = {};
  68. var result = await this.handleV2Request(uriPath, "GET", null, header);
  69. return result.workspaces;
  70. }
  71. /**
  72. * Returns list of recently accessed views.
  73. * @method getRecentViews
  74. * @returns {JSONArray} View list.
  75. * @throws {Error} If the request failed due to some error.
  76. */
  77. async getRecentViews()
  78. {
  79. var uriPath = "/restapi/v2/recentviews";
  80. var header = {};
  81. var result = await this.handleV2Request(uriPath, "GET", null, header);
  82. return result.views;
  83. }
  84. /**
  85. * Returns list of all accessible dashboards.
  86. * @method getDashboards
  87. * @returns {JSONArray} Dashboard list.
  88. * @throws {Error} If the request failed due to some error.
  89. */
  90. async getDashboards()
  91. {
  92. var uriPath = "/restapi/v2/dashboards";
  93. var header = {};
  94. var result = await this.handleV2Request(uriPath, "GET", null, header);
  95. return result;
  96. }
  97. /**
  98. * Returns list of owned dashboards.
  99. * @method getOwnedDashboards
  100. * @returns {JSONArray} Dashboard list.
  101. * @throws {Error} If the request failed due to some error.
  102. */
  103. async getOwnedDashboards()
  104. {
  105. var uriPath = "/restapi/v2/dashboards/owned";
  106. var header = {};
  107. var result = await this.handleV2Request(uriPath, "GET", null, header);
  108. return result.views;
  109. }
  110. /**
  111. * Returns list of shared dashboards.
  112. * @method getSharedDashboards
  113. * @returns {JSONArray} Dashboard list.
  114. * @throws {Error} If the request failed due to some error.
  115. */
  116. async getSharedDashboards()
  117. {
  118. var uriPath = "/restapi/v2/dashboards/shared";
  119. var header = {};
  120. var result = await this.handleV2Request(uriPath, "GET", null, header);
  121. return result.views;
  122. }
  123. /**
  124. * Returns details of the specified workspace.
  125. * @method getWorkspaceDetails
  126. * @param {String} workspaceId - The ID of the workspace.
  127. * @returns {Object} Workspace details.
  128. * @throws {Error} If the request failed due to some error.
  129. */
  130. async getWorkspaceDetails(workspaceId)
  131. {
  132. var uriPath = "/restapi/v2/workspaces/" + workspaceId
  133. var header = {};
  134. var result = await this.handleV2Request(uriPath, "GET", null, header);
  135. return result.workspaces;
  136. }
  137. /**
  138. * Returns details of the specified view.
  139. * @method getViewDetails
  140. * @param {String} viewId - The ID of the view.
  141. * @param {Object} config={} - Contains any additional control attributes.
  142. * @returns {Object} View details.
  143. * @throws {Error} If the request failed due to some error.
  144. */
  145. async getViewDetails(viewId, config={})
  146. {
  147. var uriPath = "/restapi/v2/views/" + viewId
  148. var header = {};
  149. var result = await this.handleV2Request(uriPath, "GET", config, header);
  150. return result.views;
  151. }
  152. /**
  153. * Returns a new OrgAPI instance.
  154. * @method getOrgInstance
  155. * @param {String} orgId - The ID of the organization.
  156. */
  157. getOrgInstance(orgId)
  158. {
  159. var orgInstance = new OrgAPI(this, orgId);
  160. return orgInstance;
  161. }
  162. /**
  163. * Returns a new WorkspaceAPI instance.
  164. * @method getWorkspaceInstance
  165. * @param {String} orgId - The ID of the organization.
  166. * @param {String} workspaceId - The ID of the workspace.
  167. */
  168. getWorkspaceInstance(orgId, workspaceId)
  169. {
  170. var workspaceInstance = new WorkspaceAPI(this, orgId, workspaceId);
  171. return workspaceInstance;
  172. }
  173. /**
  174. * Returns a new ViewAPI instance.
  175. * @method getViewInstance
  176. * @param {String} orgId - The ID of the organization.
  177. * @param {String} workspaceId - The ID of the workspace.
  178. * @param {String} viewId - The ID of the view.
  179. */
  180. getViewInstance(orgId, workspaceId, viewId)
  181. {
  182. var viewInstance = new ViewAPI(this, orgId, workspaceId, viewId);
  183. return viewInstance;
  184. }
  185. /**
  186. * Returns a new BulkAPI instance.
  187. * @method getBulkInstance
  188. * @param {String} orgId - The ID of the organization.
  189. * @param {String} workspaceId - The ID of the workspace.
  190. */
  191. getBulkInstance(orgId, workspaceId)
  192. {
  193. var bulkInstance = new BulkAPI(this, orgId, workspaceId);
  194. return bulkInstance;
  195. }
  196. sleep(ms)
  197. {
  198. return new Promise((resolve) => {
  199. setTimeout(resolve, ms);
  200. });
  201. }
  202. async handleBatchImportRequest(uriPath, config, header, filePath, batchSize)
  203. {
  204. const {EOL} = require('os');
  205. var jobId;
  206. var data = fs.readFileSync(filePath, 'utf8');
  207. var fileContent = data.split(EOL);
  208. const fileHeader = fileContent.shift() + EOL;
  209. var totalLines = fileContent.length;
  210. var totalBatchCount = Math.ceil(totalLines / batchSize);
  211. config.batchKey = "start";
  212. header['User-Agent'] = 'Analytics NodeJS Client v'+clientVersion;
  213. for (let i = 0; i < totalBatchCount; i++)
  214. {
  215. let batch = fileContent.slice(batchSize*i, batchSize + (i * batchSize))
  216. batch = fileHeader + batch.join(EOL)
  217. config.isLastBatch = (i == (totalBatchCount - 1))? "true" : "false"
  218. var encodedConfig = encodeURIComponent(JSON.stringify(config));
  219. var url = 'https://'+analyticsURI + uriPath + "?" + "CONFIG" + "=" + encodedConfig;
  220. if(this.accessToken == null)
  221. {
  222. this.accessToken = await this.getOauth();
  223. }
  224. var response = await this.sendBatchImportRequest(url, header, batch).catch(async error=>
  225. {
  226. if(error.errorCode == "8535")
  227. {
  228. this.accessToken = await this.getOauth();
  229. response = await this.sendBatchImportRequest(url, header, batch);
  230. }
  231. else
  232. {
  233. throw error;
  234. }
  235. });
  236. config.batchKey = response.batchKey;
  237. jobId = response.jobId;
  238. await this.sleep(2000);
  239. }
  240. return jobId;
  241. }
  242. sendBatchImportRequest(url, header, batch)
  243. {
  244. header.Authorization = 'Zoho-oauthtoken ' + this.accessToken;
  245. return new Promise(function(resolve, reject) {
  246. const formData = {
  247. FILE: {
  248. value: Buffer.from(batch),
  249. options: {
  250. filename: 'batch.csv'
  251. }
  252. }
  253. };
  254. request.post({url:url, headers:header, formData:formData, secureProtocol:'TLSv1_2_method'}, (err, resp, body)=> {
  255. if (err)
  256. {
  257. reject(JSON.parse(err));
  258. }
  259. else
  260. {
  261. let respJSON = (JSON.parse(body));
  262. if(resp.statusCode!==200)
  263. {
  264. reject(respJSON.data)
  265. }
  266. else
  267. {
  268. resolve(respJSON.data)
  269. }
  270. }
  271. });
  272. });
  273. }
  274. async handleImportRequest(uriPath, config, header, filePath, data=null)
  275. {
  276. if(this.accessToken == null)
  277. {
  278. this.accessToken = await this.getOauth();
  279. }
  280. return await this.sendImportRequest(uriPath, config, header, filePath, data).catch(async error=>
  281. {
  282. if(error.errorCode == "8535")
  283. {
  284. this.accessToken = await this.getOauth();
  285. return await this.sendImportRequest(uriPath, config, header, filePath, data);
  286. }
  287. else
  288. {
  289. throw error;
  290. }
  291. });
  292. }
  293. sendImportRequest(uriPath, config, header={}, filePath, data)
  294. {
  295. header['User-Agent'] = 'Analytics NodeJS Client v'+clientVersion;
  296. header.Authorization = 'Zoho-oauthtoken ' + this.accessToken;
  297. if(config !== null)
  298. {
  299. var encodedConfig = encodeURIComponent(JSON.stringify(config));
  300. var configParam = "CONFIG" + "=" + encodedConfig;
  301. uriPath = uriPath + "?" + configParam;
  302. }
  303. var url = 'https://'+analyticsURI+uriPath;
  304. if(data !== null)
  305. {
  306. return new Promise(function(resolve, reject) {
  307. // const { Readable } = require("stream")
  308. // const readable = Readable.from(data).then()
  309. let formData = {
  310. 'DATA': data
  311. };
  312. header['Content-Type'] = 'application/x-www-form-urlencoded';
  313. var req = request.post({url:url, headers:header, formData:formData, secureProtocol:'TLSv1_2_method'}, (err, resp, body)=> {
  314. if (err)
  315. {
  316. reject(JSON.parse(err));
  317. }
  318. else
  319. {
  320. let respJSON = (JSON.parse(body));
  321. if(resp.statusCode!==200)
  322. {
  323. reject(respJSON.data)
  324. }
  325. else
  326. {
  327. resolve(respJSON.data)
  328. }
  329. }
  330. });
  331. });
  332. }
  333. else
  334. {
  335. return new Promise(function(resolve, reject) {
  336. var zohofile = fs.createReadStream(filePath);
  337. zohofile.on('error', err =>{reject(err);});
  338. zohofile.once('readable', function() {
  339. let formData = {
  340. 'FILE': zohofile
  341. };
  342. var req = request.post({url:url, headers:header, formData:formData, secureProtocol:'TLSv1_2_method'}, (err, resp, body)=> {
  343. if (err)
  344. {
  345. reject(JSON.parse(err));
  346. }
  347. else
  348. {
  349. let respJSON = (JSON.parse(body));
  350. if(resp.statusCode!==200)
  351. {
  352. reject(respJSON.data)
  353. }
  354. else
  355. {
  356. resolve(respJSON.data)
  357. }
  358. }
  359. });
  360. });
  361. });
  362. }
  363. }
  364. async handleExportRequest(uriPath, filePath, config, header)
  365. {
  366. if(this.accessToken == null)
  367. {
  368. this.accessToken = await this.getOauth();
  369. }
  370. return await this.sendExportRequest(uriPath, filePath, config, header).catch(async error=>
  371. {
  372. if(error.errorCode == "8535")
  373. {
  374. this.accessToken = await this.getOauth();
  375. return await this.sendExportRequest(uriPath, filePath, config, header);
  376. }
  377. else
  378. {
  379. throw error;
  380. }
  381. });
  382. }
  383. sendExportRequest(uriPath, filePath, config, header={})
  384. {
  385. header['User-Agent'] = 'Analytics NodeJS Client v'+clientVersion;
  386. header.Authorization = 'Zoho-oauthtoken ' + this.accessToken;
  387. if(config !== null)
  388. {
  389. var encodedConfig = encodeURIComponent(JSON.stringify(config));
  390. var configParam = "CONFIG" + "=" + encodedConfig;
  391. uriPath = uriPath + "?" + configParam;
  392. }
  393. return new Promise(function(resolve, reject) {
  394. var url = 'https://'+analyticsURI+uriPath;
  395. var req = request.get({url:url,encoding: null,headers:header,secureProtocol: 'TLSv1_2_method'}, (err, resp, body)=> {
  396. if (err)
  397. {
  398. reject(JSON.parse(err));
  399. }
  400. else
  401. {
  402. if(resp.statusCode!==200)
  403. {
  404. let respJSON = (JSON.parse(body));
  405. reject(respJSON.data)
  406. }
  407. else
  408. {
  409. fs.writeFileSync(filePath, body);
  410. resolve();
  411. }
  412. }
  413. });
  414. });
  415. }
  416. async handleV2Request(uriPath, method, config, header, isExportReq = false)
  417. {
  418. if(this.accessToken == null)
  419. {
  420. this.accessToken = await this.getOauth();
  421. }
  422. return await this.sendV2Request(uriPath, method, config, header, isExportReq).catch(async error=>
  423. {
  424. if(error.errorCode == "8535")
  425. {
  426. this.accessToken = await this.getOauth();
  427. return await this.sendV2Request(uriPath, method, config, header, isExportReq);
  428. }
  429. else
  430. {
  431. throw error;
  432. }
  433. });
  434. }
  435. sendV2Request(uriPath, reqMethod, config, header={}, isExportReq = false)
  436. {
  437. header['User-Agent'] = 'Analytics NodeJS Client v'+clientVersion;
  438. //header.Content-Type = 'application/x-www-form-urlencoded';
  439. header.Authorization = 'Zoho-oauthtoken ' + this.accessToken;
  440. if(config !== null && Object.keys(config).length !== 0)
  441. {
  442. var encodedConfig = encodeURIComponent(JSON.stringify(config));
  443. var configParam = "CONFIG" + "=" + encodedConfig;
  444. uriPath = uriPath + "?" + configParam;
  445. //var contentLength = 'Content-Length';
  446. //header[contentLength] = configParam.length;
  447. }
  448. var options = {
  449. host: analyticsURI,
  450. path: uriPath,
  451. headers: header,
  452. method: reqMethod,
  453. secureProtocol: 'TLSv1_2_method'
  454. };
  455. return new Promise(function(resolve, reject) {
  456. var req = https.request(options, (resp) =>
  457. {
  458. let data = '';
  459. resp.setEncoding(null);
  460. resp.on('data', (chunk) => {
  461. data += chunk;
  462. });
  463. resp.on('end', () => {
  464. var respCode = (resp.statusCode).toString();
  465. var isRequestFailed = Boolean(!respCode.startsWith('2'));
  466. var hasResponse = Boolean(respCode === '200');
  467. if(isRequestFailed)
  468. {
  469. var respJSON = (JSON.parse(data));
  470. reject(respJSON.data)
  471. }
  472. else if(isExportReq)
  473. {
  474. resolve(data);
  475. }
  476. else if(hasResponse)
  477. {
  478. var respJSON = (JSON.parse(data));
  479. resolve(respJSON.data);
  480. }
  481. resolve();
  482. });
  483. }).on("error", (err) =>
  484. {
  485. reject(JSON.parse(err))
  486. });
  487. req.end();
  488. });
  489. }
  490. getOauth()
  491. {
  492. var oauthinfo = {};
  493. oauthinfo.client_id= this.clientId;
  494. oauthinfo.client_secret= this.clientSecret;
  495. oauthinfo.refresh_token= this.refreshToken;
  496. oauthinfo.grant_type= 'refresh_token';
  497. var encodedParams = querystring.stringify(oauthinfo);
  498. var options = {
  499. host: accountsURI,
  500. path: '/oauth/v2/token',
  501. headers: {
  502. 'Content-Type': 'application/x-www-form-urlencoded',
  503. 'User-Agent': 'Analytics NodeJS Client v'+clientVersion,
  504. 'Content-Length': encodedParams.length
  505. },
  506. method: "POST",
  507. secureProtocol: 'TLSv1_2_method'
  508. };
  509. return new Promise(function(resolve, reject) {
  510. var req = https.request(options, (resp) => {
  511. let data = '';
  512. // A chunk of data has been recieved.
  513. resp.on('data', (chunk) => {
  514. data += chunk;
  515. });
  516. // The whole response has been received. Print out the result.
  517. resp.on('end', () => {
  518. var respJSON = (JSON.parse(data));
  519. if(!respJSON.error)
  520. {
  521. resolve(respJSON.access_token);
  522. }
  523. else
  524. {
  525. var err = {};
  526. err.errorCode = '0';
  527. err.errorMessage = respJSON.error;
  528. reject(err);
  529. }
  530. });
  531. }).on("error", (err) => {
  532. reject(err.message);
  533. });
  534. req.write(encodedParams);
  535. req.end();
  536. });
  537. }
  538. };
  539. class OrgAPI
  540. {
  541. constructor(ac, orgId)
  542. {
  543. this.ac = ac;
  544. this.header = {};
  545. this.header['ZANALYTICS-ORGID'] = orgId;
  546. }
  547. /**
  548. * Create a blank workspace in the specified organization.
  549. * @method createWorkspace
  550. * @param {String} workspaceName - Name of the workspace.
  551. * @param {Object} config={} - Contains any additional control attributes.
  552. * @returns {String} Created workspace id.
  553. * @throws {Error} If the request failed due to some error.
  554. */
  555. async createWorkspace(workspaceName, config={})
  556. {
  557. var uriPath = "/restapi/v2/workspaces";
  558. config.workspaceName = workspaceName;
  559. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  560. return result.workspaceId;
  561. }
  562. /**
  563. * Returns list of admins for a specified organization.
  564. * @method getAdmins
  565. * @param {Object} config={} - Contains any additional control attributes.
  566. * @returns {JSONArray} Organization admin list.
  567. * @throws {Error} If the request failed due to some error.
  568. */
  569. async getAdmins(config={})
  570. {
  571. var uriPath = "/restapi/v2/orgadmins";
  572. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  573. return result.orgAdmins;
  574. }
  575. /**
  576. * Returns subscription details of the specified organization.
  577. * @method getSubscriptionDetails
  578. * @returns {Object} Subscription details.
  579. * @throws {Error} If the request failed due to some error.
  580. */
  581. async getSubscriptionDetails() {
  582. var uriPath = "/restapi/v2/subscription";
  583. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  584. return result.subscription;
  585. }
  586. /**
  587. * Returns resource usage details of the specified organization.
  588. * @method getResourceDetails
  589. * @returns {Object} Resource details.
  590. * @throws {Error} If the request failed due to some error.
  591. */
  592. async getResourceDetails() {
  593. var uriPath = "/restapi/v2/resources";
  594. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  595. return result.resourceDetails;
  596. }
  597. /**
  598. * Returns list of users for the specified organization.
  599. * @method getUsers
  600. * @returns {JSONArray} User list.
  601. * @throws {Error} If the request failed due to some error.
  602. */
  603. async getUsers() {
  604. var uriPath = "/restapi/v2/users";
  605. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  606. return result.users;
  607. }
  608. /**
  609. * Add users to the specified organization.
  610. * @method addUsers
  611. * @param {JSONArray} emailIds - The email address of the users to be added.
  612. * @param {Object} config={} - Contains any additional control attributes.
  613. * @throws {Error} If the request failed due to some error.
  614. */
  615. async addUsers(emailIds, config={}) {
  616. var uriPath = "/restapi/v2/users";
  617. config.emailIds = emailIds;
  618. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  619. }
  620. /**
  621. * Remove users from the specified organization.
  622. * @method removeUsers
  623. * @param {JSONArray} emailIds - The email address of the users to be removed.
  624. * @param {Object} config={} - Contains any additional control attributes.
  625. * @throws {Error} If the request failed due to some error.
  626. */
  627. async removeUsers(emailIds, config={}) {
  628. var uriPath = "/restapi/v2/users";
  629. config.emailIds = emailIds;
  630. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  631. }
  632. /**
  633. * Activate users in the specified organization.
  634. * @method activateUsers
  635. * @param {JSONArray} emailIds - The email address of the users to be activated.
  636. * @param {Object} config={} - Contains any additional control attributes.
  637. * @throws {Error} If the request failed due to some error.
  638. */
  639. async activateUsers(emailIds, config={}) {
  640. var uriPath = "/restapi/v2/users/active";
  641. config.emailIds = emailIds;
  642. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  643. }
  644. /**
  645. * Deactivate users in the specified organization.
  646. * @method deActivateUsers
  647. * @param {JSONArray} emailIds - The email address of the users to be deactivated.
  648. * @param {Object} config={} - Contains any additional control attributes.
  649. * @throws {Error} If the request failed due to some error.
  650. */
  651. async deActivateUsers(emailIds, config={}) {
  652. var uriPath = "/restapi/v2/users/inactive";
  653. config.emailIds = emailIds;
  654. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  655. }
  656. /**
  657. * Change role for the specified users.
  658. * @method changeUserRole
  659. * @param {JSONArray} emailIds - The email address of the users.
  660. * @param {String} role - New role for the users.
  661. * @param {Object} config={} - Contains any additional control attributes.
  662. * @throws {Error} If the request failed due to some error.
  663. */
  664. async changeUserRole(emailIds, role, config={}) {
  665. var uriPath = "/restapi/v2/users/role";
  666. config.emailIds = emailIds;
  667. config.role = role;
  668. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  669. }
  670. /**
  671. * Returns details of the specified workspace/view.
  672. * @method getMetaDetails
  673. * @param {String} workspaceName - Name of the workspace.
  674. * @param {String} viewName - Name of the view. Can be null.
  675. * @param {Object} config={} - Contains any additional control attributes.
  676. * @returns {Object} Workspace (or) View meta details.
  677. * @throws {Error} If the request failed due to some error.
  678. */
  679. async getMetaDetails(workspaceName, viewName=null, config={})
  680. {
  681. var uriPath = "/restapi/v2/metadetails";
  682. config.workspaceName = workspaceName;
  683. if(viewName !== null)
  684. {
  685. config.viewName = viewName;
  686. }
  687. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  688. return result;
  689. }
  690. }
  691. class WorkspaceAPI
  692. {
  693. constructor(ac, orgId, workspaceId)
  694. {
  695. this.ac = ac;
  696. this.uriPath = "/restapi/v2/workspaces/" + workspaceId
  697. this.header = {};
  698. this.header['ZANALYTICS-ORGID'] = orgId;
  699. this.orgId = orgId;
  700. }
  701. /**
  702. * Copy the specified workspace from one organization to another or within the organization.
  703. * @method copy
  704. * @param {String} workspaceName - Name of the new workspace.
  705. * @param {Object} config={} - Contains any additional control attributes.
  706. * @param {String} destOrgId=null - Id of the organization where the destination workspace is present.
  707. * @returns {String} Copied workspace id.
  708. * @throws {Error} If the request failed due to some error.
  709. */
  710. async copy(workspaceName, config={}, destOrgId=null)
  711. {
  712. config.newWorkspaceName = workspaceName;
  713. var reqHeader = this.header;
  714. if(destOrgId != null)
  715. {
  716. reqHeader['ZANALYTICS-DEST-ORGID'] = destOrgId;
  717. }
  718. var result = await this.ac.handleV2Request(this.uriPath, "POST", config, reqHeader);
  719. return result.workspaceId;
  720. }
  721. /**
  722. * Rename a specified workspace in the organization.
  723. * @method rename
  724. * @param {String} workspaceName - New name for the workspace.
  725. * @param {Object} config={} - Contains any additional control attributes.
  726. * @throws {Error} If the request failed due to some error.
  727. */
  728. async rename(workspaceName, config={})
  729. {
  730. config.workspaceName = workspaceName;
  731. await this.ac.handleV2Request(this.uriPath, "PUT", config, this.header);
  732. }
  733. /**
  734. * Delete a specified workspace in the organization.
  735. * @method delete
  736. * @param {Object} config={} - Contains any additional control attributes.
  737. * @throws {Error} If the request failed due to some error.
  738. */
  739. async delete(config={})
  740. {
  741. await this.ac.handleV2Request(this.uriPath, "DELETE", config, this.header);
  742. }
  743. /**
  744. * Create a table in the specified workspace.
  745. * @method createTable
  746. * @param {Object} tableDesign - Table structure.
  747. * @param {Object} config={} - Contains any additional control attributes.
  748. * @returns {String} created table id.
  749. * @throws {Error} If the request failed due to some error.
  750. */
  751. async createTable(tableDesign, config={}) {
  752. var uriPath = this.uriPath + "/tables";
  753. config.tableDesign = tableDesign;
  754. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  755. return result.viewId;
  756. }
  757. /**
  758. * Create a new query table in the workspace.
  759. * @method createQueryTable
  760. * @param {String} sqlQuery - SQL query to construct the query table.
  761. * @param {String} queryTableName - Name of the query table to be created.
  762. * @param {Object} config={} - Contains any additional control attributes.
  763. * @returns {String} Id of the created query table.
  764. * @throws {Error} If the request failed due to some error.
  765. */
  766. async createQueryTable(sqlQuery, queryTableName, config={}) {
  767. var uriPath = this.uriPath + "/querytables";
  768. config.sqlQuery = sqlQuery
  769. config.queryTableName = queryTableName
  770. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  771. return result.viewId;
  772. }
  773. /**
  774. * Update the mentioned query table in the workspace.
  775. * @method editQueryTable
  776. * @param {String} viewId - Id of the query table to be updated.
  777. * @param {String} sqlQuery - New SQL query to be updated.
  778. * @param {Object} config={} - Contains any additional control attributes.
  779. * @throws {Error} If the request failed due to some error.
  780. */
  781. async editQueryTable(viewId, sqlQuery, config={}) {
  782. var uriPath = this.uriPath + "/querytables/" + viewId;
  783. config.sqlQuery = sqlQuery
  784. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  785. }
  786. /**
  787. * Returns the secret key of the specified workspace.
  788. * @method getSecretKey
  789. * @param {Object} config={} - Contains any additional control attributes.
  790. * @returns {String} Workspace secret key.
  791. * @throws {Error} If the request failed due to some error.
  792. */
  793. async getSecretKey(config={})
  794. {
  795. var uriPath = this.uriPath + "/secretkey";
  796. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  797. return result.workspaceKey;
  798. }
  799. /**
  800. * Adds a specified workspace as favorite.
  801. * @method addFavorite
  802. * @throws {Error} If the request failed due to some error.
  803. */
  804. async addFavorite(config={})
  805. {
  806. var uriPath = this.uriPath + "/favorite";
  807. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  808. }
  809. /**
  810. * Remove a specified workspace from favorite.
  811. * @method removeFavorite
  812. * @throws {Error} If the request failed due to some error.
  813. */
  814. async removeFavorite(config={})
  815. {
  816. var uriPath = this.uriPath + "/favorite";
  817. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  818. }
  819. /**
  820. * Adds a specified workspace as default.
  821. * @method addDefault
  822. * @throws {Error} If the request failed due to some error.
  823. */
  824. async addDefault(config={})
  825. {
  826. var uriPath = this.uriPath + "/default";
  827. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  828. }
  829. /**
  830. * Remove a specified workspace from default.
  831. * @method removeDefault
  832. * @throws {Error} If the request failed due to some error.
  833. */
  834. async removeDefault(config={})
  835. {
  836. var uriPath = this.uriPath + "/default";
  837. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  838. }
  839. /**
  840. * Returns list of admins for the specified workspace.
  841. * @method getAdmins
  842. * @returns {JSONArray} Workspace admin list.
  843. * @throws {Error} If the request failed due to some error.
  844. */
  845. async getAdmins(config={})
  846. {
  847. var uriPath = this.uriPath + "/admins";
  848. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  849. return result.workspaceAdmins;
  850. }
  851. /**
  852. * Add admins for the specified workspace.
  853. * @method addAdmins
  854. * @param {JSONArray} emailIds - The email address of the admin users to be added.
  855. * @param {Object} config={} - Contains any additional control attributes.
  856. * @throws {Error} If the request failed due to some error.
  857. */
  858. async addAdmins(emailIds, config={})
  859. {
  860. var uriPath = this.uriPath + "/admins";
  861. config.emailIds = emailIds;
  862. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  863. }
  864. /**
  865. * Remove admins from the specified workspace.
  866. * @method removeAdmins
  867. * @param {JSONArray} emailIds - The email address of the admin users to be removed.
  868. * @param {Object} config={} - Contains any additional control attributes.
  869. * @throws {Error} If the request failed due to some error.
  870. */
  871. async removeAdmins(emailIds, config={})
  872. {
  873. var uriPath = this.uriPath + "/admins";
  874. config.emailIds = emailIds;
  875. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  876. }
  877. /**
  878. * Returns shared details of the specified workspace.
  879. * @method getShareInfo
  880. * @returns {Object} Workspace share info.
  881. * @throws {Error} If the request failed due to some error.
  882. */
  883. async getShareInfo()
  884. {
  885. var uriPath = this.uriPath + "/share";
  886. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  887. return result;
  888. }
  889. /**
  890. * Share views to the specified users.
  891. * @method shareViews
  892. * @param {JSONArray} viewIds - View ids to be shared.
  893. * @param {JSONArray} emailIds - The email address of the users to whom the views need to be shared.
  894. * @param {Object} permissions - Contains permission details.
  895. * @param {Object} config={} - Contains any additional control attributes.
  896. * @throws {Error} If the request failed due to some error.
  897. */
  898. async shareViews(viewIds, emailIds, permissions, config={})
  899. {
  900. var uriPath = this.uriPath + "/share";
  901. config.viewIds = viewIds;
  902. config.emailIds = emailIds;
  903. config.permissions = permissions;
  904. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  905. }
  906. /**
  907. * Remove shared views for the specified users.
  908. * @method removeShare
  909. * @param {JSONArray} viewIds - View ids whose sharing needs to be removed.
  910. * @param {JSONArray} emailIds - The email address of the users to whom the sharing need to be removed.
  911. * @param {Object} config={} - Contains any additional control attributes.
  912. * @throws {Error} If the request failed due to some error.
  913. */
  914. async removeShare(viewIds, emailIds, config={})
  915. {
  916. var uriPath = this.uriPath + "/share";
  917. if(viewIds!=null)
  918. {
  919. config.viewIds = viewIds;
  920. }
  921. config.emailIds = emailIds;
  922. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  923. }
  924. /**
  925. * Returns shared details of the specified views.
  926. * @method getSharedDetailsForViews
  927. * @param {JSONArray} viewIds - The id of the views to be copied.
  928. * @returns {JSONArray} Folder list.
  929. * @throws {Error} If the request failed due to some error.
  930. */
  931. async getSharedDetailsForViews(viewIds)
  932. {
  933. var uriPath = this.uriPath + "/share/shareddetails";
  934. var config = {};
  935. config.viewIds = viewIds;
  936. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  937. return result.sharedDetails;
  938. }
  939. /**
  940. * Returns list of all accessible folders for the specified workspace.
  941. * @method getFolders
  942. * @returns {JSONArray} Folder list.
  943. * @throws {Error} If the request failed due to some error.
  944. */
  945. async getFolders()
  946. {
  947. var uriPath = this.uriPath + "/folders";
  948. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  949. return result.folders;
  950. }
  951. /**
  952. * Create a folder in the specified workspace.
  953. * @method createFolder
  954. * @param {String} folderName - Name of the folder to be created.
  955. * @param {Object} config={} - Contains any additional control attributes.
  956. * @returns {String} Created folder id.
  957. * @throws {Error} If the request failed due to some error.
  958. */
  959. async createFolder(folderName, config={})
  960. {
  961. var uriPath = this.uriPath + "/folders";
  962. config.folderName = folderName;
  963. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  964. return result.folderId;
  965. }
  966. /**
  967. * Returns list of all accessible views for the specified workspace.
  968. * @method getViews
  969. * @param {Object} config={} - Contains any additional control attributes.
  970. * @returns {JSONArray} View list.
  971. * @throws {Error} If the request failed due to some error.
  972. */
  973. async getViews(config={})
  974. {
  975. var uriPath = this.uriPath + "/views";
  976. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  977. return result.views;
  978. }
  979. /**
  980. * Copy the specified views from one workspace to another workspace.
  981. * @method copyViews
  982. * @param {JSONArray} viewIds - The id of the views to be copied.
  983. * @param {String} destWorkspaceId - The destination workspace id.
  984. * @param {Object} config={} - Contains any additional control attributes.
  985. * @param {String} destOrgId=null - Id of the organization where the destination workspace is present.
  986. * @returns {JSONArray} View list.
  987. * @throws {Error} If the request failed due to some error.
  988. */
  989. async copyViews(viewIds, destWorkspaceId, config={}, destOrgId=null) {
  990. var uriPath = this.uriPath + "/views/copy";
  991. config.viewIds = viewIds;
  992. config.destWorkspaceId = destWorkspaceId;
  993. var reqHeader = this.header;
  994. if(destOrgId != null)
  995. {
  996. reqHeader['ZANALYTICS-DEST-ORGID'] = destOrgId;
  997. }
  998. var result = await this.ac.handleV2Request(uriPath, "POST", config, reqHeader);
  999. return result.views;
  1000. }
  1001. /**
  1002. * Enable workspace to the specified white label domain.
  1003. * @method enableDomainAccess
  1004. * @throws {Error} If the request failed due to some error.
  1005. */
  1006. async enableDomainAccess()
  1007. {
  1008. var uriPath = this.uriPath + "/wlaccess";
  1009. await this.ac.handleV2Request(uriPath, "POST", null, this.header);
  1010. }
  1011. /**
  1012. * Disable workspace from the specified white label domain.
  1013. * @method disableDomainAccess
  1014. * @throws {Error} If the request failed due to some error.
  1015. */
  1016. async disableDomainAccess()
  1017. {
  1018. var uriPath = this.uriPath + "/wlaccess";
  1019. await this.ac.handleV2Request(uriPath, "DELETE", null, this.header);
  1020. }
  1021. /**
  1022. * Rename a specified folder in the workspace.
  1023. * @method renameFolder
  1024. * @param {String} folderId - Id of the folder.
  1025. * @param {String} folderName - New name for the folder.
  1026. * @param {Object} config={} - Contains any additional control attributes.
  1027. * @throws {Error} If the request failed due to some error.
  1028. */
  1029. async renameFolder(folderId, newFolderName, config={})
  1030. {
  1031. config.folderName = newFolderName;
  1032. var uriPath = this.uriPath + "/folders/" + folderId;
  1033. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1034. }
  1035. /**
  1036. * Delete a specified folder in the workspace.
  1037. * @method deleteFolder
  1038. * @param {String} folderId - Id of the folder to be deleted.
  1039. * @throws {Error} If the request failed due to some error.
  1040. */
  1041. async deleteFolder(folderId)
  1042. {
  1043. var uriPath = this.uriPath + "/folders/" + folderId;
  1044. await this.ac.handleV2Request(uriPath, "DELETE", null, this.header);
  1045. }
  1046. /**
  1047. * Returns list of groups for the specified workspace.
  1048. * @method getGroups
  1049. * @returns {JSONArray} Group list.
  1050. * @throws {Error} If the request failed due to some error.
  1051. */
  1052. async getGroups() {
  1053. var uriPath = this.uriPath + "/groups";
  1054. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1055. return result.groups;
  1056. }
  1057. /**
  1058. * Get the details of the specified group.
  1059. * @method getGroupDetails
  1060. * @param {String} groupId - Id of the group.
  1061. * @returns {Object} Details of the specified group.
  1062. * @throws {Error} If the request failed due to some error.
  1063. */
  1064. async getGroupDetails(groupId) {
  1065. var uriPath = this.uriPath + "/groups/" + groupId;
  1066. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1067. return result.groups;
  1068. }
  1069. /**
  1070. * Create a group in the specified workspace.
  1071. * @method createGroup
  1072. * @param {String} groupName - Name of the group.
  1073. * @param {JSONArray} emailIds - The email address of the users to be added to the group.
  1074. * @param {Object} config={} - Contains any additional control attributes.
  1075. * @returns {String} Created group id.
  1076. * @throws {Error} If the request failed due to some error.
  1077. */
  1078. async createGroup(groupName, emailIds, config={}) {
  1079. var uriPath = this.uriPath + "/groups";
  1080. config.groupName = groupName;
  1081. config.emailIds = emailIds;
  1082. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1083. return result.groupId;
  1084. }
  1085. /**
  1086. * Rename a specified group.
  1087. * @method renameGroup
  1088. * @param {String} groupId - Id of the group.
  1089. * @param {String} newGroupName - New name for the group.
  1090. * @param {Object} config={} - Contains any additional control attributes.
  1091. * @throws {Error} If the request failed due to some error.
  1092. */
  1093. async renameGroup(groupId, newGroupName, config={}) {
  1094. config.groupName = newGroupName;
  1095. var uriPath = this.uriPath + "/groups/" + groupId;
  1096. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1097. }
  1098. /**
  1099. * Add users to the specified group.
  1100. * @method addGroupMembers
  1101. * @param {String} groupId - Id of the group.
  1102. * @param {JSONArray} emailIds - The email address of the users to be added to the group.
  1103. * @param {Object} config={} - Contains any additional control attributes.
  1104. * @throws {Error} If the request failed due to some error.
  1105. */
  1106. async addGroupMembers(groupId, emailIds, config={}) {
  1107. config.emailIds = emailIds;
  1108. var uriPath = this.uriPath + "/groups/" + groupId + "/members";
  1109. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1110. }
  1111. /**
  1112. * Remove users from the specified group.
  1113. * @method removeGroupMembers
  1114. * @param {String} groupId - Id of the group.
  1115. * @param {JSONArray} emailIds - The email address of the users to be removed from the group.
  1116. * @param {Object} config={} - Contains any additional control attributes.
  1117. * @throws {Error} If the request failed due to some error.
  1118. */
  1119. async removeGroupMembers(groupId, emailIds, config={}) {
  1120. config.emailIds = emailIds;
  1121. var uriPath = this.uriPath + "/groups/" + groupId + "/members";
  1122. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  1123. }
  1124. /**
  1125. * Delete a specified group.
  1126. * @method deleteGroup
  1127. * @param {String} groupId - The id of the group.
  1128. * @throws {Error} If the request failed due to some error.
  1129. */
  1130. async deleteGroup(groupId) {
  1131. var uriPath = this.uriPath + "/groups/" + groupId;
  1132. await this.ac.handleV2Request(uriPath, "DELETE", null, this.header);
  1133. }
  1134. /**
  1135. * Create a slideshow in the specified workspace.
  1136. * @method createSlideshow
  1137. * @param {String} slideName - Name of the slideshow to be created.
  1138. * @param {JSONArray} viewIds - Ids of the view to be included in the slideshow.
  1139. * @param {Object} config={} - Contains any additional control attributes.
  1140. * @returns {String} Id of the created slideshow.
  1141. * @throws {Error} If the request failed due to some error.
  1142. */
  1143. async createSlideshow(slideName, viewIds, config={}) {
  1144. var uriPath = this.uriPath + "/slides";
  1145. config.slideName = slideName
  1146. config.viewIds = viewIds
  1147. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1148. return result.slideId;
  1149. }
  1150. /**
  1151. * Update details of the specified slideshow.
  1152. * @method updateSlideshow
  1153. * @param {String} slideId - The id of the slideshow.
  1154. * @param {Object} config={} - Contains the control configurations
  1155. * @throws {Error} If the request failed due to some error.
  1156. */
  1157. async updateSlideshow(slideId, config={}) {
  1158. var uriPath = this.uriPath + "/slides/" + slideId;
  1159. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1160. }
  1161. /**
  1162. * Delete a specified slideshow in the workspace.
  1163. * @method deleteSlideshow
  1164. * @param {String} slideId - Id of the slideshow.
  1165. * @throws {Error} If the request failed due to some error.
  1166. */
  1167. async deleteSlideshow(slideId) {
  1168. var uriPath = this.uriPath + "/slides/" + slideId;
  1169. await this.ac.handleV2Request(uriPath, "DELETE", null, this.header);
  1170. }
  1171. /**
  1172. * Returns list of slideshows for the specified workspace.
  1173. * @method getSlideshows
  1174. * @returns {JSONArray} Slideshow list.
  1175. * @throws {Error} If the request failed due to some error.
  1176. */
  1177. async getSlideshows() {
  1178. var uriPath = this.uriPath + "/slides";
  1179. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1180. return result.slideshows;
  1181. }
  1182. /**
  1183. * Returns slide URL to access the specified slideshow.
  1184. * @method getSlideshowUrl
  1185. * @param {String} slideId - Id of the slideshow.
  1186. * @param {Object} config={} - Contains the control configurations
  1187. * @returns {String} Slideshow URL.
  1188. * @throws {Error} If the request failed due to some error.
  1189. */
  1190. async getSlideshowUrl(slideId, config={}) {
  1191. var uriPath = this.uriPath + "/slides/" + slideId + "/publish";
  1192. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  1193. return result.slideUrl;
  1194. }
  1195. /**
  1196. * Returns details of the specified slideshow.
  1197. * @method getSlideshowDetails
  1198. * @param {String} slideId - Id of the slideshow.
  1199. * @returns {Object} Slideshow details.
  1200. * @throws {Error} If the request failed due to some error.
  1201. */
  1202. async getSlideshowDetails(slideId) {
  1203. var uriPath = this.uriPath + "/slides/" + slideId;
  1204. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1205. return result.slideInfo;
  1206. }
  1207. /**
  1208. * Create a variable in the workspace.
  1209. * @method createVariable
  1210. * @param {String} variableName - Name of the variable to be created.
  1211. * @param {Number} variableName - Datatype of the variable to be created.
  1212. * @param {Number} variableName - Type of the variable to be created.
  1213. * @param {Object} config={} - Contains the control attributes.
  1214. * @returns {String} Id of the created variable.
  1215. * @throws {Error} If the request failed due to some error.
  1216. */
  1217. async createVariable(variableName, variableDataType, variableType, config={}) {
  1218. var uriPath = this.uriPath + "/variables";
  1219. config.variableName = variableName
  1220. config.variableDataType = variableDataType
  1221. config.variableType = variableType
  1222. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1223. return result.variableId;
  1224. }
  1225. /**
  1226. * Update details of the specified variable in the workspace.
  1227. * @method updateVariable
  1228. * @param {String} variableId - Id of the variable.
  1229. * @param {String} variableName - New name for the variable.
  1230. * @param {Number} variableName - New datatype for the variable.
  1231. * @param {Number} variableName - New type for the variable.
  1232. * @param {Object} config={} - Contains the control attributes.
  1233. * @throws {Error} If the request failed due to some error.
  1234. */
  1235. async updateVariable(variableId, variableName, variableDataType, variableType, config={}) {
  1236. var uriPath = this.uriPath + "/variables/" + variableId;
  1237. config.variableName = variableName
  1238. config.variableDataType = variableDataType
  1239. config.variableType = variableType
  1240. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1241. }
  1242. /**
  1243. * Delete a specified variable in the workspace.
  1244. * @method deleteVariable
  1245. * @param {String} variableId - Id of the variable.
  1246. * @throws {Error} If the request failed due to some error.
  1247. */
  1248. async deleteVariable(variableId) {
  1249. var uriPath = this.uriPath + "/variables/" + variableId;
  1250. await this.ac.handleV2Request(uriPath, "DELETE", null, this.header);
  1251. }
  1252. /**
  1253. * Returns list of variables for the specified workspace.
  1254. * @method getVariables
  1255. * @returns {JSONArray} Variable list.
  1256. * @throws {Error} If the request failed due to some error.
  1257. */
  1258. async getVariables() {
  1259. var uriPath = this.uriPath + "/variables";
  1260. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1261. return result.variables;
  1262. }
  1263. /**
  1264. * Returns details of the specified variable.
  1265. * @method getVariableDetails
  1266. * @param {String} variableId - Id of the variable.
  1267. * @returns {Object} Variable details.
  1268. * @throws {Error} If the request failed due to some error.
  1269. */
  1270. async getVariableDetails(variableId) {
  1271. var uriPath = this.uriPath + "/variables/" + variableId;
  1272. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1273. return result;
  1274. }
  1275. /**
  1276. * Make the specified folder as default.
  1277. * @method makeDefaultFolder
  1278. * @param {String} folderId - Id of the folder.
  1279. * @throws {Error} If the request failed due to some error.
  1280. */
  1281. async makeDefaultFolder(folderId) {
  1282. var uriPath = this.uriPath + "/folders/" + folderId + "/default";
  1283. await this.ac.handleV2Request(uriPath, "PUT", null, this.header);
  1284. }
  1285. /**
  1286. * Returns list of datasources for the specified workspace.
  1287. * @method getDatasources
  1288. * @returns {JSONArray} Datasource list.
  1289. * @throws {Error} If the request failed due to some error.
  1290. */
  1291. async getDatasources() {
  1292. var uriPath = this.uriPath + "/datasources";
  1293. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1294. return result.dataSources;
  1295. }
  1296. /**
  1297. * Initiate data sync for the specified datasource.
  1298. * @method syncData
  1299. * @param {String} datasourceId - Id of the datasource.
  1300. * @param {Object} config={} - Contains any additional control attributes.
  1301. * @throws {Error} If the request failed due to some error.
  1302. */
  1303. async syncData(datasourceId, config={}) {
  1304. var uriPath = this.uriPath + "/datasources/" + datasourceId + "/sync";
  1305. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1306. }
  1307. /**
  1308. * Update connection details for the specified datasource.
  1309. * @method updateDatasourceConnection
  1310. * @param {String} datasourceId - Id of the datasource.
  1311. * @param {Object} config={} - Contains the control attributes.
  1312. * @throws {Error} If the request failed due to some error.
  1313. */
  1314. async updateDatasourceConnection(datasourceId, config={}) {
  1315. var uriPath = this.uriPath + "/datasources/" + datasourceId;
  1316. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1317. }
  1318. /**
  1319. * Returns list of all views available in trash for the specified workspace.
  1320. * @method getTrashViews
  1321. * @returns {JSONArray} Trash view list.
  1322. * @throws {Error} If the request failed due to some error.
  1323. */
  1324. async getTrashViews() {
  1325. var uriPath = this.uriPath + "/trash";
  1326. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1327. return result.views;
  1328. }
  1329. /**
  1330. * Restore the specified view from trash.
  1331. * @method restoreTrashView
  1332. * @param {String} viewId - Id of the view.
  1333. * @param {Object} config={} - Contains any additional control attributes.
  1334. * @throws {Error} If the request failed due to some error.
  1335. */
  1336. async restoreTrashView(viewId, config={}) {
  1337. var uriPath = this.uriPath + "/trash/" + viewId;
  1338. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1339. }
  1340. /**
  1341. * Delete the specified view permanently from trash.
  1342. * @method deleteTrashView
  1343. * @param {String} viewId - Id of the view.
  1344. * @param {Object} config={} - Contains any additional control attributes.
  1345. * @throws {Error} If the request failed due to some error.
  1346. */
  1347. async deleteTrashView(viewId, config={}) {
  1348. var uriPath = this.uriPath + "/trash/" + viewId;
  1349. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  1350. }
  1351. /**
  1352. * Swaps the hierarchy of a parent folder and a subfolder.
  1353. * @method changeFolderHierarchy
  1354. * @param {String} folderId - Id of the folder.
  1355. * @param {Number} hierarchy - New hierarchy for the folder. (0 - Parent; 1 - Child).
  1356. * @param {Object} config={} - Contains any additional control attributes.
  1357. * @throws {Error} If the request failed due to some error.
  1358. */
  1359. async changeFolderHierarchy(folderId, hierarchy, config={}) {
  1360. var uriPath = this.uriPath + "/folders/" + folderId + "/move";
  1361. config.hierarchy = hierarchy;
  1362. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1363. }
  1364. /**
  1365. * Place the folder above the reference folder.
  1366. * @method changeFolderPosition
  1367. * @param {String} folderId - Id of the folder.
  1368. * @param {String} referenceFolderId - Id of the reference folder.
  1369. * @param {Object} config={} - Contains any additional control attributes.
  1370. * @throws {Error} If the request failed due to some error.
  1371. */
  1372. async changeFolderPosition(folderId, referenceFolderId, config={}) {
  1373. var uriPath = this.uriPath + "/folders/" + folderId + "/reorder";
  1374. config.referenceFolderId = referenceFolderId;
  1375. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1376. }
  1377. /**
  1378. * Move views to the mentioned folder.
  1379. * @method moveViewsToFolder
  1380. * @param {String} folderId - Id of the folder.
  1381. * @param {JSONArray} viewIds - View ids to be moved.
  1382. * @param {Object} config={} - Contains any additional control attributes.
  1383. * @throws {Error} If the request failed due to some error.
  1384. */
  1385. async moveViewsToFolder(folderId, viewIds, config={}) {
  1386. var uriPath = this.uriPath + "/views/movetofolder";
  1387. config.folderId = folderId;
  1388. config.viewIds = viewIds;
  1389. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1390. }
  1391. /**
  1392. * Export the mentioned views as templates.
  1393. * @method exportAsTemplate
  1394. * @param {JSONArray} viewIds - Ids of the view to be exported.
  1395. * @param {String} filePath - Path of the file where the data exported to be stored. ( Should be in 'atpt' format )
  1396. * @param {Object} config={} - Contains any additional control attributes.
  1397. * @throws {Error} If the request failed due to some error.
  1398. */
  1399. async exportAsTemplate(viewIds, filePath, config={})
  1400. {
  1401. var uriPath = this.uriPath + "/template/data";
  1402. config.viewIds = viewIds;
  1403. await this.ac.handleExportRequest(uriPath, filePath, config, this.header);
  1404. }
  1405. /**
  1406. * Returns list of users for the specified workspace.
  1407. * @method getWorkspaceUsers
  1408. * @returns {JSONArray} User list.
  1409. * @throws {Error} If the request failed due to some error.
  1410. */
  1411. async getWorkspaceUsers() {
  1412. var uriPath = this.uriPath + "/users";
  1413. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1414. return result.users;
  1415. }
  1416. /**
  1417. * Add users to the specified workspace.
  1418. * @method addWorkspaceUsers
  1419. * @param {JSONArray} emailIds - The email address of the users to be added.
  1420. * @param {String} role - Role for the users to be added.
  1421. * @param {Object} config={} - Contains any additional control attributes.
  1422. * @throws {Error} If the request failed due to some error.
  1423. */
  1424. async addWorkspaceUsers(emailIds, role, config={}) {
  1425. var uriPath = this.uriPath + "/users";
  1426. config.emailIds = emailIds;
  1427. config.role = role;
  1428. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1429. }
  1430. /**
  1431. * Remove users from the specified workspace.
  1432. * @method removeWorkspaceUsers
  1433. * @param {JSONArray} emailIds - The email address of the users to be removed.
  1434. * @param {Object} config={} - Contains any additional control attributes.
  1435. * @throws {Error} If the request failed due to some error.
  1436. */
  1437. async removeWorkspaceUsers(emailIds, config={}) {
  1438. var uriPath = this.uriPath + "/users";
  1439. config.emailIds = emailIds;
  1440. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  1441. }
  1442. /**
  1443. * Change users staus in the specified workspace.
  1444. * @method changeWorkspaceUserStatus
  1445. * @param {JSONArray} emailIds - The email address of the users.
  1446. * @param {String} operation - New status for the users ( Values - activate | deactivate )
  1447. * @param {Object} config={} - Contains any additional control attributes.
  1448. * @throws {Error} If the request failed due to some error.
  1449. */
  1450. async changeWorkspaceUserStatus(emailIds, operation, config={}) {
  1451. var uriPath = this.uriPath + "/users/status";
  1452. config.emailIds = emailIds;
  1453. config.operation = operation;
  1454. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1455. }
  1456. /**
  1457. * Change role for the specified users.
  1458. * @method changeWorkspaceUserRole
  1459. * @param {JSONArray} emailIds - The email address of the users.
  1460. * @param {String} role - New role for the users.
  1461. * @param {Object} config={} - Contains any additional control attributes.
  1462. * @throws {Error} If the request failed due to some error.
  1463. */
  1464. async changeWorkspaceUserRole(emailIds, role, config={}) {
  1465. var uriPath = this.uriPath + "/users/role";
  1466. config.emailIds = emailIds;
  1467. config.role = role;
  1468. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1469. }
  1470. /**
  1471. * Returns list of email schedules available in the specified workspace.
  1472. * @method getEmailSchedules
  1473. * @returns {JSONArray} List of email schedules.
  1474. * @throws {Error} If the request failed due to some error.
  1475. */
  1476. async getEmailSchedules() {
  1477. var uriPath = this.uriPath + "/emailschedules";
  1478. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1479. return result.emailSchedules;
  1480. }
  1481. /**
  1482. * Create an email schedule in the specified workspace.
  1483. * @method createEmailSchedule
  1484. * @param {String} scheduleName - Name of the email schedule.
  1485. * @param {JSONArray} viewIds - View ids to be mailed.
  1486. * @param {String} format - The format in which the data has to be mailed.
  1487. * @param {JSONArray} emailIds - The recipients' email addresses for sending views.
  1488. * @param {Object} scheduleDetails - Contains schedule frequency, date, and time info.
  1489. * @param {Object} config={} - Contains any additional control attributes.
  1490. * @returns {String} Created email schedule id.
  1491. * @throws {Error} If the request failed due to some error.
  1492. */
  1493. async createEmailSchedule(scheduleName, viewIds, format, emailIds, scheduleDetails, config={}) {
  1494. var uriPath = this.uriPath + "/emailschedules";
  1495. config.scheduleName = scheduleName;
  1496. config.viewIds = viewIds;
  1497. config.exportType = format;
  1498. config.emailIds = emailIds;
  1499. config.scheduleDetails = scheduleDetails;
  1500. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1501. return result.scheduleId;
  1502. }
  1503. /**
  1504. * Update configurations of the specified email schedule in the workspace.
  1505. * @method updateEmailSchedule
  1506. * @param {String} scheduleId - Id for the email schedule.
  1507. * @param {Object} config - Contains the control configurations.
  1508. * @returns {String} Updated email schedule id.
  1509. * @throws {Error} If the request failed due to some error.
  1510. */
  1511. async updateEmailSchedule(scheduleId, config) {
  1512. var uriPath = this.uriPath + "/emailschedules/" + scheduleId;
  1513. var result = await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1514. return result.scheduleId;
  1515. }
  1516. /**
  1517. * Trigger configured email schedules instantly.
  1518. * @method triggerEmailSchedule
  1519. * @param {String} scheduleId - Id for the email schedule.
  1520. * @throws {Error} If the request failed due to some error.
  1521. */
  1522. async triggerEmailSchedule(scheduleId) {
  1523. var uriPath = this.uriPath + "/emailschedules/" + scheduleId;
  1524. await this.ac.handleV2Request(uriPath, "POST", null, this.header);
  1525. }
  1526. /**
  1527. * Update email schedule status.
  1528. * @method changeEmailScheduleStatus
  1529. * @param {String} scheduleId - Id for the email schedule.
  1530. * @param {String} operation - New status for the schedule ( Values - activate | deactivate )
  1531. * @throws {Error} If the request failed due to some error.
  1532. */
  1533. async changeEmailScheduleStatus(scheduleId, operation) {
  1534. var uriPath = this.uriPath + "/emailschedules/" + scheduleId + "/status";
  1535. var config = { operation: operation };
  1536. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1537. }
  1538. /**
  1539. * Delete the specified email schedule in the workspace.
  1540. * @method deleteEmailSchedule
  1541. * @param {String} scheduleId - Id for the email schedule.
  1542. * @throws {Error} If the request failed due to some error.
  1543. */
  1544. async deleteEmailSchedule(scheduleId) {
  1545. var uriPath = this.uriPath + "/emailschedules/" + scheduleId;
  1546. await this.ac.handleV2Request(uriPath, "DELETE", null, this.header);
  1547. }
  1548. }
  1549. class ViewAPI
  1550. {
  1551. constructor(ac, orgId, workspaceId, viewId)
  1552. {
  1553. this.ac = ac;
  1554. this.uriPath = "/restapi/v2/workspaces/" + workspaceId + "/views/" + viewId
  1555. this.header = {};
  1556. this.header['ZANALYTICS-ORGID'] = orgId;
  1557. }
  1558. /**
  1559. * Rename a specified view in the workspace.
  1560. * @method rename
  1561. * @param {String} viewName - New name of the view.
  1562. * @param {Object} config={} - Contains any additional control attributes.
  1563. * @throws {Error} If the request failed due to some error.
  1564. */
  1565. async rename(viewName, config={}) {
  1566. config.viewName = viewName;
  1567. await this.ac.handleV2Request(this.uriPath, "PUT", config, this.header);
  1568. }
  1569. /**
  1570. * Delete a specified view in the workspace.
  1571. * @method delete
  1572. * @param {Object} config={} - Contains any additional control attributes.
  1573. * @throws {Error} If the request failed due to some error.
  1574. */
  1575. async delete(config={}) {
  1576. await this.ac.handleV2Request(this.uriPath, "DELETE", config, this.header);
  1577. }
  1578. /**
  1579. * Copy a specified view within the workspace.
  1580. * @method saveAs
  1581. * @param {String} newViewName - The name of the new view.
  1582. * @param {Object} config={} - Contains any additional control attributes.
  1583. * @returns {String} Newly created view id.
  1584. * @throws {Error} If the request failed due to some error.
  1585. */
  1586. async saveAs(newViewName, config={}) {
  1587. var uriPath = this.uriPath + "/saveas";
  1588. config.viewName = newViewName;
  1589. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1590. return result.viewId;
  1591. }
  1592. /**
  1593. * Copy the specified formulas from one table to another within the workspace or across workspaces.
  1594. * @method copyFormulas
  1595. * @param {JSONArray} formulaNames - The name of the formula columns to be copied.
  1596. * @param {String} destWorkspaceId - The ID of the destination workspace.
  1597. * @param {Object} config={} - Contains any additional control attributes.
  1598. * @param {String} destOrgId=null - Id of the organization where the destination workspace is present.
  1599. * @throws {Error} If the request failed due to some error.
  1600. */
  1601. async copyFormulas(formulaNames, destWorkspaceId, config={}, destOrgId=null) {
  1602. var uriPath = this.uriPath + "/formulas/copy";
  1603. config.formulaColumnNames = formulaNames;
  1604. config.destWorkspaceId = destWorkspaceId;
  1605. var reqHeader = this.header;
  1606. if(destOrgId != null)
  1607. {
  1608. reqHeader['ZANALYTICS-DEST-ORGID'] = destOrgId;
  1609. }
  1610. await this.ac.handleV2Request(uriPath, "POST", config, reqHeader);
  1611. }
  1612. /**
  1613. * Adds a specified view as favorite.
  1614. * @method addFavorite
  1615. * @throws {Error} If the request failed due to some error.
  1616. */
  1617. async addFavorite(config={})
  1618. {
  1619. var uriPath = this.uriPath + "/favorite";
  1620. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1621. }
  1622. /**
  1623. * Remove a specified view from favorite.
  1624. * @method removeFavorite
  1625. * @throws {Error} If the request failed due to some error.
  1626. */
  1627. async removeFavorite(config={})
  1628. {
  1629. var uriPath = this.uriPath + "/favorite";
  1630. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  1631. }
  1632. /**
  1633. * Create reports for the specified table based on the reference table.
  1634. * @method createSimilarViews
  1635. * @param {String} refViewId - The ID of the reference view.
  1636. * @param {String} folderId - The folder id where the views to be saved.
  1637. * @param {Object} config={} - Contains any additional control attributes.
  1638. * @throws {Error} If the request failed due to some error.
  1639. */
  1640. async createSimilarViews(refViewId, folderId, config={}) {
  1641. var uriPath = this.uriPath + "/similarviews";
  1642. config.referenceViewId = refViewId;
  1643. config.folderId = folderId;
  1644. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1645. }
  1646. /**
  1647. * Auto generate reports for the specified table.
  1648. * @method autoAnalyse
  1649. * @param {Object} config={} - Contains any additional control attributes.
  1650. * @throws {Error} If the request failed due to some error.
  1651. */
  1652. async autoAnalyse(config={}) {
  1653. var uriPath = this.uriPath + "/autoanalyse";
  1654. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1655. }
  1656. /**
  1657. * Returns permissions for the specified view.
  1658. * @method removeShare
  1659. * @returns {Object} Permission details.
  1660. * @throws {Error} If the request failed due to some error.
  1661. */
  1662. async getMyPermissions()
  1663. {
  1664. var uriPath = this.uriPath + "/share/userpermissions";
  1665. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1666. return result.permissions;
  1667. }
  1668. /**
  1669. * Returns the URL to access the specified view.
  1670. * @method getViewUrl
  1671. * @param {Object} config={} - Contains any additional control attributes.
  1672. * @returns {String} View URL.
  1673. * @throws {Error} If the request failed due to some error.
  1674. */
  1675. async getViewUrl(config={}) {
  1676. var uriPath = this.uriPath + "/publish";
  1677. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  1678. return result.viewUrl;
  1679. }
  1680. /**
  1681. * Returns embed URL to access the specified view.
  1682. * @method getEmbedUrl
  1683. * @param {Object} config={} - Contains any additional control attributes.
  1684. * @returns {String} Embed URL.
  1685. * @throws {Error} If the request failed due to some error.
  1686. */
  1687. async getEmbedUrl(config={}) {
  1688. var uriPath = this.uriPath + "/publish/embed";
  1689. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  1690. return result.embedUrl;
  1691. }
  1692. /**
  1693. * Returns private URL to access the specified view.
  1694. * @method getPrivateUrl
  1695. * @param {Object} config={} - Contains any additional control attributes.
  1696. * @returns {String} Private URL.
  1697. * @throws {Error} If the request failed due to some error.
  1698. */
  1699. async getPrivateUrl(config={}) {
  1700. var uriPath = this.uriPath + "/publish/privatelink";
  1701. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  1702. return result.privateUrl;
  1703. }
  1704. /**
  1705. * Create a private URL for the specified view.
  1706. * @method createPrivateUrl
  1707. * @param {Object} config={} - Contains any additional control attributes.
  1708. * @returns {String} Private URL.
  1709. * @throws {Error} If the request failed due to some error.
  1710. */
  1711. async createPrivateUrl(config={}) {
  1712. var uriPath = this.uriPath + "/publish/privatelink";
  1713. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1714. return result.privateUrl;
  1715. }
  1716. /**
  1717. * Remove private link access for the specified view.
  1718. * @method removePrivateAccess
  1719. * @throws {Error} If the request failed due to some error.
  1720. */
  1721. async removePrivateAccess() {
  1722. var uriPath = this.uriPath + "/publish/privatelink";
  1723. await this.ac.handleV2Request(uriPath, "DELETE", null, this.header);
  1724. }
  1725. /**
  1726. * Make the specified view publically accessible.
  1727. * @method makeViewPublic
  1728. * @param {Object} config={} - Contains any additional control attributes.
  1729. * @returns {String} Public URL.
  1730. * @throws {Error} If the request failed due to some error.
  1731. */
  1732. async makeViewPublic(config={}) {
  1733. var uriPath = this.uriPath + "/publish/public";
  1734. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1735. return result.publicUrl;
  1736. }
  1737. /**
  1738. * Remove public access for the specified view.
  1739. * @method removePublicAccess
  1740. * @throws {Error} If the request failed due to some error.
  1741. */
  1742. async removePublicAccess() {
  1743. var uriPath = this.uriPath + "/publish/public";
  1744. await this.ac.handleV2Request(uriPath, "DELETE", null, this.header);
  1745. }
  1746. /**
  1747. * Returns publish configurations for the specified view.
  1748. * @method getPublishConfigurations
  1749. * @returns {Object} Publish details.
  1750. * @throws {Error} If the request failed due to some error.
  1751. */
  1752. async getPublishConfigurations()
  1753. {
  1754. var uriPath = this.uriPath + "/publish/config";
  1755. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1756. return result;
  1757. }
  1758. /**
  1759. * Update publish configurations for the specified view.
  1760. * @method updatePublishConfigurations
  1761. * @param {Object} config={} - Contains the required control attributes.
  1762. * @throws {Error} If the request failed due to some error.
  1763. */
  1764. async updatePublishConfigurations(config={}) {
  1765. var uriPath = this.uriPath + "/publish/config";
  1766. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1767. }
  1768. /**
  1769. * Add a column in the specified table.
  1770. * @method addColumn
  1771. * @param {String} columnName - The name of the column.
  1772. * @param {String} dataType - The data-type of the column.
  1773. * @param {Object} config={} - Contains any additional control attributes.
  1774. * @returns {String} Created column id.
  1775. * @throws {Error} If the request failed due to some error.
  1776. */
  1777. async addColumn(columnName, dataType, config={}) {
  1778. var uriPath = this.uriPath + "/columns";
  1779. config.columnName = columnName;
  1780. config.dataType = dataType;
  1781. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1782. return result.columnId;
  1783. }
  1784. /**
  1785. * Hide the specified columns in the table.
  1786. * @method hideColumns
  1787. * @param {JSONArray} columnIds - Ids of the columns to be hidden.
  1788. * @param {Object} config={} - Contains any additional control attributes.
  1789. * @throws {Error} If the request failed due to some error.
  1790. */
  1791. async hideColumns(columnIds, config={}) {
  1792. var uriPath = this.uriPath + "/columns/hide";
  1793. config.columnIds = columnIds;
  1794. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1795. }
  1796. /**
  1797. * Show the specified hidden columns in the table.
  1798. * @method showColumns
  1799. * @param {JSONArray} columnIds - Ids of the columns to be shown.
  1800. * @param {Object} config={} - Contains any additional control attributes.
  1801. * @throws {Error} If the request failed due to some error.
  1802. */
  1803. async showColumns(columnIds, config={}) {
  1804. var uriPath = this.uriPath + "/columns/show";
  1805. config.columnIds = columnIds;
  1806. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1807. }
  1808. /**
  1809. * Add a single row in the specified table.
  1810. * @method addRow
  1811. * @param {Object} columnValues - Contains the values for the row. The column names are the key.
  1812. * @param {Object} config={} - Contains any additional control attributes.
  1813. * @returns {Object} Column Names and Added Row Values.
  1814. * @throws {Error} If the request failed due to some error.
  1815. */
  1816. async addRow(columnValues, config={})
  1817. {
  1818. var uriPath = this.uriPath + "/rows";
  1819. config.columns = columnValues;
  1820. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1821. return result;
  1822. }
  1823. /**
  1824. * Update rows in the specified table.
  1825. * @method updateRow
  1826. * @param {Object} columnValues - Contains the values for the row. The column names are the key.
  1827. * @param {String} criteria - The criteria to be applied for updating data. Only rows matching the criteria will be updated. Should be null for update all rows.
  1828. * @param {Object} config={} - Contains any additional control attributes.
  1829. * @returns {Object} Updated Columns List and Updated Rows Count.
  1830. * @throws {Error} If the request failed due to some error.
  1831. */
  1832. async updateRow(columnValues, criteria, config={})
  1833. {
  1834. var uriPath = this.uriPath + "/rows";
  1835. config.columns = columnValues;
  1836. if(criteria != null && criteria.length>0)
  1837. {
  1838. config.criteria = criteria;
  1839. }
  1840. var result = await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1841. return result;
  1842. }
  1843. /**
  1844. * Delete rows in the specified table.
  1845. * @method deleteRow
  1846. * @param {String} criteria - The criteria to be applied for deleting data. Only rows matching the criteria will be deleted. Should be null for delete all rows.
  1847. * @param {Object} config={} - Contains any additional control attributes.
  1848. * @returns {int} Deleted rows count.
  1849. * @throws {Error} If the request failed due to some error.
  1850. */
  1851. async deleteRow(criteria, config={})
  1852. {
  1853. var uriPath = this.uriPath + "/rows";
  1854. if(criteria != null && criteria.length>0)
  1855. {
  1856. config.criteria = criteria;
  1857. }
  1858. var result = await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  1859. return result.deletedRows;
  1860. }
  1861. /**
  1862. * Rename a specified column in the table.
  1863. * @method renameColumn
  1864. * @param {String} columnId - Id of the column.
  1865. * @param {String} newColumnName - New name for the column.
  1866. * @param {Object} config={} - Contains any additional control attributes.
  1867. * @throws {Error} If the request failed due to some error.
  1868. */
  1869. async renameColumn(columnId, newColumnName, config={}) {
  1870. var uriPath = this.uriPath + "/columns/" + columnId;
  1871. config.columnName = newColumnName;
  1872. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1873. }
  1874. /**
  1875. * Delete a specified column in the table.
  1876. * @method deleteColumn
  1877. * @param {String} columnId - Id of the column.
  1878. * @param {Object} config={} - Contains any additional control attributes.
  1879. * @throws {Error} If the request failed due to some error.
  1880. */
  1881. async deleteColumn(columnId, config={}) {
  1882. var uriPath = this.uriPath + "/columns/" + columnId;
  1883. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  1884. }
  1885. /**
  1886. * Add a lookup in the specified child table.
  1887. * @method addLookup
  1888. * @param {String} columnId - Id of the column.
  1889. * @param {String} refViewId - The id of the table contains the parent column.
  1890. * @param {String} refColumnId - The id of the parent column.
  1891. * @param {Object} config={} - Contains any additional control attributes.
  1892. * @throws {Error} If the request failed due to some error.
  1893. */
  1894. async addLookup(columnId, refViewId, refColumnId, config={}) {
  1895. var uriPath = this.uriPath + "/columns/" + columnId + "/lookup";
  1896. config.referenceViewId = refViewId;
  1897. config.referenceColumnId = refColumnId;
  1898. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1899. }
  1900. /**
  1901. * Remove the lookup for the specified column in the table.
  1902. * @method removeLookup
  1903. * @param {String} columnId - Id of the column.
  1904. * @param {Object} config={} - Contains any additional control attributes.
  1905. * @throws {Error} If the request failed due to some error.
  1906. */
  1907. async removeLookup(columnId, config={}) {
  1908. var uriPath = this.uriPath + "/columns/" + columnId + "/lookup";
  1909. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  1910. }
  1911. /**
  1912. * Auto generate reports for the specified column.
  1913. * @method autoAnalyseColumn
  1914. * @param {String} columnId - Id of the column.
  1915. * @param {Object} config={} - Contains any additional control attributes.
  1916. * @throws {Error} If the request failed due to some error.
  1917. */
  1918. async autoAnalyseColumn(columnId, config={}) {
  1919. var uriPath = this.uriPath + "/columns/" + columnId + "/autoanalyse";
  1920. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1921. }
  1922. /**
  1923. * Sync data from available datasource for the specified view.
  1924. * @method refetchData
  1925. * @param {String} viewName - New name of the view.
  1926. * @param {Object} config={} - Contains any additional control attributes.
  1927. * @throws {Error} If the request failed due to some error.
  1928. */
  1929. async refetchData(config={}) {
  1930. var uriPath = this.uriPath + "/sync";
  1931. await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1932. }
  1933. /**
  1934. * Returns last import details of the specified view.
  1935. * @method getLastImportDetails
  1936. * @returns {int} Last import details.
  1937. * @throws {Error} If the request failed due to some error.
  1938. */
  1939. async getLastImportDetails()
  1940. {
  1941. var uriPath = this.uriPath + "/importdetails";
  1942. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1943. return result;
  1944. }
  1945. /**
  1946. * Returns list of all formula columns for the specified table.
  1947. * @method getFormulaColumns
  1948. * @returns {JSONArray} Formula column list.
  1949. * @throws {Error} If the request failed due to some error.
  1950. */
  1951. async getFormulaColumns()
  1952. {
  1953. var uriPath = this.uriPath + "/customformulas";
  1954. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  1955. return result.customFormulas;
  1956. }
  1957. /**
  1958. * Add a formula column in the specified table.
  1959. * @method addFormulaColumn
  1960. * @param {String} formulaName - Name of the formula column to be created.
  1961. * @param {String} expression - Formula expression.
  1962. * @param {Object} config={} - Contains any additional control attributes.
  1963. * @returns {String} Created formula column id.
  1964. * @throws {Error} If the request failed due to some error.
  1965. */
  1966. async addFormulaColumn(formulaName, expression, config={}) {
  1967. var uriPath = this.uriPath + "/customformulas";
  1968. config.formulaName = formulaName;
  1969. config.expression = expression;
  1970. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  1971. return result.formulaId;
  1972. }
  1973. /**
  1974. * Edit the specified formula column.
  1975. * @method editFormulaColumn
  1976. * @param {String} formulaId - Id of the formula column to be updated.
  1977. * @param {String} expression - Formula expression.
  1978. * @param {Object} config={} - Contains any additional control attributes.
  1979. * @throws {Error} If the request failed due to some error.
  1980. */
  1981. async editFormulaColumn(formulaId, expression, config={}) {
  1982. var uriPath = this.uriPath + "/customformulas/" + formulaId;
  1983. config.expression = expression;
  1984. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  1985. }
  1986. /**
  1987. * Delete the specified formula column.
  1988. * @method deleteFormulaColumn
  1989. * @param {String} formulaId - Id of the formula column to be deleted.
  1990. * @param {Object} config={} - Contains any additional control attributes.
  1991. * @throws {Error} If the request failed due to some error.
  1992. */
  1993. async deleteFormulaColumn(formulaId, config={}) {
  1994. var uriPath = this.uriPath + "/customformulas/" + formulaId;
  1995. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  1996. }
  1997. /**
  1998. * Returns list of all aggregate formulas for the specified table.
  1999. * @method getAggregateFormulas
  2000. * @returns {JSONArray} Aggregate formula list.
  2001. * @throws {Error} If the request failed due to some error.
  2002. */
  2003. async getAggregateFormulas()
  2004. {
  2005. var uriPath = this.uriPath + "/aggregateformulas";
  2006. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  2007. return result.aggregateFormulas;
  2008. }
  2009. /**
  2010. * Add a aggregate formula in the specified table.
  2011. * @method addAggregateFormula
  2012. * @param {String} formulaName - Name of the aggregate formula to be created.
  2013. * @param {String} expression - Formula expression.
  2014. * @param {Object} config={} - Contains any additional control attributes.
  2015. * @returns {String} Created aggregate formula id.
  2016. * @throws {Error} If the request failed due to some error.
  2017. */
  2018. async addAggregateFormula(formulaName, expression, config={}) {
  2019. var uriPath = this.uriPath + "/aggregateformulas";
  2020. config.formulaName = formulaName;
  2021. config.expression = expression;
  2022. var result = await this.ac.handleV2Request(uriPath, "POST", config, this.header);
  2023. return result.formulaId;
  2024. }
  2025. /**
  2026. * Edit the specified aggregate formula.
  2027. * @method editAggregateFormula
  2028. * @param {String} formulaId - Id of the aggregate formula to be updated.
  2029. * @param {String} expression - Formula expression.
  2030. * @param {Object} config={} - Contains any additional control attributes.
  2031. * @throws {Error} If the request failed due to some error.
  2032. */
  2033. async editAggregateFormula(formulaId, expression, config={}) {
  2034. var uriPath = this.uriPath + "/aggregateformulas/" + formulaId;
  2035. config.expression = expression;
  2036. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  2037. }
  2038. /**
  2039. * Delete the specified aggregate formula.
  2040. * @method deleteAggregateFormula
  2041. * @param {String} formulaId - Id of the aggregate formula to be deleted.
  2042. * @param {Object} config={} - Contains any additional control attributes.
  2043. * @throws {Error} If the request failed due to some error.
  2044. */
  2045. async deleteAggregateFormula(formulaId, config={}) {
  2046. var uriPath = this.uriPath + "/aggregateformulas/" + formulaId;
  2047. await this.ac.handleV2Request(uriPath, "DELETE", config, this.header);
  2048. }
  2049. /**
  2050. * Returns list of dependents views for the specified view.
  2051. * @method getViewDependents
  2052. * @returns {JSONArray} Dependent view list.
  2053. * @throws {Error} If the request failed due to some error.
  2054. */
  2055. async getViewDependents()
  2056. {
  2057. var uriPath = this.uriPath + "/dependents";
  2058. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  2059. return result.views;
  2060. }
  2061. /**
  2062. * Returns list of dependents views and formulas for the specified column.
  2063. * @method getColumnDependents
  2064. * @param {String} columnId - Id of the column.
  2065. * @returns {JSONObject} Dependent details.
  2066. * @throws {Error} If the request failed due to some error.
  2067. */
  2068. async getColumnDependents(columnId)
  2069. {
  2070. var uriPath = this.uriPath + "/columns/" + columnId + "/dependents";
  2071. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  2072. return result;
  2073. }
  2074. /**
  2075. * Update shared details of the specified view.
  2076. * @method updateSharedDetails
  2077. * @param {Object} config={} - Contains the control attributes.
  2078. * @throws {Error} If the request failed due to some error.
  2079. */
  2080. async updateSharedDetails(config={})
  2081. {
  2082. var uriPath = this.uriPath + "/share";
  2083. await this.ac.handleV2Request(uriPath, "PUT", config, this.header);
  2084. }
  2085. }
  2086. class BulkAPI
  2087. {
  2088. constructor(ac, orgId, workspaceId)
  2089. {
  2090. this.ac = ac;
  2091. this.uriPath = "/restapi/v2/workspaces/" + workspaceId
  2092. this.bulkUriPath = "/restapi/v2/bulk/workspaces/" + workspaceId
  2093. this.header = {};
  2094. this.header['ZANALYTICS-ORGID'] = orgId;
  2095. }
  2096. /**
  2097. * Create a new table and import the data contained in the mentioned file into the created table.
  2098. * @method importDataInNewTable
  2099. * @param {String} tableName - Name of the new table to be created.
  2100. * @param {String} fileType - Type of the file to be imported.
  2101. * @param {String} autoIdentify - Used to specify whether to auto identify the CSV format. Allowable values - true/false.
  2102. * @param {String} filePath - Path of the file to be imported.
  2103. * @param {Object} config={} - Contains any additional control attributes.
  2104. * @return {JSONObject} Import result.
  2105. * @throws {Error} If the request failed due to some error.
  2106. */
  2107. async importDataInNewTable(tableName, fileType, autoIdentify, filePath, config={})
  2108. {
  2109. var uriPath = this.uriPath + "/data";
  2110. config.tableName = tableName;
  2111. config.fileType = fileType;
  2112. config.autoIdentify = autoIdentify;
  2113. var result = await this.ac.handleImportRequest(uriPath, config, this.header, filePath);
  2114. return result;
  2115. }
  2116. /**
  2117. * Create a new table and import the raw data provided into the created table.
  2118. * @method importRawDataInNewTable
  2119. * @param {String} tableName - Name of the new table to be created.
  2120. * @param {String} fileType - Type of the file to be imported.
  2121. * @param {String} autoIdentify - Used to specify whether to auto identify the CSV format. Allowable values - true/false.
  2122. * @param {String} data - Raw data to be imported.
  2123. * @param {Object} config={} - Contains any additional control attributes.
  2124. * @return {JSONObject} Import result.
  2125. * @throws {Error} If the request failed due to some error.
  2126. */
  2127. async importRawDataInNewTable(tableName, fileType, autoIdentify, data, config={})
  2128. {
  2129. var uriPath = this.uriPath + "/data";
  2130. config.tableName = tableName;
  2131. config.fileType = fileType;
  2132. config.autoIdentify = autoIdentify;
  2133. var result = await this.ac.handleImportRequest(uriPath, config, this.header, null, data);
  2134. return result;
  2135. }
  2136. /**
  2137. * Import the data contained in the mentioned file into the table.
  2138. * @method importData
  2139. * @param {String} viewId - Id of the view where the data to be imported.
  2140. * @param {String} importType - The type of import. Can be one of - append, truncateadd, updateadd.
  2141. * @param {String} fileType - Type of the file to be imported.
  2142. * @param {String} autoIdentify - Used to specify whether to auto identify the CSV format. Allowable values - true/false.
  2143. * @param {String} filePath - Path of the file to be imported.
  2144. * @param {Object} config={} - Contains any additional control attributes.
  2145. * @return {JSONObject} Import result.
  2146. * @throws {Error} If the request failed due to some error.
  2147. */
  2148. async importData(viewId, importType, fileType, autoIdentify, filePath, config={})
  2149. {
  2150. var uriPath = this.uriPath + "/views/" + viewId + "/data";
  2151. config.importType = importType;
  2152. config.fileType = fileType;
  2153. config.autoIdentify = autoIdentify;
  2154. var result = await this.ac.handleImportRequest(uriPath, config, this.header, filePath);
  2155. return result;
  2156. }
  2157. /**
  2158. * Import the raw data provided into the table.
  2159. * @method importRawData
  2160. * @param {String} viewId - Id of the view where the data to be imported.
  2161. * @param {String} importType - The type of import. Can be one of - append, truncateadd, updateadd.
  2162. * @param {String} fileType - Type of the file to be imported.
  2163. * @param {String} autoIdentify - Used to specify whether to auto identify the CSV format. Allowable values - true/false.
  2164. * @param {String} data - Raw data to be imported.
  2165. * @param {Object} config={} - Contains any additional control attributes.
  2166. * @return {JSONObject} Import result.
  2167. * @throws {Error} If the request failed due to some error.
  2168. */
  2169. async importRawData(viewId, importType, fileType, autoIdentify, data, config={})
  2170. {
  2171. var uriPath = this.uriPath + "/views/" + viewId + "/data";
  2172. config.importType = importType;
  2173. config.fileType = fileType;
  2174. config.autoIdentify = autoIdentify;
  2175. var result = await this.ac.handleImportRequest(uriPath, config, this.header, null, data);
  2176. return result;
  2177. }
  2178. /**
  2179. * Asynchronously create a new table and import the data contained in the mentioned file into the created table.
  2180. * @method importBulkDataInNewTable
  2181. * @param {String} tableName - Name of the new table to be created.
  2182. * @param {String} fileType - Type of the file to be imported.
  2183. * @param {String} autoIdentify - Used to specify whether to auto identify the CSV format. Allowable values - true/false.
  2184. * @param {String} filePath - Path of the file to be imported.
  2185. * @param {Object} config={} - Contains any additional control attributes.
  2186. * @return {String} Import job id.
  2187. * @throws {Error} If the request failed due to some error.
  2188. */
  2189. async importBulkDataInNewTable(tableName, fileType, autoIdentify, filePath, config={})
  2190. {
  2191. var uriPath = this.bulkUriPath + "/data";
  2192. config.tableName = tableName;
  2193. config.fileType = fileType;
  2194. config.autoIdentify = autoIdentify;
  2195. var result = await this.ac.handleImportRequest(uriPath, config, this.header, filePath);
  2196. return result.jobId;
  2197. }
  2198. /**
  2199. * Create a new table and import the data contained in the mentioned file into the created table.
  2200. * @method importDataInNewTableAsBatches
  2201. * @param {String} tableName - Name of the new table to be created.
  2202. * @param {String} fileType - Type of the file to be imported.
  2203. * @param {String} autoIdentify - Used to specify whether to auto identify the CSV format. Allowable values - true/false.
  2204. * @param {String} filePath - Path of the file to be imported.
  2205. * @param {Number} batchSize - Number of lines per batch.
  2206. * @param {Object} config={} - Contains any additional control attributes.
  2207. * @return {JSONObject} Import result.
  2208. * @throws {Error} If the request failed due to some error.
  2209. */
  2210. async importDataInNewTableAsBatches(tableName, autoIdentify, filePath, batchSize, config={})
  2211. {
  2212. var uriPath = this.bulkUriPath + "/data/batch";
  2213. config.tableName = tableName;
  2214. config.autoIdentify = autoIdentify;
  2215. var result = await this.ac.handleBatchImportRequest(uriPath, config, this.header, filePath, batchSize);
  2216. return result;
  2217. }
  2218. /**
  2219. * Asynchronously import the data contained in the mentioned file into the table.
  2220. * @method importBulkDataAsBatches
  2221. * @param {String} viewId - Id of the view where the data to be imported.
  2222. * @param {String} importType - The type of import. Can be one of - append, truncateadd, updateadd.
  2223. * @param {String} autoIdentify - Used to specify whether to auto identify the CSV format. Allowable values - true/false.
  2224. * @param {String} filePath - Path of the file to be imported.
  2225. * @param {Number} batchSize - Number of lines per batch.
  2226. * @param {Object} config={} - Contains any additional control attributes.
  2227. * @return {String} Import job id.
  2228. * @throws {Error} If the request failed due to some error.
  2229. */
  2230. async importBulkDataAsBatches(viewId, importType, autoIdentify, filePath, batchSize, config={})
  2231. {
  2232. var uriPath = this.bulkUriPath + "/views/" + viewId + "/data/batch";
  2233. config.importType = importType;
  2234. config.autoIdentify = autoIdentify;
  2235. var result = await this.ac.handleBatchImportRequest(uriPath, config, this.header, filePath, batchSize);
  2236. return result;
  2237. }
  2238. /**
  2239. * Asynchronously import the data contained in the mentioned file into the table.
  2240. * @method importBulkData
  2241. * @param {String} viewId - Id of the view where the data to be imported.
  2242. * @param {String} importType - The type of import. Can be one of - append, truncateadd, updateadd.
  2243. * @param {String} fileType - Type of the file to be imported.
  2244. * @param {String} autoIdentify - Used to specify whether to auto identify the CSV format. Allowable values - true/false.
  2245. * @param {String} filePath - Path of the file to be imported.
  2246. * @param {Object} config={} - Contains any additional control attributes.
  2247. * @return {String} Import job id.
  2248. * @throws {Error} If the request failed due to some error.
  2249. */
  2250. async importBulkData(viewId, importType, fileType, autoIdentify, filePath, config={})
  2251. {
  2252. var uriPath = this.bulkUriPath + "/views/" + viewId + "/data";
  2253. config.importType = importType;
  2254. config.fileType = fileType;
  2255. config.autoIdentify = autoIdentify;
  2256. var result = await this.ac.handleImportRequest(uriPath, config, this.header, filePath);
  2257. return result.jobId;
  2258. }
  2259. /**
  2260. * Returns the details of the import job.
  2261. * @method getImportJobDetails
  2262. * @param {String} jobId - Id of the job.
  2263. * @return {JSONObject} Import job details.
  2264. * @throws {Error} If the request failed due to some error.
  2265. */
  2266. async getImportJobDetails(jobId)
  2267. {
  2268. var uriPath = this.bulkUriPath + "/importjobs/" + jobId;
  2269. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  2270. return result;
  2271. }
  2272. /**
  2273. * Export the mentioned table (or) view data.
  2274. * @method exportData
  2275. * @param {String} viewId - Id of the view to be exported.
  2276. * @param {String} responseFormat - The format in which the data is to be exported.
  2277. * @param {String} filePath - Path of the file where the data exported to be stored.
  2278. * @param {Object} config={} - Contains any additional control attributes.
  2279. * @throws {Error} If the request failed due to some error.
  2280. */
  2281. async exportData(viewId, responseFormat, filePath, config={})
  2282. {
  2283. var uriPath = this.uriPath + "/views/" + viewId + "/data";
  2284. config.responseFormat = responseFormat;
  2285. await this.ac.handleExportRequest(uriPath, filePath, config, this.header);
  2286. }
  2287. /**
  2288. * Initiate asynchronous export for the mentioned table (or) view data.
  2289. * @method initiateBulkExport
  2290. * @param {String} viewId - Id of the view to be exported.
  2291. * @param {String} responseFormat - The format in which the data is to be exported.
  2292. * @param {Object} config={} - Contains any additional control attributes.
  2293. * @return {String} Export job id.
  2294. * @throws {Error} If the request failed due to some error.
  2295. */
  2296. async initiateBulkExport(viewId, responseFormat, config={})
  2297. {
  2298. var uriPath = this.bulkUriPath + "/views/" + viewId + "/data";
  2299. config.responseFormat = responseFormat;
  2300. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  2301. return result.jobId;
  2302. }
  2303. /**
  2304. * Initiate asynchronous export with the given SQL Query.
  2305. * @method initiateBulkExportUsingSQL
  2306. * @param {String} sqlQuery - The SQL Query whose output is exported.
  2307. * @param {String} responseFormat - The format in which the data is to be exported.
  2308. * @param {Object} config={} - Contains any additional control attributes.
  2309. * @return {String} Export job id.
  2310. * @throws {Error} If the request failed due to some error.
  2311. */
  2312. async initiateBulkExportUsingSQL(sqlQuery, responseFormat, config={})
  2313. {
  2314. var uriPath = this.bulkUriPath + "/data";
  2315. config.sqlQuery = sqlQuery;
  2316. config.responseFormat = responseFormat;
  2317. var result = await this.ac.handleV2Request(uriPath, "GET", config, this.header);
  2318. return result.jobId;
  2319. }
  2320. /**
  2321. * Returns the details of the export job.
  2322. * @method getExportJobDetails
  2323. * @param {String} jobId - Id of the export job.
  2324. * @return {JSONObject} Export job details.
  2325. * @throws {Error} If the request failed due to some error.
  2326. */
  2327. async getExportJobDetails(jobId)
  2328. {
  2329. var uriPath = this.bulkUriPath + "/exportjobs/" + jobId;
  2330. var result = await this.ac.handleV2Request(uriPath, "GET", null, this.header);
  2331. return result;
  2332. }
  2333. /**
  2334. * Download the exported data for the mentioned job id.
  2335. * @method exportBulkData
  2336. * @param {String} jobId - Id of the job to be exported.
  2337. * @param {String} filePath - Path of the file where the data exported to be stored.
  2338. * @throws {Error} If the request failed due to some error.
  2339. */
  2340. async exportBulkData(jobId, filePath)
  2341. {
  2342. var uriPath = this.bulkUriPath + "/exportjobs/" + jobId + "/data";
  2343. await this.ac.handleExportRequest(uriPath, filePath, null, this.header);
  2344. }
  2345. }
  2346. module.exports = AnalyticsClient;