Source: messaging/connectors/email/EmailClient.js

/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: Collaboration
 * (C) Copyright IBM Corp. 2019
 *
 * 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/ImageUtils'
], function (Class, ImageUtils) {
	'use strict';

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

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

		/**
		 * Sends email requests to collaboration-service.
		 *
		 * @instance
		 * @param {Object} options
		 * @param {string} options.url - The url for sending the email request.
		 * @param {Object} options.payload - JSON payload.
		 * @param {string} options.imageData - Optonal image data to upload.
		 * @returns {promise} a JSON object
		 */
		sendMessage: function (options) {
			var formData = new FormData();
			formData.append('payload', JSON.stringify(options.payload));

			if (options.imageData) {
				var base64ImageContent = options.imageData.replace(/^data:image\/(png|jpg);base64,/, '');
				var blob = ImageUtils.base64ToBlob(base64ImageContent, 'image/png');
				formData.append('file', blob);
			} else {
				formData.append('file', '');
			}

			return this.doMultipart({
				url: options.url,
				data: formData
			});
		},

		/**
		 * Sends a multipart POST request
		 * @instance
		 * @param {object} options
		 * @param {string} options.url URL for sending requests
		 * @param {object} options.data form data
		 * @returns {promise} a JSON object
		 */
		doMultipart: function (options) {
			var ajaxOptions = {
				type: 'POST',
				url: options.url,
				cache: false,
				contentType: false,
				processData: false,
				data: options.data
			};
			return this._doAjax(ajaxOptions);
		},

		/**
		 * Sends a GET request
		 * @instance
		 * @param {object} options
		 * @param {string} options.url URL for sending requests
		 * @param {object} options.data JSON object of params
		 * @returns {promise} a JSON object
		 */
		doGet: function (options) {
			var ajaxOptions = {
				type: 'GET',
				url: options.url,
				dataType: 'json',
				data: {
					'payload': JSON.stringify(options.data)
				}
			};
			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 given options. It will handle re-auth when access token is invalid.
		 * @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: error handling like in SlackClient...
					throw error;
				}.bind(this));
		}
	});

	return EmailClient;
});