diff --git a/lib/services/loganalytics/LICENSE.txt b/lib/services/loganalytics/LICENSE.txt index a70e8cf660..5431ba98b9 100644 --- a/lib/services/loganalytics/LICENSE.txt +++ b/lib/services/loganalytics/LICENSE.txt @@ -1,21 +1,21 @@ -The MIT License (MIT) - -Copyright (c) 2018 Microsoft - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +The MIT License (MIT) + +Copyright (c) 2018 Microsoft + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/services/loganalytics/lib/logAnalyticsClient.d.ts b/lib/services/loganalytics/lib/logAnalyticsClient.d.ts index c2f3bbbdca..5e00e547cc 100644 --- a/lib/services/loganalytics/lib/logAnalyticsClient.d.ts +++ b/lib/services/loganalytics/lib/logAnalyticsClient.d.ts @@ -8,8 +8,9 @@ * regenerated. */ -import { ServiceClient, ServiceClientOptions, ServiceCallback, HttpOperationResponse, ServiceClientCredentials } from 'ms-rest'; +import { ServiceClient, ServiceClientOptions, ServiceClientCredentials } from 'ms-rest'; import * as models from "./models"; +import * as operations from "./operations"; export default class LogAnalyticsClient extends ServiceClient { /** @@ -35,96 +36,8 @@ export default class LogAnalyticsClient extends ServiceClient { credentials: ServiceClientCredentials; - - /** - * @summary Execute an Analytics query - * - * Executes an Analytics query for data. - * [Here](https://dev.loganalytics.io/documentation/Using-the-API) is an - * example for using POST with an Analytics query. - * - * @param {string} workspaceId ID of the workspace. This is Workspace ID from - * the Properties blade in the Azure portal. - * - * @param {object} body The Analytics query. Learn more about the [Analytics - * query - * syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/) - * - * @param {string} body.query The query to execute. - * - * @param {string} [body.timespan] Optional. The timespan over which to query - * data. This is an ISO8601 time period value. This timespan is applied in - * addition to any that are specified in the query expression. - * - * @param {array} [body.workspaces] A list of workspaces that are included in - * the query. - * - * @param {object} [options] Optional Parameters. - * - * @param {object} [options.customHeaders] Headers that will be added to the - * request - * - * @returns {Promise} A promise is returned - * - * @resolve {HttpOperationResponse} - The deserialized result object. - * - * @reject {Error|ServiceError} - The error object. - */ - queryWithHttpOperationResponse(workspaceId: string, body: models.QueryBody, options?: { customHeaders? : { [headerName: string]: string; } }): Promise>; - - /** - * @summary Execute an Analytics query - * - * Executes an Analytics query for data. - * [Here](https://dev.loganalytics.io/documentation/Using-the-API) is an - * example for using POST with an Analytics query. - * - * @param {string} workspaceId ID of the workspace. This is Workspace ID from - * the Properties blade in the Azure portal. - * - * @param {object} body The Analytics query. Learn more about the [Analytics - * query - * syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/) - * - * @param {string} body.query The query to execute. - * - * @param {string} [body.timespan] Optional. The timespan over which to query - * data. This is an ISO8601 time period value. This timespan is applied in - * addition to any that are specified in the query expression. - * - * @param {array} [body.workspaces] A list of workspaces that are included in - * the query. - * - * @param {object} [options] Optional Parameters. - * - * @param {object} [options.customHeaders] Headers that will be added to the - * request - * - * @param {ServiceCallback} [optionalCallback] - The optional callback. - * - * @returns {ServiceCallback|Promise} If a callback was passed as the last - * parameter then it returns the callback else returns a Promise. - * - * {Promise} A promise is returned. - * - * @resolve {QueryResults} - The deserialized result object. - * - * @reject {Error|ServiceError} - The error object. - * - * {ServiceCallback} optionalCallback(err, result, request, response) - * - * {Error|ServiceError} err - The Error object if an error occurred, null otherwise. - * - * {QueryResults} [result] - The deserialized result object if an error did not occur. - * See {@link QueryResults} for more information. - * - * {WebResource} [request] - The HTTP Request object if an error did not occur. - * - * {http.IncomingMessage} [response] - The HTTP Response stream if an error did not occur. - */ - query(workspaceId: string, body: models.QueryBody, options?: { customHeaders? : { [headerName: string]: string; } }): Promise; - query(workspaceId: string, body: models.QueryBody, callback: ServiceCallback): void; - query(workspaceId: string, body: models.QueryBody, options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback): void; + // Operation groups + query: operations.Query; } export { LogAnalyticsClient, models as LogAnalyticsModels }; diff --git a/lib/services/loganalytics/lib/logAnalyticsClient.js b/lib/services/loganalytics/lib/logAnalyticsClient.js index 14336cacbe..77d5c72044 100644 --- a/lib/services/loganalytics/lib/logAnalyticsClient.js +++ b/lib/services/loganalytics/lib/logAnalyticsClient.js @@ -16,165 +16,11 @@ const msRest = require('ms-rest'); const ServiceClient = msRest.ServiceClient; -const WebResource = msRest.WebResource; const models = require('./models'); +const operations = require('./operations'); -/** - * @summary Execute an Analytics query - * - * Executes an Analytics query for data. - * [Here](https://dev.loganalytics.io/documentation/Using-the-API) is an - * example for using POST with an Analytics query. - * - * @param {string} workspaceId ID of the workspace. This is Workspace ID from - * the Properties blade in the Azure portal. - * - * @param {object} body The Analytics query. Learn more about the [Analytics - * query - * syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/) - * - * @param {string} body.query The query to execute. - * - * @param {string} [body.timespan] Optional. The timespan over which to query - * data. This is an ISO8601 time period value. This timespan is applied in - * addition to any that are specified in the query expression. - * - * @param {array} [body.workspaces] A list of workspaces that are included in - * the query. - * - * @param {object} [options] Optional Parameters. - * - * @param {object} [options.customHeaders] Headers that will be added to the - * request - * - * @param {function} callback - The 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 if an error did not occur. - * See {@link QueryResults} 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. - */ -function _query(workspaceId, body, options, callback) { - /* jshint validthis: true */ - let client = this; - if(!callback && typeof options === 'function') { - callback = options; - options = null; - } - if (!callback) { - throw new Error('callback cannot be null.'); - } - // Validate - try { - if (workspaceId === null || workspaceId === undefined || typeof workspaceId.valueOf() !== 'string') { - throw new Error('workspaceId cannot be null or undefined and it must be of type string.'); - } - if (body === null || body === undefined) { - throw new Error('body cannot be null or undefined.'); - } - } catch (error) { - return callback(error); - } - - // Construct URL - let baseUrl = this.baseUri; - let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'workspaces/{workspaceId}/query'; - requestUrl = requestUrl.replace('{workspaceId}', encodeURIComponent(workspaceId)); - - // Create HTTP transport objects - let httpRequest = new WebResource(); - httpRequest.method = 'POST'; - httpRequest.url = requestUrl; - httpRequest.headers = {}; - // Set Headers - httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; - if(options) { - for(let headerName in options['customHeaders']) { - if (options['customHeaders'].hasOwnProperty(headerName)) { - httpRequest.headers[headerName] = options['customHeaders'][headerName]; - } - } - } - // Serialize Request - let requestContent = null; - let requestModel = null; - try { - if (body !== null && body !== undefined) { - let requestModelMapper = new client.models['QueryBody']().mapper(); - requestModel = client.serialize(requestModelMapper, body, 'body'); - requestContent = JSON.stringify(requestModel); - } - } catch (error) { - let serializationError = new Error(`Error "${error.message}" occurred in serializing the ` + - `payload - ${JSON.stringify(body, null, 2)}.`); - return callback(serializationError); - } - httpRequest.body = requestContent; - // Send Request - return client.pipeline(httpRequest, (err, response, responseBody) => { - if (err) { - return callback(err); - } - let statusCode = response.statusCode; - if (statusCode !== 200) { - let error = new Error(responseBody); - error.statusCode = response.statusCode; - error.request = msRest.stripRequest(httpRequest); - error.response = msRest.stripResponse(response); - if (responseBody === '') responseBody = null; - let parsedErrorResponse; - try { - parsedErrorResponse = JSON.parse(responseBody); - if (parsedErrorResponse) { - let internalError = null; - if (parsedErrorResponse.error) internalError = parsedErrorResponse.error; - error.code = internalError ? internalError.code : parsedErrorResponse.code; - error.message = internalError ? internalError.message : parsedErrorResponse.message; - } - if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) { - let resultMapper = new client.models['ErrorResponse']().mapper(); - error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body'); - } - } catch (defaultError) { - error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` + - `- "${responseBody}" for the default response.`; - return callback(error); - } - return callback(error); - } - // Create Result - let result = null; - if (responseBody === '') responseBody = null; - // Deserialize Response - if (statusCode === 200) { - let parsedResponse = null; - try { - parsedResponse = JSON.parse(responseBody); - result = JSON.parse(responseBody); - if (parsedResponse !== null && parsedResponse !== undefined) { - let resultMapper = new client.models['QueryResults']().mapper(); - result = client.deserialize(resultMapper, parsedResponse, 'result'); - } - } catch (error) { - let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`); - deserializationError.request = msRest.stripRequest(httpRequest); - deserializationError.response = msRest.stripResponse(response); - return callback(deserializationError); - } - } - - return callback(null, result, httpRequest, response); - }); -} - /** Class representing a LogAnalyticsClient. */ class LogAnalyticsClient extends ServiceClient { /** @@ -204,129 +50,11 @@ class LogAnalyticsClient extends ServiceClient { let packageInfo = this.getPackageJsonInfo(__dirname); this.addUserAgentInfo(`${packageInfo.name}/${packageInfo.version}`); + this.query = new operations.Query(this); this.models = models; - this._query = _query; msRest.addSerializationMixin(this); } - /** - * @summary Execute an Analytics query - * - * Executes an Analytics query for data. - * [Here](https://dev.loganalytics.io/documentation/Using-the-API) is an - * example for using POST with an Analytics query. - * - * @param {string} workspaceId ID of the workspace. This is Workspace ID from - * the Properties blade in the Azure portal. - * - * @param {object} body The Analytics query. Learn more about the [Analytics - * query - * syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/) - * - * @param {string} body.query The query to execute. - * - * @param {string} [body.timespan] Optional. The timespan over which to query - * data. This is an ISO8601 time period value. This timespan is applied in - * addition to any that are specified in the query expression. - * - * @param {array} [body.workspaces] A list of workspaces that are included in - * the query. - * - * @param {object} [options] Optional Parameters. - * - * @param {object} [options.customHeaders] Headers that will be added to the - * request - * - * @returns {Promise} A promise is returned - * - * @resolve {HttpOperationResponse} - The deserialized result object. - * - * @reject {Error} - The error object. - */ - queryWithHttpOperationResponse(workspaceId, body, options) { - let client = this; - let self = this; - return new Promise((resolve, reject) => { - self._query(workspaceId, body, options, (err, result, request, response) => { - let httpOperationResponse = new msRest.HttpOperationResponse(request, response); - httpOperationResponse.body = result; - if (err) { reject(err); } - else { resolve(httpOperationResponse); } - return; - }); - }); - } - - /** - * @summary Execute an Analytics query - * - * Executes an Analytics query for data. - * [Here](https://dev.loganalytics.io/documentation/Using-the-API) is an - * example for using POST with an Analytics query. - * - * @param {string} workspaceId ID of the workspace. This is Workspace ID from - * the Properties blade in the Azure portal. - * - * @param {object} body The Analytics query. Learn more about the [Analytics - * query - * syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/) - * - * @param {string} body.query The query to execute. - * - * @param {string} [body.timespan] Optional. The timespan over which to query - * data. This is an ISO8601 time period value. This timespan is applied in - * addition to any that are specified in the query expression. - * - * @param {array} [body.workspaces] A list of workspaces that are included in - * the query. - * - * @param {object} [options] Optional Parameters. - * - * @param {object} [options.customHeaders] Headers that will be added to the - * request - * - * @param {function} [optionalCallback] - The optional callback. - * - * @returns {function|Promise} If a callback was passed as the last parameter - * then it returns the callback else returns a Promise. - * - * {Promise} A promise is returned - * - * @resolve {QueryResults} - The deserialized result object. - * - * @reject {Error} - The error object. - * - * {function} optionalCallback(err, result, request, response) - * - * {Error} err - The Error object if an error occurred, null otherwise. - * - * {object} [result] - The deserialized result object if an error did not occur. - * See {@link QueryResults} 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. - */ - query(workspaceId, body, options, optionalCallback) { - let client = this; - let self = this; - if (!optionalCallback && typeof options === 'function') { - optionalCallback = options; - options = null; - } - if (!optionalCallback) { - return new Promise((resolve, reject) => { - self._query(workspaceId, body, options, (err, result, request, response) => { - if (err) { reject(err); } - else { resolve(result); } - return; - }); - }); - } else { - return self._query(workspaceId, body, options, optionalCallback); - } - } - } module.exports = LogAnalyticsClient; diff --git a/lib/services/loganalytics/lib/operations/index.d.ts b/lib/services/loganalytics/lib/operations/index.d.ts new file mode 100644 index 0000000000..ca4e4747d7 --- /dev/null +++ b/lib/services/loganalytics/lib/operations/index.d.ts @@ -0,0 +1,113 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + * + * Code generated by Microsoft (R) AutoRest Code Generator. + * Changes may cause incorrect behavior and will be lost if the code is + * regenerated. +*/ + +import { ServiceClientOptions, RequestOptions, ServiceCallback, HttpOperationResponse } from 'ms-rest'; +import * as models from '../models'; + + +/** + * @class + * Query + * __NOTE__: An instance of this class is automatically created for an + * instance of the LogAnalyticsClient. + */ +export interface Query { + + + /** + * @summary Execute an Analytics query + * + * Executes an Analytics query for data. + * [Here](https://dev.loganalytics.io/documentation/Using-the-API) is an + * example for using POST with an Analytics query. + * + * @param {string} workspaceId ID of the workspace. This is Workspace ID from + * the Properties blade in the Azure portal. + * + * @param {object} body The Analytics query. Learn more about the [Analytics + * query + * syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/) + * + * @param {string} body.query The query to execute. + * + * @param {string} [body.timespan] Optional. The timespan over which to query + * data. This is an ISO8601 time period value. This timespan is applied in + * addition to any that are specified in the query expression. + * + * @param {array} [body.workspaces] A list of workspaces that are included in + * the query. + * + * @param {object} [options] Optional Parameters. + * + * @param {object} [options.customHeaders] Headers that will be added to the + * request + * + * @returns {Promise} A promise is returned + * + * @resolve {HttpOperationResponse} - The deserialized result object. + * + * @reject {Error|ServiceError} - The error object. + */ + executeWithHttpOperationResponse(workspaceId: string, body: models.QueryBody, options?: { customHeaders? : { [headerName: string]: string; } }): Promise>; + + /** + * @summary Execute an Analytics query + * + * Executes an Analytics query for data. + * [Here](https://dev.loganalytics.io/documentation/Using-the-API) is an + * example for using POST with an Analytics query. + * + * @param {string} workspaceId ID of the workspace. This is Workspace ID from + * the Properties blade in the Azure portal. + * + * @param {object} body The Analytics query. Learn more about the [Analytics + * query + * syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/) + * + * @param {string} body.query The query to execute. + * + * @param {string} [body.timespan] Optional. The timespan over which to query + * data. This is an ISO8601 time period value. This timespan is applied in + * addition to any that are specified in the query expression. + * + * @param {array} [body.workspaces] A list of workspaces that are included in + * the query. + * + * @param {object} [options] Optional Parameters. + * + * @param {object} [options.customHeaders] Headers that will be added to the + * request + * + * @param {ServiceCallback} [optionalCallback] - The optional callback. + * + * @returns {ServiceCallback|Promise} If a callback was passed as the last + * parameter then it returns the callback else returns a Promise. + * + * {Promise} A promise is returned. + * + * @resolve {QueryResults} - The deserialized result object. + * + * @reject {Error|ServiceError} - The error object. + * + * {ServiceCallback} optionalCallback(err, result, request, response) + * + * {Error|ServiceError} err - The Error object if an error occurred, null otherwise. + * + * {QueryResults} [result] - The deserialized result object if an error did not occur. + * See {@link QueryResults} for more information. + * + * {WebResource} [request] - The HTTP Request object if an error did not occur. + * + * {http.IncomingMessage} [response] - The HTTP Response stream if an error did not occur. + */ + execute(workspaceId: string, body: models.QueryBody, options?: { customHeaders? : { [headerName: string]: string; } }): Promise; + execute(workspaceId: string, body: models.QueryBody, callback: ServiceCallback): void; + execute(workspaceId: string, body: models.QueryBody, options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback): void; +} diff --git a/lib/services/loganalytics/lib/operations/index.js b/lib/services/loganalytics/lib/operations/index.js new file mode 100644 index 0000000000..6e0ac550e4 --- /dev/null +++ b/lib/services/loganalytics/lib/operations/index.js @@ -0,0 +1,17 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + * + * Code generated by Microsoft (R) AutoRest Code Generator. + * Changes may cause incorrect behavior and will be lost if the code is + * regenerated. + */ + +/* jshint latedef:false */ +/* jshint forin:false */ +/* jshint noempty:false */ + +'use strict'; + +exports.Query = require('./query'); diff --git a/lib/services/loganalytics/lib/operations/query.js b/lib/services/loganalytics/lib/operations/query.js new file mode 100644 index 0000000000..8b3f26fbbc --- /dev/null +++ b/lib/services/loganalytics/lib/operations/query.js @@ -0,0 +1,301 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + * + * Code generated by Microsoft (R) AutoRest Code Generator. + * Changes may cause incorrect behavior and will be lost if the code is + * regenerated. + */ + +'use strict'; + +const msRest = require('ms-rest'); +const WebResource = msRest.WebResource; + +/** + * @summary Execute an Analytics query + * + * Executes an Analytics query for data. + * [Here](https://dev.loganalytics.io/documentation/Using-the-API) is an + * example for using POST with an Analytics query. + * + * @param {string} workspaceId ID of the workspace. This is Workspace ID from + * the Properties blade in the Azure portal. + * + * @param {object} body The Analytics query. Learn more about the [Analytics + * query + * syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/) + * + * @param {string} body.query The query to execute. + * + * @param {string} [body.timespan] Optional. The timespan over which to query + * data. This is an ISO8601 time period value. This timespan is applied in + * addition to any that are specified in the query expression. + * + * @param {array} [body.workspaces] A list of workspaces that are included in + * the query. + * + * @param {object} [options] Optional Parameters. + * + * @param {object} [options.customHeaders] Headers that will be added to the + * request + * + * @param {function} callback - The 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 if an error did not occur. + * See {@link QueryResults} 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. + */ +function _execute(workspaceId, body, options, callback) { + /* jshint validthis: true */ + let client = this.client; + if(!callback && typeof options === 'function') { + callback = options; + options = null; + } + if (!callback) { + throw new Error('callback cannot be null.'); + } + // Validate + try { + if (workspaceId === null || workspaceId === undefined || typeof workspaceId.valueOf() !== 'string') { + throw new Error('workspaceId cannot be null or undefined and it must be of type string.'); + } + if (body === null || body === undefined) { + throw new Error('body cannot be null or undefined.'); + } + } catch (error) { + return callback(error); + } + + // Construct URL + let baseUrl = this.client.baseUri; + let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'workspaces/{workspaceId}/query'; + requestUrl = requestUrl.replace('{workspaceId}', encodeURIComponent(workspaceId)); + + // Create HTTP transport objects + let httpRequest = new WebResource(); + httpRequest.method = 'POST'; + httpRequest.url = requestUrl; + httpRequest.headers = {}; + // Set Headers + httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; + if(options) { + for(let headerName in options['customHeaders']) { + if (options['customHeaders'].hasOwnProperty(headerName)) { + httpRequest.headers[headerName] = options['customHeaders'][headerName]; + } + } + } + // Serialize Request + let requestContent = null; + let requestModel = null; + try { + if (body !== null && body !== undefined) { + let requestModelMapper = new client.models['QueryBody']().mapper(); + requestModel = client.serialize(requestModelMapper, body, 'body'); + requestContent = JSON.stringify(requestModel); + } + } catch (error) { + let serializationError = new Error(`Error "${error.message}" occurred in serializing the ` + + `payload - ${JSON.stringify(body, null, 2)}.`); + return callback(serializationError); + } + httpRequest.body = requestContent; + // Send Request + return client.pipeline(httpRequest, (err, response, responseBody) => { + if (err) { + return callback(err); + } + let statusCode = response.statusCode; + if (statusCode !== 200) { + let error = new Error(responseBody); + error.statusCode = response.statusCode; + error.request = msRest.stripRequest(httpRequest); + error.response = msRest.stripResponse(response); + if (responseBody === '') responseBody = null; + let parsedErrorResponse; + try { + parsedErrorResponse = JSON.parse(responseBody); + if (parsedErrorResponse) { + let internalError = null; + if (parsedErrorResponse.error) internalError = parsedErrorResponse.error; + error.code = internalError ? internalError.code : parsedErrorResponse.code; + error.message = internalError ? internalError.message : parsedErrorResponse.message; + } + if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) { + let resultMapper = new client.models['ErrorResponse']().mapper(); + error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body'); + } + } catch (defaultError) { + error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` + + `- "${responseBody}" for the default response.`; + return callback(error); + } + return callback(error); + } + // Create Result + let result = null; + if (responseBody === '') responseBody = null; + // Deserialize Response + if (statusCode === 200) { + let parsedResponse = null; + try { + parsedResponse = JSON.parse(responseBody); + result = JSON.parse(responseBody); + if (parsedResponse !== null && parsedResponse !== undefined) { + let resultMapper = new client.models['QueryResults']().mapper(); + result = client.deserialize(resultMapper, parsedResponse, 'result'); + } + } catch (error) { + let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`); + deserializationError.request = msRest.stripRequest(httpRequest); + deserializationError.response = msRest.stripResponse(response); + return callback(deserializationError); + } + } + + return callback(null, result, httpRequest, response); + }); +} + +/** Class representing a Query. */ +class Query { + /** + * Create a Query. + * @param {LogAnalyticsClient} client Reference to the service client. + */ + constructor(client) { + this.client = client; + this._execute = _execute; + } + + /** + * @summary Execute an Analytics query + * + * Executes an Analytics query for data. + * [Here](https://dev.loganalytics.io/documentation/Using-the-API) is an + * example for using POST with an Analytics query. + * + * @param {string} workspaceId ID of the workspace. This is Workspace ID from + * the Properties blade in the Azure portal. + * + * @param {object} body The Analytics query. Learn more about the [Analytics + * query + * syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/) + * + * @param {string} body.query The query to execute. + * + * @param {string} [body.timespan] Optional. The timespan over which to query + * data. This is an ISO8601 time period value. This timespan is applied in + * addition to any that are specified in the query expression. + * + * @param {array} [body.workspaces] A list of workspaces that are included in + * the query. + * + * @param {object} [options] Optional Parameters. + * + * @param {object} [options.customHeaders] Headers that will be added to the + * request + * + * @returns {Promise} A promise is returned + * + * @resolve {HttpOperationResponse} - The deserialized result object. + * + * @reject {Error} - The error object. + */ + executeWithHttpOperationResponse(workspaceId, body, options) { + let client = this.client; + let self = this; + return new Promise((resolve, reject) => { + self._execute(workspaceId, body, options, (err, result, request, response) => { + let httpOperationResponse = new msRest.HttpOperationResponse(request, response); + httpOperationResponse.body = result; + if (err) { reject(err); } + else { resolve(httpOperationResponse); } + return; + }); + }); + } + + /** + * @summary Execute an Analytics query + * + * Executes an Analytics query for data. + * [Here](https://dev.loganalytics.io/documentation/Using-the-API) is an + * example for using POST with an Analytics query. + * + * @param {string} workspaceId ID of the workspace. This is Workspace ID from + * the Properties blade in the Azure portal. + * + * @param {object} body The Analytics query. Learn more about the [Analytics + * query + * syntax](https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/) + * + * @param {string} body.query The query to execute. + * + * @param {string} [body.timespan] Optional. The timespan over which to query + * data. This is an ISO8601 time period value. This timespan is applied in + * addition to any that are specified in the query expression. + * + * @param {array} [body.workspaces] A list of workspaces that are included in + * the query. + * + * @param {object} [options] Optional Parameters. + * + * @param {object} [options.customHeaders] Headers that will be added to the + * request + * + * @param {function} [optionalCallback] - The optional callback. + * + * @returns {function|Promise} If a callback was passed as the last parameter + * then it returns the callback else returns a Promise. + * + * {Promise} A promise is returned + * + * @resolve {QueryResults} - The deserialized result object. + * + * @reject {Error} - The error object. + * + * {function} optionalCallback(err, result, request, response) + * + * {Error} err - The Error object if an error occurred, null otherwise. + * + * {object} [result] - The deserialized result object if an error did not occur. + * See {@link QueryResults} 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. + */ + execute(workspaceId, body, options, optionalCallback) { + let client = this.client; + let self = this; + if (!optionalCallback && typeof options === 'function') { + optionalCallback = options; + options = null; + } + if (!optionalCallback) { + return new Promise((resolve, reject) => { + self._execute(workspaceId, body, options, (err, result, request, response) => { + if (err) { reject(err); } + else { resolve(result); } + return; + }); + }); + } else { + return self._execute(workspaceId, body, options, optionalCallback); + } + } + +} + +module.exports = Query;