ApiBase.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: admin
  5. * Copyright IBM Corp. 2015, 2016
  6. * US Government Users Restricted Rights - Use,
  7. * duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['jquery', 'underscore', 'q', 'bi/commons/ui/core/Class', 'bi/admin/nls/StringResource', 'bi/admin/common/utils/AJAXUtils'], function ($, _, Q, Class, StringResource, AJAXUtils) {
  10. 'use strict'; //NOSONAR: meant to be strict
  11. var ApiBase = Class.extend({
  12. baseUrl: 'v1/',
  13. ajaxService: null,
  14. /**
  15. * constructor
  16. *
  17. * @options {object}
  18. * @example: {baseUrl: "http://localhost"}
  19. */
  20. init: function init(options) {
  21. ApiBase.inherited('init', this, arguments);
  22. _.extend(this, options);
  23. },
  24. /**
  25. * Get REST options
  26. */
  27. _getRestOptions: function _getRestOptions(method, data) {
  28. method = method || 'GET';
  29. var options = {
  30. method: method,
  31. contentType: 'application/json; charset=utf-8',
  32. dataType: 'json'
  33. };
  34. if (data) {
  35. options.data = JSON.stringify(data);
  36. }
  37. return options;
  38. },
  39. /**
  40. * parse a friendly error message from response text
  41. */
  42. _parseErrorMsg: function _parseErrorMsg(response) {
  43. var result = '';
  44. if (response.responseJSON.messages !== undefined) {
  45. result = response.responseJSON.messages.join('\n');
  46. }
  47. return result;
  48. },
  49. isNameConflictErrorMsg: function isNameConflictErrorMsg(message) {
  50. var errMsg = this._parseErrorMsg(message);
  51. if (errMsg) {
  52. return errMsg.indexOf('CM-REQ-4201') >= 0 || errMsg.indexOf('CM-REQ-4036') >= 0 || errMsg.indexOf('CM-REQ-4024') >= 0;
  53. } else {
  54. return false;
  55. }
  56. },
  57. getAjaxService: function getAjaxService() {
  58. if (null === this.ajaxService) {
  59. this.ajaxService = this.glassContext.services.ajax;
  60. }
  61. },
  62. /**
  63. * ajax function
  64. *
  65. * @param: path string: RESTFul api path
  66. * @param: options {object}, contain parameters that
  67. * ajax needs
  68. * @example: ajax("http://localhost",
  69. * this._getRestOptions());
  70. */
  71. _ajax: function _ajax(path, options) {
  72. this.getAjaxService();
  73. options.url = (this.baseUrl || '') + path;
  74. var ajaxPromise = this.ajaxService.ajax($.extend({
  75. 'headers': {
  76. 'Accept': 'application/json',
  77. 'Content-Type': 'application/json'
  78. },
  79. 'dataType': 'json'
  80. }, options));
  81. return ajaxPromise;
  82. },
  83. getCredential: function getCredential() {
  84. var options = {
  85. url: 'v1/users/~/credentials',
  86. type: 'GET',
  87. dataType: 'json'
  88. };
  89. return this.glassContext.getCoreSvc('.Ajax').ajax(options);
  90. },
  91. renewCredential: function renewCredential() {
  92. return this.glassContext.getCoreSvc('.Login').renewCredential();
  93. },
  94. updateCMObject: function updateCMObject(objectInformation, updatedData, stringKeysObj, url) {
  95. if (!url) {
  96. updatedData.type = objectInformation.type;
  97. }
  98. if (!stringKeysObj) {
  99. stringKeysObj = {
  100. success: 'objectUpdateSuccess',
  101. fail: 'objectUpdateFail'
  102. };
  103. }
  104. var updateUrl = url ? url : 'v1/objects/';
  105. var options = {
  106. dataType: 'json',
  107. type: 'PUT',
  108. contentType: 'application/json; charset=utf-8',
  109. data: JSON.stringify(updatedData),
  110. url: updateUrl + objectInformation.id
  111. };
  112. return this.glassContext.getCoreSvc('.Ajax').ajax(options).then(function () {
  113. this.glassContext.appController.showToast(StringResource.get(stringKeysObj.success, {
  114. 'num': objectInformation.defaultName
  115. }));
  116. }.bind(this), function (error) {
  117. this.glassContext.appController.showErrorMessage(StringResource.get(stringKeysObj.fail) + ', ' + AJAXUtils.buildErrorMessage(error.jqXHR.responseJSON.errors));
  118. }.bind(this));
  119. },
  120. updateCOSConnection: function updateCOSConnection(objectInformation, updatedData) {
  121. var stringKeys = {
  122. success: 'cloudConnectionUpdateSuccessful',
  123. fail: 'cloudConnectionUpdateFail'
  124. };
  125. return this.updateCMObject(objectInformation, updatedData, stringKeys, 'v1/cos/');
  126. },
  127. updateCOSLocation: function updateCOSLocation(objectInformation, updatedData) {
  128. var stringKeys = {
  129. success: 'cloudLocationUpdateSuccessful',
  130. fail: 'cloudLocationUpdateFail'
  131. };
  132. return this.updateCMObject(objectInformation, updatedData, stringKeys, 'v1/coslocations/');
  133. },
  134. updateSISubscription: function updateSISubscription(objectInformation, updatedData) {
  135. var stringKeys = {
  136. success: 'siSubscriptionUpdateSuccessful',
  137. fail: 'siSubscriptionUpdateFail'
  138. };
  139. return this.updateCMObject(objectInformation, updatedData, stringKeys, 'v1/social_insights/subscriptions/');
  140. },
  141. updateResourceFolder: function updateResourceFolder(objectInformation, updatedData) {
  142. var stringKeys = {
  143. success: 'resourceFolderUpdateSuccessful',
  144. fail: 'resourceFolderUpdateFailure'
  145. };
  146. return this.updateCMObject(objectInformation, updatedData, stringKeys);
  147. }
  148. });
  149. return ApiBase;
  150. });