EmbeddedSocial-NodeJS-API-L.../operations/requestTokens.js

176 строки
6.3 KiB
JavaScript

/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under
* the MIT License. See LICENSE in the project root for license information.
* <autogenerated> This file was generated using AutoRest. </autogenerated>
*/
'use strict';
var util = require('util');
var msRest = require('ms-rest');
var WebResource = msRest.WebResource;
/**
* @class
* RequestTokens
* __NOTE__: An instance of this class is automatically created for an
* instance of the SocialPlusClient.
* Initializes a new instance of the RequestTokens class.
* @constructor
*
* @param {SocialPlusClient} client Reference to the service client.
*/
function RequestTokens(client) {
this.client = client;
}
/**
* @summary Get request token
*
* @param {string} identityProvider Identity provider type. Possible values
* include: 'Facebook', 'Microsoft', 'Google', 'Twitter', 'AADS2S',
* 'SocialPlus'
*
* @param {string} authorization Format is: "Scheme CredentialsList". Possible
* values are:
*
* - Anon AK=AppKey
*
* - SocialPlus TK=SessionToken
*
* - Facebook AK=AppKey|TK=AccessToken
*
* - Google AK=AppKey|TK=AccessToken
*
* - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken
*
* - Microsoft AK=AppKey|TK=AccessToken
*
* - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken
*
* @param {object} [options] Optional Parameters.
*
* @param {object} [options.customHeaders] Headers that will be added to the
* request
*
* @param {function} callback
*
* @returns {function} callback(err, result, request, response)
*
* {Error} err - The Error object if an error occurred, null otherwise.
*
* {object} [result] - The deserialized result object.
* See {@link GetRequestTokenResponse} for more
* information.
*
* {object} [request] - The HTTP Request object if an error did not occur.
*
* {stream} [response] - The HTTP Response stream if an error did not occur.
*/
RequestTokens.prototype.getRequestToken = function (identityProvider, authorization, options, callback) {
var client = this.client;
if(!callback && typeof options === 'function') {
callback = options;
options = null;
}
if (!callback) {
throw new Error('callback cannot be null.');
}
// Validate
try {
if (identityProvider) {
var allowedValues = [ 'Facebook', 'Microsoft', 'Google', 'Twitter', 'AADS2S', 'SocialPlus' ];
if (!allowedValues.some( function(item) { return item === identityProvider; })) {
throw new Error(identityProvider + ' is not a valid value. The valid values are: ' + allowedValues);
}
} else {
throw new Error('identityProvider cannot be null or undefined.');
}
if (authorization === null || authorization === undefined || typeof authorization.valueOf() !== 'string') {
throw new Error('authorization cannot be null or undefined and it must be of type string.');
}
} catch (error) {
return callback(error);
}
// Construct URL
var requestUrl = this.client.baseUri +
'//v0.7/request_tokens/{identityProvider}';
requestUrl = requestUrl.replace('{identityProvider}', encodeURIComponent(identityProvider));
// trim all duplicate forward slashes in the url
var regex = /([^:]\/)\/+/gi;
requestUrl = requestUrl.replace(regex, '$1');
// Create HTTP transport objects
var httpRequest = new WebResource();
httpRequest.method = 'GET';
httpRequest.headers = {};
httpRequest.url = requestUrl;
// Set Headers
if (authorization !== undefined && authorization !== null) {
httpRequest.headers['Authorization'] = authorization;
}
if(options) {
for(var headerName in options['customHeaders']) {
if (options['customHeaders'].hasOwnProperty(headerName)) {
httpRequest.headers[headerName] = options['customHeaders'][headerName];
}
}
}
httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
httpRequest.body = null;
// Send Request
return client.pipeline(httpRequest, function (err, response, responseBody) {
if (err) {
return callback(err);
}
var statusCode = response.statusCode;
if (statusCode !== 200 && statusCode !== 400 && statusCode !== 401 && statusCode !== 500) {
var error = new Error(responseBody);
error.statusCode = response.statusCode;
error.request = msRest.stripRequest(httpRequest);
error.response = msRest.stripResponse(response);
if (responseBody === '') responseBody = null;
var parsedErrorResponse;
try {
parsedErrorResponse = JSON.parse(responseBody);
if (parsedErrorResponse) {
if (parsedErrorResponse.error) parsedErrorResponse = parsedErrorResponse.error;
if (parsedErrorResponse.code) error.code = parsedErrorResponse.code;
if (parsedErrorResponse.message) error.message = parsedErrorResponse.message;
}
} catch (defaultError) {
error.message = util.format('Error "%s" occurred in deserializing the responseBody ' +
'- "%s" for the default response.', defaultError.message, responseBody);
return callback(error);
}
return callback(error);
}
// Create Result
var result = null;
if (responseBody === '') responseBody = null;
// Deserialize Response
if (statusCode === 200) {
var parsedResponse = null;
try {
parsedResponse = JSON.parse(responseBody);
result = JSON.parse(responseBody);
if (parsedResponse !== null && parsedResponse !== undefined) {
var resultMapper = new client.models['GetRequestTokenResponse']().mapper();
result = client.deserialize(resultMapper, parsedResponse, 'result');
}
} catch (error) {
var deserializationError = new Error(util.format('Error "%s" occurred in deserializing the responseBody - "%s"', error, responseBody));
deserializationError.request = msRest.stripRequest(httpRequest);
deserializationError.response = msRest.stripResponse(response);
return callback(deserializationError);
}
}
return callback(null, result, httpRequest, response);
});
};
module.exports = RequestTokens;