ApiSvc.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: admin
  5. * Copyright IBM Corp. 2015, 2017
  6. * US Government Users Restricted Rights -
  7. * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['underscore', 'bi/admin/common/services/ApiBase'], function (_, ApiBase) {
  10. 'use strict'; //NOSONAR: meant to be strict
  11. var _singletonInstance = null;
  12. var ApiService = ApiBase.extend({
  13. filterTypes: [],
  14. /**
  15. * constructor
  16. * @options {object}
  17. * @example: {baseUrl: "http://localhost"}
  18. */
  19. init: function init(options) {
  20. ApiService.inherited('init', this, arguments);
  21. this.dataSources = this._getDsApiSet();
  22. this.dataConnections = this._getDcApiSet();
  23. this.signOns = this._getSignonApiSet();
  24. this.tables = this._getTableApiSet();
  25. this.schemas = this._getSchemaApiSet();
  26. this.tasks = this._getTaskApiSet();
  27. this.schemaSources = this._getSchemaSourceApiSet();
  28. this.history = this._getHistoryApiSet();
  29. },
  30. _getDsApiSet: function _getDsApiSet() {
  31. var self = this;
  32. var objType = 'datasources';
  33. var patchPath = function patchPath(path) {
  34. if (path.indexOf('?') > -1) {
  35. path = path + '&intType=light';
  36. } else {
  37. path = path + '?intType=light';
  38. }
  39. return path;
  40. };
  41. return {
  42. read: function read(id) {
  43. return self._ajax(patchPath(objType + "/" + id), self._getRestOptions());
  44. },
  45. update: function update(id, data) {
  46. return self._ajax(patchPath(objType + "/" + id), self._getRestOptions('PUT', data));
  47. },
  48. create: function create(data) {
  49. return self._ajax(patchPath(objType + "/"), self._getRestOptions('POST', data));
  50. },
  51. listDefinitions: function listDefinitions(cat) {
  52. return self._ajax(patchPath('definitions?cat=' + cat), self._getRestOptions());
  53. }
  54. };
  55. },
  56. _createResource: function _createResource(resTemp) {
  57. var self = this;
  58. var getResPath = function getResPath(resTemp, params) {
  59. var resPath = resTemp;
  60. params.push('');
  61. params.push('');
  62. params.push('');
  63. _.each(params, function (p, index) {
  64. var t = '[id' + index + ']';
  65. resPath = resPath.replace(t, p);
  66. });
  67. return resPath;
  68. };
  69. return {
  70. template: resTemp,
  71. request: function request(params, options, xCaAffinity) {
  72. var resPath = getResPath(this.template, params);
  73. if (xCaAffinity) {
  74. options.headers = {
  75. Accept: 'application/json',
  76. "x-ca-affinity": xCaAffinity
  77. };
  78. }
  79. return self._ajax(resPath, options);
  80. }
  81. };
  82. },
  83. _getDcApiSet: function _getDcApiSet() {
  84. var self = this;
  85. var res = this._createResource("datasources/[id0]/connections/[id1]");
  86. return {
  87. list: function list(dsId) {
  88. return res.request([dsId], self._getRestOptions());
  89. },
  90. read: function read(dsId, dcId) {
  91. return res.request([dsId, dcId], self._getRestOptions());
  92. },
  93. update: function update(dsId, dcId, data) {
  94. return res.request([dsId, dcId], self._getRestOptions('PUT', data));
  95. },
  96. create: function create(dsId, data) {
  97. return res.request([dsId], self._getRestOptions('POST', data));
  98. },
  99. remove: function remove(dsId, dcId) {
  100. return res.request([dsId, dcId], self._getRestOptions('DELETE'));
  101. },
  102. testConnection: function testConnection(data) {
  103. return self._ajax('datasourceconnection/test', self._getRestOptions('POST', data));
  104. },
  105. testConnectionWithCredential: function testConnectionWithCredential(data) {
  106. return self._ajax('datasourceconnection/test_with_credential', self._getRestOptions('POST', data));
  107. }
  108. };
  109. },
  110. _getSchemaApiSet: function _getSchemaApiSet() {
  111. var self = this;
  112. var res = this._createResource("metadata/schemas/[id0]");
  113. return {
  114. 'read': function read(schemaId) {
  115. return res.request([schemaId], self._getRestOptions());
  116. },
  117. 'update': function update(schemaId, data) {
  118. return res.request([schemaId], self._getRestOptions('PUT', data));
  119. },
  120. 'clear': function clear(schemaId) {
  121. return res.request([schemaId], self._getRestOptions('DELETE'));
  122. }
  123. };
  124. },
  125. _getSchemaSourceApiSet: function _getSchemaSourceApiSet() {
  126. var self = this;
  127. var res = this._createResource("metadata/sources/[id0]/schemas");
  128. return {
  129. 'create': function create(dsId, data) {
  130. return res.request([dsId], $.extend(self._getRestOptions('POST', data), {
  131. 'dataType': 'text'
  132. }));
  133. }
  134. };
  135. },
  136. _getTableApiSet: function _getTableApiSet(schemaId) {
  137. var self = this;
  138. var res = this._createResource("metadata/schemas/[id0]/tables");
  139. return {
  140. 'list': function list(schemaId) {
  141. return res.request([schemaId], self._getRestOptions());
  142. }
  143. };
  144. },
  145. _getTaskApiSet: function _getTaskApiSet() {
  146. var self = this;
  147. var res = this._createResource('metadata/tasks/[id0]');
  148. return {
  149. 'cancel': function cancel(taskId, xCaAffinity) {
  150. return res.request([taskId], self._getRestOptions('DELETE'), xCaAffinity);
  151. }
  152. };
  153. },
  154. /*
  155. * This method fetches the history of the schema with schemaID.
  156. * @param {String} The schemaID of the selected schema
  157. * @return {Object} An object containing the requested api set
  158. */
  159. _getHistoryApiSet: function _getHistoryApiSet(schemaId) {
  160. var self = this,
  161. res = this._createResource("metadata/schemas/[id0]/history");
  162. return {
  163. list: function list(schemaId) {
  164. return res.request([schemaId], self._getRestOptions());
  165. }
  166. };
  167. },
  168. _getSignonApiSet: function _getSignonApiSet() {
  169. var self = this;
  170. var res = this._createResource("datasources/[id0]/connections/[id1]/signons/[id2]");
  171. return {
  172. list: function list(dsId, dcId) {
  173. return res.request([dsId, dcId], self._getRestOptions());
  174. },
  175. read: function read(dsId, dcId, signOnId) {
  176. return res.request([dsId, dcId, signOnId], self._getRestOptions());
  177. },
  178. update: function update(dsId, dcId, signOnId, data) {
  179. return res.request([dsId, dcId, signOnId], self._getRestOptions('PUT', data));
  180. },
  181. create: function create(dsId, dcId, data) {
  182. return res.request([dsId, dcId], self._getRestOptions('POST', data));
  183. },
  184. remove: function remove(dsId, dcId, signOnId) {
  185. return res.request([dsId, dcId, signOnId], self._getRestOptions('DELETE'));
  186. }
  187. };
  188. }
  189. });
  190. var _static = {
  191. getInstance: function getInstance() {
  192. if (!_singletonInstance) {
  193. _singletonInstance = new ApiService();
  194. }
  195. return _singletonInstance;
  196. }
  197. };
  198. return _static.getInstance();
  199. });