Source: messaging/connectors/msteams/MSTeamsClient.js

/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: Collaboration
 * (C) Copyright IBM Corp. 2018
 *
 * US Government Users Restricted Rights - Use, duplication or disclosure
 * restricted by GSA ADP Schedule Contract with IBM Corp.
 */
define([
	'../../../lib/@waca/core-client/js/core-client/ui/core/Class',
	'../../utils/CustomStatus',
	'../../../nls/StringResources',
	'./MSTeamsAuth'
], function (Class, CustomStatus, StringMessages, MSTeamsAuth) {
	'use strict';

	var MSTeamsClient = Class.extend( /** @lends MSTeamsClient */ {

		/**
		 * @desc A common object that calls backend REST APIs to handle MS Teams operations.
		 *
		 * @constructs MSTeamsClient
		 * @extends Class
		 * @public
		 * @param {object} glassContext - the Glass context
		 * @returns an object
		 *
		 * @example var msTeamsClient = new MSTeamsClient(glassContext);
		 */
		init: function (glassContext) {
			MSTeamsClient.inherited('init', this, arguments);
			this.glassContext = glassContext;
			this.msTeamsAuth = new MSTeamsAuth(glassContext);
		},

		/**
		 * Signs in to MS Teams.
		 * @instance
		 * @returns {promise} a JSON object
		 */
		signIn: function () {
			return this.msTeamsAuth.authWithTeams();
		},

		/**
		 * Signs out of MS Teams.
		 * @instance
		 * @param {string} uri the URI for signing out
		 * @returns {promise} a JSON object
		 */
		signOut: function (uri) {
			void(uri);
			return Promise.reject(new Error(StringMessages.get('error_not_implemented')));
		},

		/**
		 * Sends a message to MS Teams.
		 * @instance
		 * @param {object} options
		 * @param {string} options.url the url for sending messages
		 * @param {object} options.data a JSON object represents a message to send
		 * @returns {promise} a JSON object
		 */
		sendMessage: function (options) {
			return this.doPost(options);
		},

		/**
		 * Sends a GET request.
		 * @instance
		 * @param {object} options
		 * @param {string} options.url Request URL
		 * @param {object} options.data Optional request payload
		 * @returns {promise} a JSON object
		 */
		doGet: function (options) {
			var ajaxOptions = {
				type: 'GET',
				url: options.url,
				contentType: 'application/json; charset=utf-8',
				dataType: 'json',
				data: options.data ? {
					'payload': JSON.stringify(options.data)
				} : null
			};
			return this._doAjax(ajaxOptions);
		},

		/**
		 * Sends a POST request.
		 * @instance
		 * @param {object} options
		 * @param {string} options.url URL for sending requests
		 * @param {object} options.data JSON object of POST body
		 * @returns {promise} a JSON object
		 */
		doPost: function (options) {
			var ajaxOptions = {
				type: 'POST',
				url: options.url,
				data: JSON.stringify(options.data),
				contentType: 'application/json; charset=utf-8',
				dataType: 'json'
			};
			return this._doAjax(ajaxOptions);
		},

		/**
		 * Sends an HTTP request with the given options.
		 * @instance
		 * @private
		 * @param {object} options AJAX options
		 * @returns {promise} a JSON response
		 */
		_doAjax: function (options) {
			return this.glassContext.getCoreSvc('.Ajax')
				.ajax(options)
				.catch(function (error) {
					// TODO: handle re-authorization and other special cases.
					throw error;
				}.bind(this));
		}
	});

	return MSTeamsClient;
});