| | | 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.CognitiveServices.AnomalyDetector |
| | | 12 | | { |
| | | 13 | | using Microsoft.Rest; |
| | | 14 | | using Microsoft.Rest.Serialization; |
| | | 15 | | using Models; |
| | | 16 | | using Newtonsoft.Json; |
| | | 17 | | using System.Collections; |
| | | 18 | | using System.Collections.Generic; |
| | | 19 | | using System.Net; |
| | | 20 | | using System.Net.Http; |
| | | 21 | | using System.Threading; |
| | | 22 | | using System.Threading.Tasks; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The Anomaly Detector API detects anomalies automatically in time series |
| | | 26 | | /// data. It supports two functionalities, one is for detecting the whole |
| | | 27 | | /// series with model trained by the timeseries, another is detecting last |
| | | 28 | | /// point with model trained by points before. By using this service, |
| | | 29 | | /// business customers can discover incidents and establish a logic flow |
| | | 30 | | /// for root cause analysis. |
| | | 31 | | /// </summary> |
| | | 32 | | public partial class AnomalyDetectorClient : ServiceClient<AnomalyDetectorClient>, IAnomalyDetectorClient |
| | | 33 | | { |
| | | 34 | | /// <summary> |
| | | 35 | | /// The base URI of the service. |
| | | 36 | | /// </summary> |
| | 0 | 37 | | internal string BaseUri {get; set;} |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets or sets json serialization settings. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public JsonSerializerSettings SerializationSettings { get; private set; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets or sets json deserialization settings. |
| | | 46 | | /// </summary> |
| | 0 | 47 | | public JsonSerializerSettings DeserializationSettings { get; private set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Supported Cognitive Services endpoints (protocol and hostname, for example: |
| | | 51 | | /// https://westus2.api.cognitive.microsoft.com). |
| | | 52 | | /// </summary> |
| | 0 | 53 | | public string Endpoint { get; set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Subscription credentials which uniquely identify client subscription. |
| | | 57 | | /// </summary> |
| | 0 | 58 | | public ServiceClientCredentials Credentials { get; private set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Initializes a new instance of the AnomalyDetectorClient class. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name='httpClient'> |
| | | 64 | | /// HttpClient to be used |
| | | 65 | | /// </param> |
| | | 66 | | /// <param name='disposeHttpClient'> |
| | | 67 | | /// True: will dispose the provided httpClient on calling AnomalyDetectorClient.Dispose(). False: will not dispo |
| | 0 | 68 | | protected AnomalyDetectorClient(HttpClient httpClient, bool disposeHttpClient) : base(httpClient, disposeHttpCli |
| | | 69 | | { |
| | 0 | 70 | | Initialize(); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Initializes a new instance of the AnomalyDetectorClient class. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name='handlers'> |
| | | 77 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | | 78 | | /// </param> |
| | 0 | 79 | | protected AnomalyDetectorClient(params DelegatingHandler[] handlers) : base(handlers) |
| | | 80 | | { |
| | 0 | 81 | | Initialize(); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Initializes a new instance of the AnomalyDetectorClient class. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name='rootHandler'> |
| | | 88 | | /// Optional. The http client handler used to handle http transport. |
| | | 89 | | /// </param> |
| | | 90 | | /// <param name='handlers'> |
| | | 91 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | | 92 | | /// </param> |
| | 0 | 93 | | protected AnomalyDetectorClient(HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : base(rootH |
| | | 94 | | { |
| | 0 | 95 | | Initialize(); |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Initializes a new instance of the AnomalyDetectorClient class. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name='credentials'> |
| | | 102 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | | 103 | | /// </param> |
| | | 104 | | /// <param name='handlers'> |
| | | 105 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | | 106 | | /// </param> |
| | | 107 | | /// <exception cref="System.ArgumentNullException"> |
| | | 108 | | /// Thrown when a required parameter is null |
| | | 109 | | /// </exception> |
| | 0 | 110 | | public AnomalyDetectorClient(ServiceClientCredentials credentials, params DelegatingHandler[] handlers) : this(h |
| | | 111 | | { |
| | 0 | 112 | | if (credentials == null) |
| | | 113 | | { |
| | 0 | 114 | | throw new System.ArgumentNullException("credentials"); |
| | | 115 | | } |
| | 0 | 116 | | Credentials = credentials; |
| | 0 | 117 | | if (Credentials != null) |
| | | 118 | | { |
| | 0 | 119 | | Credentials.InitializeServiceClient(this); |
| | | 120 | | } |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Initializes a new instance of the AnomalyDetectorClient class. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <param name='credentials'> |
| | | 127 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | | 128 | | /// </param> |
| | | 129 | | /// <param name='httpClient'> |
| | | 130 | | /// HttpClient to be used |
| | | 131 | | /// </param> |
| | | 132 | | /// <param name='disposeHttpClient'> |
| | | 133 | | /// True: will dispose the provided httpClient on calling AnomalyDetectorClient.Dispose(). False: will not dispo |
| | | 134 | | /// <exception cref="System.ArgumentNullException"> |
| | | 135 | | /// Thrown when a required parameter is null |
| | | 136 | | /// </exception> |
| | 0 | 137 | | public AnomalyDetectorClient(ServiceClientCredentials credentials, HttpClient httpClient, bool disposeHttpClient |
| | | 138 | | { |
| | 0 | 139 | | if (credentials == null) |
| | | 140 | | { |
| | 0 | 141 | | throw new System.ArgumentNullException("credentials"); |
| | | 142 | | } |
| | 0 | 143 | | Credentials = credentials; |
| | 0 | 144 | | if (Credentials != null) |
| | | 145 | | { |
| | 0 | 146 | | Credentials.InitializeServiceClient(this); |
| | | 147 | | } |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Initializes a new instance of the AnomalyDetectorClient class. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <param name='credentials'> |
| | | 154 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | | 155 | | /// </param> |
| | | 156 | | /// <param name='rootHandler'> |
| | | 157 | | /// Optional. The http client handler used to handle http transport. |
| | | 158 | | /// </param> |
| | | 159 | | /// <param name='handlers'> |
| | | 160 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | | 161 | | /// </param> |
| | | 162 | | /// <exception cref="System.ArgumentNullException"> |
| | | 163 | | /// Thrown when a required parameter is null |
| | | 164 | | /// </exception> |
| | 0 | 165 | | public AnomalyDetectorClient(ServiceClientCredentials credentials, HttpClientHandler rootHandler, params Delegat |
| | | 166 | | { |
| | 0 | 167 | | if (credentials == null) |
| | | 168 | | { |
| | 0 | 169 | | throw new System.ArgumentNullException("credentials"); |
| | | 170 | | } |
| | 0 | 171 | | Credentials = credentials; |
| | 0 | 172 | | if (Credentials != null) |
| | | 173 | | { |
| | 0 | 174 | | Credentials.InitializeServiceClient(this); |
| | | 175 | | } |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// An optional partial-method to perform custom initialization. |
| | | 180 | | ///</summary> |
| | | 181 | | partial void CustomInitialize(); |
| | | 182 | | /// <summary> |
| | | 183 | | /// Initializes client properties. |
| | | 184 | | /// </summary> |
| | | 185 | | private void Initialize() |
| | | 186 | | { |
| | 0 | 187 | | BaseUri = "{Endpoint}/anomalydetector/v1.0"; |
| | 0 | 188 | | SerializationSettings = new JsonSerializerSettings |
| | 0 | 189 | | { |
| | 0 | 190 | | Formatting = Newtonsoft.Json.Formatting.Indented, |
| | 0 | 191 | | DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, |
| | 0 | 192 | | DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, |
| | 0 | 193 | | NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, |
| | 0 | 194 | | ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, |
| | 0 | 195 | | ContractResolver = new ReadOnlyJsonContractResolver(), |
| | 0 | 196 | | Converters = new List<JsonConverter> |
| | 0 | 197 | | { |
| | 0 | 198 | | new Iso8601TimeSpanConverter() |
| | 0 | 199 | | } |
| | 0 | 200 | | }; |
| | 0 | 201 | | DeserializationSettings = new JsonSerializerSettings |
| | 0 | 202 | | { |
| | 0 | 203 | | DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, |
| | 0 | 204 | | DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, |
| | 0 | 205 | | NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, |
| | 0 | 206 | | ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, |
| | 0 | 207 | | ContractResolver = new ReadOnlyJsonContractResolver(), |
| | 0 | 208 | | Converters = new List<JsonConverter> |
| | 0 | 209 | | { |
| | 0 | 210 | | new Iso8601TimeSpanConverter() |
| | 0 | 211 | | } |
| | 0 | 212 | | }; |
| | | 213 | | CustomInitialize(); |
| | 0 | 214 | | } |
| | | 215 | | /// <summary> |
| | | 216 | | /// Detect anomalies for the entire series in batch. |
| | | 217 | | /// </summary> |
| | | 218 | | /// <remarks> |
| | | 219 | | /// This operation generates a model using an entire series, each point is |
| | | 220 | | /// detected with the same model. With this method, points before and after a |
| | | 221 | | /// certain point are used to determine whether it is an anomaly. The entire |
| | | 222 | | /// detection can give user an overall status of the time series. |
| | | 223 | | /// </remarks> |
| | | 224 | | /// <param name='body'> |
| | | 225 | | /// Time series points and period if needed. Advanced model parameters can also |
| | | 226 | | /// be set in the request. |
| | | 227 | | /// </param> |
| | | 228 | | /// <param name='customHeaders'> |
| | | 229 | | /// Headers that will be added to request. |
| | | 230 | | /// </param> |
| | | 231 | | /// <param name='cancellationToken'> |
| | | 232 | | /// The cancellation token. |
| | | 233 | | /// </param> |
| | | 234 | | /// <exception cref="APIErrorException"> |
| | | 235 | | /// Thrown when the operation returned an invalid status code |
| | | 236 | | /// </exception> |
| | | 237 | | /// <exception cref="SerializationException"> |
| | | 238 | | /// Thrown when unable to deserialize the response |
| | | 239 | | /// </exception> |
| | | 240 | | /// <exception cref="ValidationException"> |
| | | 241 | | /// Thrown when a required parameter is null |
| | | 242 | | /// </exception> |
| | | 243 | | /// <exception cref="System.ArgumentNullException"> |
| | | 244 | | /// Thrown when a required parameter is null |
| | | 245 | | /// </exception> |
| | | 246 | | /// <return> |
| | | 247 | | /// A response object containing the response body and response headers. |
| | | 248 | | /// </return> |
| | | 249 | | public async Task<HttpOperationResponse<EntireDetectResponse>> EntireDetectWithHttpMessagesAsync(Request body, D |
| | | 250 | | { |
| | 0 | 251 | | if (Endpoint == null) |
| | | 252 | | { |
| | 0 | 253 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | | 254 | | } |
| | 0 | 255 | | if (body == null) |
| | | 256 | | { |
| | 0 | 257 | | throw new ValidationException(ValidationRules.CannotBeNull, "body"); |
| | | 258 | | } |
| | 0 | 259 | | if (body != null) |
| | | 260 | | { |
| | 0 | 261 | | body.Validate(); |
| | | 262 | | } |
| | | 263 | | // Tracing |
| | 0 | 264 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 0 | 265 | | string _invocationId = null; |
| | 0 | 266 | | if (_shouldTrace) |
| | | 267 | | { |
| | 0 | 268 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 269 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 270 | | tracingParameters.Add("body", body); |
| | 0 | 271 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 272 | | ServiceClientTracing.Enter(_invocationId, this, "EntireDetect", tracingParameters); |
| | | 273 | | } |
| | | 274 | | // Construct URL |
| | 0 | 275 | | var _baseUrl = BaseUri; |
| | 0 | 276 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "timeseries/entire/detect"; |
| | 0 | 277 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | | 278 | | // Create HTTP transport objects |
| | 0 | 279 | | var _httpRequest = new HttpRequestMessage(); |
| | 0 | 280 | | HttpResponseMessage _httpResponse = null; |
| | 0 | 281 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 0 | 282 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 283 | | // Set Headers |
| | | 284 | | |
| | | 285 | | |
| | 0 | 286 | | if (customHeaders != null) |
| | | 287 | | { |
| | 0 | 288 | | foreach(var _header in customHeaders) |
| | | 289 | | { |
| | 0 | 290 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 291 | | { |
| | 0 | 292 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 293 | | } |
| | 0 | 294 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 295 | | } |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | // Serialize Request |
| | 0 | 299 | | string _requestContent = null; |
| | 0 | 300 | | if(body != null) |
| | | 301 | | { |
| | 0 | 302 | | _requestContent = SafeJsonConvert.SerializeObject(body, SerializationSettings); |
| | 0 | 303 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 0 | 304 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 305 | | } |
| | | 306 | | // Set Credentials |
| | 0 | 307 | | if (Credentials != null) |
| | | 308 | | { |
| | 0 | 309 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 310 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 311 | | } |
| | | 312 | | // Send Request |
| | 0 | 313 | | if (_shouldTrace) |
| | | 314 | | { |
| | 0 | 315 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 316 | | } |
| | 0 | 317 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 318 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 0 | 319 | | if (_shouldTrace) |
| | | 320 | | { |
| | 0 | 321 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 322 | | } |
| | 0 | 323 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 0 | 324 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 325 | | string _responseContent = null; |
| | 0 | 326 | | if ((int)_statusCode != 200) |
| | | 327 | | { |
| | 0 | 328 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | | 329 | | try |
| | | 330 | | { |
| | 0 | 331 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 332 | | APIError _errorBody = SafeJsonConvert.DeserializeObject<APIError>(_responseContent, Deserialization |
| | 0 | 333 | | if (_errorBody != null) |
| | | 334 | | { |
| | 0 | 335 | | ex.Body = _errorBody; |
| | | 336 | | } |
| | 0 | 337 | | } |
| | 0 | 338 | | catch (JsonException) |
| | | 339 | | { |
| | | 340 | | // Ignore the exception |
| | 0 | 341 | | } |
| | 0 | 342 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 343 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 344 | | if (_shouldTrace) |
| | | 345 | | { |
| | 0 | 346 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 347 | | } |
| | 0 | 348 | | _httpRequest.Dispose(); |
| | 0 | 349 | | if (_httpResponse != null) |
| | | 350 | | { |
| | 0 | 351 | | _httpResponse.Dispose(); |
| | | 352 | | } |
| | 0 | 353 | | throw ex; |
| | | 354 | | } |
| | | 355 | | // Create Result |
| | 0 | 356 | | var _result = new HttpOperationResponse<EntireDetectResponse>(); |
| | 0 | 357 | | _result.Request = _httpRequest; |
| | 0 | 358 | | _result.Response = _httpResponse; |
| | | 359 | | // Deserialize Response |
| | 0 | 360 | | if ((int)_statusCode == 200) |
| | | 361 | | { |
| | 0 | 362 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 363 | | try |
| | | 364 | | { |
| | 0 | 365 | | _result.Body = SafeJsonConvert.DeserializeObject<EntireDetectResponse>(_responseContent, Deserializa |
| | 0 | 366 | | } |
| | 0 | 367 | | catch (JsonException ex) |
| | | 368 | | { |
| | 0 | 369 | | _httpRequest.Dispose(); |
| | 0 | 370 | | if (_httpResponse != null) |
| | | 371 | | { |
| | 0 | 372 | | _httpResponse.Dispose(); |
| | | 373 | | } |
| | 0 | 374 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 375 | | } |
| | | 376 | | } |
| | 0 | 377 | | if (_shouldTrace) |
| | | 378 | | { |
| | 0 | 379 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 380 | | } |
| | 0 | 381 | | return _result; |
| | 0 | 382 | | } |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Detect anomaly status of the latest point in time series. |
| | | 386 | | /// </summary> |
| | | 387 | | /// <remarks> |
| | | 388 | | /// This operation generates a model using points before the latest one. With |
| | | 389 | | /// this method, only historical points are used to determine whether the |
| | | 390 | | /// target point is an anomaly. The latest point detecting operation matches |
| | | 391 | | /// the scenario of real-time monitoring of business metrics. |
| | | 392 | | /// </remarks> |
| | | 393 | | /// <param name='body'> |
| | | 394 | | /// Time series points and period if needed. Advanced model parameters can also |
| | | 395 | | /// be set in the request. |
| | | 396 | | /// </param> |
| | | 397 | | /// <param name='customHeaders'> |
| | | 398 | | /// Headers that will be added to request. |
| | | 399 | | /// </param> |
| | | 400 | | /// <param name='cancellationToken'> |
| | | 401 | | /// The cancellation token. |
| | | 402 | | /// </param> |
| | | 403 | | /// <exception cref="APIErrorException"> |
| | | 404 | | /// Thrown when the operation returned an invalid status code |
| | | 405 | | /// </exception> |
| | | 406 | | /// <exception cref="SerializationException"> |
| | | 407 | | /// Thrown when unable to deserialize the response |
| | | 408 | | /// </exception> |
| | | 409 | | /// <exception cref="ValidationException"> |
| | | 410 | | /// Thrown when a required parameter is null |
| | | 411 | | /// </exception> |
| | | 412 | | /// <exception cref="System.ArgumentNullException"> |
| | | 413 | | /// Thrown when a required parameter is null |
| | | 414 | | /// </exception> |
| | | 415 | | /// <return> |
| | | 416 | | /// A response object containing the response body and response headers. |
| | | 417 | | /// </return> |
| | | 418 | | public async Task<HttpOperationResponse<LastDetectResponse>> LastDetectWithHttpMessagesAsync(Request body, Dicti |
| | | 419 | | { |
| | 0 | 420 | | if (Endpoint == null) |
| | | 421 | | { |
| | 0 | 422 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | | 423 | | } |
| | 0 | 424 | | if (body == null) |
| | | 425 | | { |
| | 0 | 426 | | throw new ValidationException(ValidationRules.CannotBeNull, "body"); |
| | | 427 | | } |
| | 0 | 428 | | if (body != null) |
| | | 429 | | { |
| | 0 | 430 | | body.Validate(); |
| | | 431 | | } |
| | | 432 | | // Tracing |
| | 0 | 433 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 0 | 434 | | string _invocationId = null; |
| | 0 | 435 | | if (_shouldTrace) |
| | | 436 | | { |
| | 0 | 437 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 438 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 439 | | tracingParameters.Add("body", body); |
| | 0 | 440 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 441 | | ServiceClientTracing.Enter(_invocationId, this, "LastDetect", tracingParameters); |
| | | 442 | | } |
| | | 443 | | // Construct URL |
| | 0 | 444 | | var _baseUrl = BaseUri; |
| | 0 | 445 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "timeseries/last/detect"; |
| | 0 | 446 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | | 447 | | // Create HTTP transport objects |
| | 0 | 448 | | var _httpRequest = new HttpRequestMessage(); |
| | 0 | 449 | | HttpResponseMessage _httpResponse = null; |
| | 0 | 450 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 0 | 451 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 452 | | // Set Headers |
| | | 453 | | |
| | | 454 | | |
| | 0 | 455 | | if (customHeaders != null) |
| | | 456 | | { |
| | 0 | 457 | | foreach(var _header in customHeaders) |
| | | 458 | | { |
| | 0 | 459 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 460 | | { |
| | 0 | 461 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 462 | | } |
| | 0 | 463 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 464 | | } |
| | | 465 | | } |
| | | 466 | | |
| | | 467 | | // Serialize Request |
| | 0 | 468 | | string _requestContent = null; |
| | 0 | 469 | | if(body != null) |
| | | 470 | | { |
| | 0 | 471 | | _requestContent = SafeJsonConvert.SerializeObject(body, SerializationSettings); |
| | 0 | 472 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 0 | 473 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 474 | | } |
| | | 475 | | // Set Credentials |
| | 0 | 476 | | if (Credentials != null) |
| | | 477 | | { |
| | 0 | 478 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 479 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 480 | | } |
| | | 481 | | // Send Request |
| | 0 | 482 | | if (_shouldTrace) |
| | | 483 | | { |
| | 0 | 484 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 485 | | } |
| | 0 | 486 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 487 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 0 | 488 | | if (_shouldTrace) |
| | | 489 | | { |
| | 0 | 490 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 491 | | } |
| | 0 | 492 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 0 | 493 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 494 | | string _responseContent = null; |
| | 0 | 495 | | if ((int)_statusCode != 200) |
| | | 496 | | { |
| | 0 | 497 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | | 498 | | try |
| | | 499 | | { |
| | 0 | 500 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 501 | | APIError _errorBody = SafeJsonConvert.DeserializeObject<APIError>(_responseContent, Deserialization |
| | 0 | 502 | | if (_errorBody != null) |
| | | 503 | | { |
| | 0 | 504 | | ex.Body = _errorBody; |
| | | 505 | | } |
| | 0 | 506 | | } |
| | 0 | 507 | | catch (JsonException) |
| | | 508 | | { |
| | | 509 | | // Ignore the exception |
| | 0 | 510 | | } |
| | 0 | 511 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 512 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 513 | | if (_shouldTrace) |
| | | 514 | | { |
| | 0 | 515 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 516 | | } |
| | 0 | 517 | | _httpRequest.Dispose(); |
| | 0 | 518 | | if (_httpResponse != null) |
| | | 519 | | { |
| | 0 | 520 | | _httpResponse.Dispose(); |
| | | 521 | | } |
| | 0 | 522 | | throw ex; |
| | | 523 | | } |
| | | 524 | | // Create Result |
| | 0 | 525 | | var _result = new HttpOperationResponse<LastDetectResponse>(); |
| | 0 | 526 | | _result.Request = _httpRequest; |
| | 0 | 527 | | _result.Response = _httpResponse; |
| | | 528 | | // Deserialize Response |
| | 0 | 529 | | if ((int)_statusCode == 200) |
| | | 530 | | { |
| | 0 | 531 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 532 | | try |
| | | 533 | | { |
| | 0 | 534 | | _result.Body = SafeJsonConvert.DeserializeObject<LastDetectResponse>(_responseContent, Deserializati |
| | 0 | 535 | | } |
| | 0 | 536 | | catch (JsonException ex) |
| | | 537 | | { |
| | 0 | 538 | | _httpRequest.Dispose(); |
| | 0 | 539 | | if (_httpResponse != null) |
| | | 540 | | { |
| | 0 | 541 | | _httpResponse.Dispose(); |
| | | 542 | | } |
| | 0 | 543 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 544 | | } |
| | | 545 | | } |
| | 0 | 546 | | if (_shouldTrace) |
| | | 547 | | { |
| | 0 | 548 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 549 | | } |
| | 0 | 550 | | return _result; |
| | 0 | 551 | | } |
| | | 552 | | |
| | | 553 | | } |
| | | 554 | | } |