| | 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 | |
|
| | 11 | | namespace 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> |
| 20 | 39 | | public QueryOperations(ApplicationInsightsDataClient client) |
| | 40 | | { |
| 20 | 41 | | if (client == null) |
| | 42 | | { |
| 0 | 43 | | throw new System.ArgumentNullException("client"); |
| | 44 | | } |
| 20 | 45 | | Client = client; |
| 20 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets a reference to the ApplicationInsightsDataClient |
| | 50 | | /// </summary> |
| 0 | 51 | | 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 | | { |
| 0 | 99 | | if (appId == null) |
| | 100 | | { |
| 0 | 101 | | throw new ValidationException(ValidationRules.CannotBeNull, "appId"); |
| | 102 | | } |
| 0 | 103 | | if (query == null) |
| | 104 | | { |
| 0 | 105 | | throw new ValidationException(ValidationRules.CannotBeNull, "query"); |
| | 106 | | } |
| 0 | 107 | | QueryBody body = new QueryBody(); |
| 0 | 108 | | if (query != null || timespan != null || applications != null) |
| | 109 | | { |
| 0 | 110 | | body.Query = query; |
| 0 | 111 | | body.Timespan = timespan; |
| 0 | 112 | | body.Applications = applications; |
| | 113 | | } |
| | 114 | | // Tracing |
| 0 | 115 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 116 | | string _invocationId = null; |
| 0 | 117 | | if (_shouldTrace) |
| | 118 | | { |
| 0 | 119 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 120 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 121 | | tracingParameters.Add("appId", appId); |
| 0 | 122 | | tracingParameters.Add("body", body); |
| 0 | 123 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 124 | | ServiceClientTracing.Enter(_invocationId, this, "Execute", tracingParameters); |
| | 125 | | } |
| | 126 | | // Construct URL |
| 0 | 127 | | var _baseUrl = Client.BaseUri.AbsoluteUri; |
| 0 | 128 | | var _url = new System.Uri(new System.Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "apps/{appId}/quer |
| 0 | 129 | | _url = _url.Replace("{appId}", System.Uri.EscapeDataString(appId)); |
| | 130 | | // Create HTTP transport objects |
| 0 | 131 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 132 | | HttpResponseMessage _httpResponse = null; |
| 0 | 133 | | _httpRequest.Method = new HttpMethod("POST"); |
| 0 | 134 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 135 | | // Set Headers |
| | 136 | |
|
| | 137 | |
|
| 0 | 138 | | if (customHeaders != null) |
| | 139 | | { |
| 0 | 140 | | foreach(var _header in customHeaders) |
| | 141 | | { |
| 0 | 142 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 143 | | { |
| 0 | 144 | | _httpRequest.Headers.Remove(_header.Key); |
| | 145 | | } |
| 0 | 146 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 147 | | } |
| | 148 | | } |
| | 149 | |
|
| | 150 | | // Serialize Request |
| 0 | 151 | | string _requestContent = null; |
| 0 | 152 | | if(body != null) |
| | 153 | | { |
| 0 | 154 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(body, Client.SerializationSettings) |
| 0 | 155 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 0 | 156 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 157 | | } |
| | 158 | | // Set Credentials |
| 0 | 159 | | if (Client.Credentials != null) |
| | 160 | | { |
| 0 | 161 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 162 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 163 | | } |
| | 164 | | // Send Request |
| 0 | 165 | | if (_shouldTrace) |
| | 166 | | { |
| 0 | 167 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 168 | | } |
| 0 | 169 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 170 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 171 | | if (_shouldTrace) |
| | 172 | | { |
| 0 | 173 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 174 | | } |
| 0 | 175 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 176 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 177 | | string _responseContent = null; |
| 0 | 178 | | if ((int)_statusCode != 200) |
| | 179 | | { |
| 0 | 180 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 181 | | try |
| | 182 | | { |
| 0 | 183 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 184 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| 0 | 185 | | if (_errorBody != null) |
| | 186 | | { |
| 0 | 187 | | ex.Body = _errorBody; |
| | 188 | | } |
| 0 | 189 | | } |
| 0 | 190 | | catch (JsonException) |
| | 191 | | { |
| | 192 | | // Ignore the exception |
| 0 | 193 | | } |
| 0 | 194 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 195 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 196 | | if (_shouldTrace) |
| | 197 | | { |
| 0 | 198 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 199 | | } |
| 0 | 200 | | _httpRequest.Dispose(); |
| 0 | 201 | | if (_httpResponse != null) |
| | 202 | | { |
| 0 | 203 | | _httpResponse.Dispose(); |
| | 204 | | } |
| 0 | 205 | | throw ex; |
| | 206 | | } |
| | 207 | | // Create Result |
| 0 | 208 | | var _result = new HttpOperationResponse<QueryResults>(); |
| 0 | 209 | | _result.Request = _httpRequest; |
| 0 | 210 | | _result.Response = _httpResponse; |
| | 211 | | // Deserialize Response |
| 0 | 212 | | if ((int)_statusCode == 200) |
| | 213 | | { |
| 0 | 214 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 215 | | try |
| | 216 | | { |
| 0 | 217 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<QueryResults>(_responseContent, |
| 0 | 218 | | } |
| 0 | 219 | | catch (JsonException ex) |
| | 220 | | { |
| 0 | 221 | | _httpRequest.Dispose(); |
| 0 | 222 | | if (_httpResponse != null) |
| | 223 | | { |
| 0 | 224 | | _httpResponse.Dispose(); |
| | 225 | | } |
| 0 | 226 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 227 | | } |
| | 228 | | } |
| 0 | 229 | | if (_shouldTrace) |
| | 230 | | { |
| 0 | 231 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 232 | | } |
| 0 | 233 | | return _result; |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | } |
| | 237 | | } |