"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: admin * Copyright IBM Corp. 2015, 2016 * US Government Users Restricted Rights - Use, * duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['jquery', 'underscore', 'q', 'bi/commons/ui/core/Class', 'bi/admin/nls/StringResource', 'bi/admin/common/utils/AJAXUtils'], function ($, _, Q, Class, StringResource, AJAXUtils) { 'use strict'; //NOSONAR: meant to be strict var ApiBase = Class.extend({ baseUrl: 'v1/', ajaxService: null, /** * constructor * * @options {object} * @example: {baseUrl: "http://localhost"} */ init: function init(options) { ApiBase.inherited('init', this, arguments); _.extend(this, options); }, /** * Get REST options */ _getRestOptions: function _getRestOptions(method, data) { method = method || 'GET'; var options = { method: method, contentType: 'application/json; charset=utf-8', dataType: 'json' }; if (data) { options.data = JSON.stringify(data); } return options; }, /** * parse a friendly error message from response text */ _parseErrorMsg: function _parseErrorMsg(response) { var result = ''; if (response.responseJSON.messages !== undefined) { result = response.responseJSON.messages.join('\n'); } return result; }, isNameConflictErrorMsg: function isNameConflictErrorMsg(message) { var errMsg = this._parseErrorMsg(message); if (errMsg) { return errMsg.indexOf('CM-REQ-4201') >= 0 || errMsg.indexOf('CM-REQ-4036') >= 0 || errMsg.indexOf('CM-REQ-4024') >= 0; } else { return false; } }, getAjaxService: function getAjaxService() { if (null === this.ajaxService) { this.ajaxService = this.glassContext.services.ajax; } }, /** * ajax function * * @param: path string: RESTFul api path * @param: options {object}, contain parameters that * ajax needs * @example: ajax("http://localhost", * this._getRestOptions()); */ _ajax: function _ajax(path, options) { this.getAjaxService(); options.url = (this.baseUrl || '') + path; var ajaxPromise = this.ajaxService.ajax($.extend({ 'headers': { 'Accept': 'application/json', 'Content-Type': 'application/json' }, 'dataType': 'json' }, options)); return ajaxPromise; }, getCredential: function getCredential() { var options = { url: 'v1/users/~/credentials', type: 'GET', dataType: 'json' }; return this.glassContext.getCoreSvc('.Ajax').ajax(options); }, renewCredential: function renewCredential() { return this.glassContext.getCoreSvc('.Login').renewCredential(); }, updateCMObject: function updateCMObject(objectInformation, updatedData, stringKeysObj, url) { if (!url) { updatedData.type = objectInformation.type; } if (!stringKeysObj) { stringKeysObj = { success: 'objectUpdateSuccess', fail: 'objectUpdateFail' }; } var updateUrl = url ? url : 'v1/objects/'; var options = { dataType: 'json', type: 'PUT', contentType: 'application/json; charset=utf-8', data: JSON.stringify(updatedData), url: updateUrl + objectInformation.id }; return this.glassContext.getCoreSvc('.Ajax').ajax(options).then(function () { this.glassContext.appController.showToast(StringResource.get(stringKeysObj.success, { 'num': objectInformation.defaultName })); }.bind(this), function (error) { this.glassContext.appController.showErrorMessage(StringResource.get(stringKeysObj.fail) + ', ' + AJAXUtils.buildErrorMessage(error.jqXHR.responseJSON.errors)); }.bind(this)); }, updateCOSConnection: function updateCOSConnection(objectInformation, updatedData) { var stringKeys = { success: 'cloudConnectionUpdateSuccessful', fail: 'cloudConnectionUpdateFail' }; return this.updateCMObject(objectInformation, updatedData, stringKeys, 'v1/cos/'); }, updateCOSLocation: function updateCOSLocation(objectInformation, updatedData) { var stringKeys = { success: 'cloudLocationUpdateSuccessful', fail: 'cloudLocationUpdateFail' }; return this.updateCMObject(objectInformation, updatedData, stringKeys, 'v1/coslocations/'); }, updateSISubscription: function updateSISubscription(objectInformation, updatedData) { var stringKeys = { success: 'siSubscriptionUpdateSuccessful', fail: 'siSubscriptionUpdateFail' }; return this.updateCMObject(objectInformation, updatedData, stringKeys, 'v1/social_insights/subscriptions/'); }, updateResourceFolder: function updateResourceFolder(objectInformation, updatedData) { var stringKeys = { success: 'resourceFolderUpdateSuccessful', fail: 'resourceFolderUpdateFailure' }; return this.updateCMObject(objectInformation, updatedData, stringKeys); } }); return ApiBase; });