< Summary

Class:Microsoft.Azure.ApplicationInsights.Query.QueryOperations
Assembly:Microsoft.Azure.ApplicationInsights.Query
File(s):C:\Git\azure-sdk-for-net\sdk\applicationinsights\Microsoft.Azure.ApplicationInsights.Query\src\Generated\QueryOperations.cs
Covered lines:4
Uncovered lines:82
Coverable lines:86
Total lines:237
Line coverage:4.6% (4 of 86)
Covered branches:1
Total branches:44
Branch coverage:2.2% (1 of 44)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-80%50%
get_Client()-0%100%
ExecuteWithHttpMessagesAsync()-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\applicationinsights\Microsoft.Azure.ApplicationInsights.Query\src\Generated\QueryOperations.cs

#LineLine coverage
 1// <auto-generated>
 2// Copyright (c) Microsoft Corporation. All rights reserved.
 3// Licensed under the MIT License. See License.txt in the project root for
 4// license information.
 5//
 6// Code generated by Microsoft (R) AutoRest Code Generator.
 7// Changes may cause incorrect behavior and will be lost if the code is
 8// regenerated.
 9// </auto-generated>
 10
 11namespace Microsoft.Azure.ApplicationInsights.Query
 12{
 13    using Microsoft.Rest;
 14    using Models;
 15    using Newtonsoft.Json;
 16    using System.Collections;
 17    using System.Collections.Generic;
 18    using System.IO;
 19    using System.Linq;
 20    using System.Net;
 21    using System.Net.Http;
 22    using System.Threading;
 23    using System.Threading.Tasks;
 24
 25    /// <summary>
 26    /// QueryOperations operations.
 27    /// </summary>
 28    public partial class QueryOperations : IServiceOperations<ApplicationInsightsDataClient>, IQueryOperations
 29    {
 30        /// <summary>
 31        /// Initializes a new instance of the QueryOperations class.
 32        /// </summary>
 33        /// <param name='client'>
 34        /// Reference to the service client.
 35        /// </param>
 36        /// <exception cref="System.ArgumentNullException">
 37        /// Thrown when a required parameter is null
 38        /// </exception>
 2039        public QueryOperations(ApplicationInsightsDataClient client)
 40        {
 2041            if (client == null)
 42            {
 043                throw new System.ArgumentNullException("client");
 44            }
 2045            Client = client;
 2046        }
 47
 48        /// <summary>
 49        /// Gets a reference to the ApplicationInsightsDataClient
 50        /// </summary>
 051        public ApplicationInsightsDataClient Client { get; private set; }
 52
 53        /// <summary>
 54        /// Execute an Analytics query
 55        /// </summary>
 56        /// <remarks>
 57        /// Executes an Analytics query for data.
 58        /// [Here](https://dev.applicationinsights.io/documentation/Using-the-API/Query)
 59        /// is an example for using POST with an Analytics query.
 60        /// </remarks>
 61        /// <param name='appId'>
 62        /// ID of the application. This is Application ID from the API Access settings
 63        /// blade in the Azure portal.
 64        /// </param>
 65        /// <param name='query'>
 66        /// The query to execute.
 67        /// </param>
 68        /// <param name='timespan'>
 69        /// Optional. The timespan over which to query data. This is an ISO8601 time
 70        /// period value.  This timespan is applied in addition to any that are
 71        /// specified in the query expression.
 72        /// </param>
 73        /// <param name='applications'>
 74        /// A list of Application IDs for cross-application queries.
 75        /// </param>
 76        /// <param name='customHeaders'>
 77        /// Headers that will be added to request.
 78        /// </param>
 79        /// <param name='cancellationToken'>
 80        /// The cancellation token.
 81        /// </param>
 82        /// <exception cref="ErrorResponseException">
 83        /// Thrown when the operation returned an invalid status code
 84        /// </exception>
 85        /// <exception cref="SerializationException">
 86        /// Thrown when unable to deserialize the response
 87        /// </exception>
 88        /// <exception cref="ValidationException">
 89        /// Thrown when a required parameter is null
 90        /// </exception>
 91        /// <exception cref="System.ArgumentNullException">
 92        /// Thrown when a required parameter is null
 93        /// </exception>
 94        /// <return>
 95        /// A response object containing the response body and response headers.
 96        /// </return>
 97        public async Task<HttpOperationResponse<QueryResults>> ExecuteWithHttpMessagesAsync(string appId, string query, 
 98        {
 099            if (appId == null)
 100            {
 0101                throw new ValidationException(ValidationRules.CannotBeNull, "appId");
 102            }
 0103            if (query == null)
 104            {
 0105                throw new ValidationException(ValidationRules.CannotBeNull, "query");
 106            }
 0107            QueryBody body = new QueryBody();
 0108            if (query != null || timespan != null || applications != null)
 109            {
 0110                body.Query = query;
 0111                body.Timespan = timespan;
 0112                body.Applications = applications;
 113            }
 114            // Tracing
 0115            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 0116            string _invocationId = null;
 0117            if (_shouldTrace)
 118            {
 0119                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0120                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0121                tracingParameters.Add("appId", appId);
 0122                tracingParameters.Add("body", body);
 0123                tracingParameters.Add("cancellationToken", cancellationToken);
 0124                ServiceClientTracing.Enter(_invocationId, this, "Execute", tracingParameters);
 125            }
 126            // Construct URL
 0127            var _baseUrl = Client.BaseUri.AbsoluteUri;
 0128            var _url = new System.Uri(new System.Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "apps/{appId}/quer
 0129            _url = _url.Replace("{appId}", System.Uri.EscapeDataString(appId));
 130            // Create HTTP transport objects
 0131            var _httpRequest = new HttpRequestMessage();
 0132            HttpResponseMessage _httpResponse = null;
 0133            _httpRequest.Method = new HttpMethod("POST");
 0134            _httpRequest.RequestUri = new System.Uri(_url);
 135            // Set Headers
 136
 137
 0138            if (customHeaders != null)
 139            {
 0140                foreach(var _header in customHeaders)
 141                {
 0142                    if (_httpRequest.Headers.Contains(_header.Key))
 143                    {
 0144                        _httpRequest.Headers.Remove(_header.Key);
 145                    }
 0146                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 147                }
 148            }
 149
 150            // Serialize Request
 0151            string _requestContent = null;
 0152            if(body != null)
 153            {
 0154                _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(body, Client.SerializationSettings)
 0155                _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
 0156                _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio
 157            }
 158            // Set Credentials
 0159            if (Client.Credentials != null)
 160            {
 0161                cancellationToken.ThrowIfCancellationRequested();
 0162                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 163            }
 164            // Send Request
 0165            if (_shouldTrace)
 166            {
 0167                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 168            }
 0169            cancellationToken.ThrowIfCancellationRequested();
 0170            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 0171            if (_shouldTrace)
 172            {
 0173                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 174            }
 0175            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 0176            cancellationToken.ThrowIfCancellationRequested();
 0177            string _responseContent = null;
 0178            if ((int)_statusCode != 200)
 179            {
 0180                var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st
 181                try
 182                {
 0183                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0184                    ErrorResponse _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res
 0185                    if (_errorBody != null)
 186                    {
 0187                        ex.Body = _errorBody;
 188                    }
 0189                }
 0190                catch (JsonException)
 191                {
 192                    // Ignore the exception
 0193                }
 0194                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0195                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0196                if (_shouldTrace)
 197                {
 0198                    ServiceClientTracing.Error(_invocationId, ex);
 199                }
 0200                _httpRequest.Dispose();
 0201                if (_httpResponse != null)
 202                {
 0203                    _httpResponse.Dispose();
 204                }
 0205                throw ex;
 206            }
 207            // Create Result
 0208            var _result = new HttpOperationResponse<QueryResults>();
 0209            _result.Request = _httpRequest;
 0210            _result.Response = _httpResponse;
 211            // Deserialize Response
 0212            if ((int)_statusCode == 200)
 213            {
 0214                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 215                try
 216                {
 0217                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<QueryResults>(_responseContent, 
 0218                }
 0219                catch (JsonException ex)
 220                {
 0221                    _httpRequest.Dispose();
 0222                    if (_httpResponse != null)
 223                    {
 0224                        _httpResponse.Dispose();
 225                    }
 0226                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 227                }
 228            }
 0229            if (_shouldTrace)
 230            {
 0231                ServiceClientTracing.Exit(_invocationId, _result);
 232            }
 0233            return _result;
 0234        }
 235
 236    }
 237}