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

176 строки
6.3 KiB
JavaScript
Исходник Постоянная ссылка Обычный вид История

2016-02-28 05:08:05 +03:00
/*
2016-03-15 03:57:25 +03:00
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under
* the MIT License. See LICENSE in the project root for license information.
2016-06-11 03:50:51 +03:00
* <autogenerated> This file was generated using AutoRest. </autogenerated>
2016-02-28 05:08:05 +03:00
*/
'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
2016-09-12 20:20:50 +03:00
* include: 'Facebook', 'Microsoft', 'Google', 'Twitter', 'AADS2S',
* 'SocialPlus'
2016-02-28 05:08:05 +03:00
*
2016-09-12 20:20:50 +03:00
* @param {string} authorization Format is: "Scheme CredentialsList". Possible
* values are:
*
* - Anon AK=AppKey
*
* - SocialPlus TK=SessionToken
2016-02-28 05:08:05 +03:00
*
2016-09-12 20:20:50 +03:00
* - Facebook AK=AppKey|TK=AccessToken
2016-02-28 05:08:05 +03:00
*
2016-09-12 20:20:50 +03:00
* - Google AK=AppKey|TK=AccessToken
*
2016-09-12 20:20:50 +03:00
* - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken
*
2016-09-12 20:20:50 +03:00
* - Microsoft AK=AppKey|TK=AccessToken
*
2016-09-12 20:20:50 +03:00
* - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken
*
* @param {object} [options] Optional Parameters.
2016-02-28 05:08:05 +03:00
*
* @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.
*/
2016-09-12 20:20:50 +03:00
RequestTokens.prototype.getRequestToken = function (identityProvider, authorization, options, callback) {
2016-02-28 05:08:05 +03:00
var client = this.client;
if(!callback && typeof options === 'function') {
callback = options;
options = null;
}
if (!callback) {
throw new Error('callback cannot be null.');
}
// Validate
try {
2016-03-15 03:57:25 +03:00
if (identityProvider) {
2016-09-12 20:20:50 +03:00
var allowedValues = [ 'Facebook', 'Microsoft', 'Google', 'Twitter', 'AADS2S', 'SocialPlus' ];
2016-03-15 03:57:25 +03:00
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.');
2016-02-28 05:08:05 +03:00
}
2016-09-12 20:20:50 +03:00
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.');
}
2016-02-28 05:08:05 +03:00
} catch (error) {
return callback(error);
}
// Construct URL
var requestUrl = this.client.baseUri +
2017-09-30 01:06:37 +03:00
'//v0.7/request_tokens/{identityProvider}';
2016-02-28 05:08:05 +03:00
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;