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

389 строки
14 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
* Search
* __NOTE__: An instance of this class is automatically created for an
* instance of the SocialPlusClient.
* Initializes a new instance of the Search class.
* @constructor
*
* @param {SocialPlusClient} client Reference to the service client.
*/
function Search(client) {
this.client = client;
}
/**
* @summary Search topics with a query
*
* The query string will be searched across hashtags, topic titles, and topic
* texts,
* and matching results will be returned.
*
* If the query string contains only hashtags, e.g. #foo #bar,
* then only the hashtags
* in topics will be searched.
*
* Query string supports the following operators:
* - suffix: "foo*"
* - and: "foo+bar"
* - or: "foo|bar"
* - not: "-foo"
* - phrase: ""foo bar""
* - precedence: "foo+(bar|baz)"
* You need to escape * if it is at the end of a word, and - if
* it is at the start of a word.
* Default behavior is to use and, so if you use whitespace to
* separate words,
* such as "foo bar", that is equivalent to "foo+bar".
*
* @param {string} query Search query
*
* @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 {number} [options.cursor] Current read cursor
*
* @param {number} [options.limit] Number of items to return
*
* @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 FeedResponseTopicView} 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.
*/
Search.prototype.getTopics = function (query, 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.');
}
var cursor = (options && options.cursor !== undefined) ? options.cursor : undefined;
var limit = (options && options.limit !== undefined) ? options.limit : undefined;
// Validate
try {
if (query === null || query === undefined || typeof query.valueOf() !== 'string') {
throw new Error('query cannot be null or undefined and it must be of type string.');
}
if (cursor !== null && cursor !== undefined && typeof cursor !== 'number') {
throw new Error('cursor must be of type number.');
}
if (limit !== null && limit !== undefined && typeof limit !== 'number') {
throw new Error('limit must be of type number.');
}
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/search/topics';
var queryParameters = [];
queryParameters.push('query=' + encodeURIComponent(query));
if (cursor !== null && cursor !== undefined) {
queryParameters.push('cursor=' + encodeURIComponent(cursor.toString()));
}
if (limit !== null && limit !== undefined) {
queryParameters.push('limit=' + encodeURIComponent(limit.toString()));
}
if (queryParameters.length > 0) {
requestUrl += '?' + queryParameters.join('&');
}
// 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['FeedResponseTopicView']().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);
});
};
/**
* @summary Search users with a query
*
* The query string will be searched across the full name of users
* and matching results will be returned.
*
* Query string supports the following operators:
* - suffix: "foo*"
* - and: "foo+bar"
* - or: "foo|bar"
* - not: "-foo"
* - phrase: ""foo bar""
* - precedence: "foo+(bar|baz)"
* You need to escape * if it is at the end of a word, and - if
* it is at the start of a word.
* Default behavior is to use and, so if you use whitespace to
* separate words,
* such as "foo bar", that is equivalent to "foo+bar".
*
* @param {string} query Search query
*
* @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 {number} [options.cursor] Current read cursor
*
* @param {number} [options.limit] Number of items to return
*
* @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 FeedResponseUserCompactView} 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.
*/
Search.prototype.getUsers = function (query, 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.');
}
var cursor = (options && options.cursor !== undefined) ? options.cursor : undefined;
var limit = (options && options.limit !== undefined) ? options.limit : undefined;
// Validate
try {
if (query === null || query === undefined || typeof query.valueOf() !== 'string') {
throw new Error('query cannot be null or undefined and it must be of type string.');
}
if (cursor !== null && cursor !== undefined && typeof cursor !== 'number') {
throw new Error('cursor must be of type number.');
}
if (limit !== null && limit !== undefined && typeof limit !== 'number') {
throw new Error('limit must be of type number.');
}
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/search/users';
var queryParameters = [];
queryParameters.push('query=' + encodeURIComponent(query));
if (cursor !== null && cursor !== undefined) {
queryParameters.push('cursor=' + encodeURIComponent(cursor.toString()));
}
if (limit !== null && limit !== undefined) {
queryParameters.push('limit=' + encodeURIComponent(limit.toString()));
}
if (queryParameters.length > 0) {
requestUrl += '?' + queryParameters.join('&');
}
// 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['FeedResponseUserCompactView']().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 = Search;