| | | 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.Personalizer |
| | | 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 | | /// Evaluations operations. |
| | | 27 | | /// </summary> |
| | | 28 | | public partial class Evaluations : IServiceOperations<PersonalizerClient>, IEvaluations |
| | | 29 | | { |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the Evaluations 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> |
| | 34 | 39 | | public Evaluations(PersonalizerClient client) |
| | | 40 | | { |
| | 34 | 41 | | if (client == null) |
| | | 42 | | { |
| | 0 | 43 | | throw new System.ArgumentNullException("client"); |
| | | 44 | | } |
| | 34 | 45 | | Client = client; |
| | 34 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets a reference to the PersonalizerClient |
| | | 50 | | /// </summary> |
| | 92 | 51 | | public PersonalizerClient Client { get; private set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Get Evaluation. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <remarks> |
| | | 57 | | /// Get the evaluation associated with the Id. |
| | | 58 | | /// </remarks> |
| | | 59 | | /// <param name='evaluationId'> |
| | | 60 | | /// Id of the evaluation. |
| | | 61 | | /// </param> |
| | | 62 | | /// <param name='customHeaders'> |
| | | 63 | | /// Headers that will be added to request. |
| | | 64 | | /// </param> |
| | | 65 | | /// <param name='cancellationToken'> |
| | | 66 | | /// The cancellation token. |
| | | 67 | | /// </param> |
| | | 68 | | /// <exception cref="ErrorResponseException"> |
| | | 69 | | /// Thrown when the operation returned an invalid status code |
| | | 70 | | /// </exception> |
| | | 71 | | /// <exception cref="SerializationException"> |
| | | 72 | | /// Thrown when unable to deserialize the response |
| | | 73 | | /// </exception> |
| | | 74 | | /// <exception cref="ValidationException"> |
| | | 75 | | /// Thrown when a required parameter is null |
| | | 76 | | /// </exception> |
| | | 77 | | /// <exception cref="System.ArgumentNullException"> |
| | | 78 | | /// Thrown when a required parameter is null |
| | | 79 | | /// </exception> |
| | | 80 | | /// <return> |
| | | 81 | | /// A response object containing the response body and response headers. |
| | | 82 | | /// </return> |
| | | 83 | | public async Task<HttpOperationResponse<Evaluation>> GetWithHttpMessagesAsync(string evaluationId, Dictionary<st |
| | | 84 | | { |
| | 2 | 85 | | if (Client.Endpoint == null) |
| | | 86 | | { |
| | 0 | 87 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 88 | | } |
| | 2 | 89 | | if (evaluationId == null) |
| | | 90 | | { |
| | 0 | 91 | | throw new ValidationException(ValidationRules.CannotBeNull, "evaluationId"); |
| | | 92 | | } |
| | 2 | 93 | | if (evaluationId != null) |
| | | 94 | | { |
| | 2 | 95 | | if (evaluationId.Length > 256) |
| | | 96 | | { |
| | 0 | 97 | | throw new ValidationException(ValidationRules.MaxLength, "evaluationId", 256); |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | // Tracing |
| | 2 | 101 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 2 | 102 | | string _invocationId = null; |
| | 2 | 103 | | if (_shouldTrace) |
| | | 104 | | { |
| | 0 | 105 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 106 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 107 | | tracingParameters.Add("evaluationId", evaluationId); |
| | 0 | 108 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 109 | | ServiceClientTracing.Enter(_invocationId, this, "Get", tracingParameters); |
| | | 110 | | } |
| | | 111 | | // Construct URL |
| | 2 | 112 | | var _baseUrl = Client.BaseUri; |
| | 2 | 113 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "evaluations/{evaluationId}"; |
| | 2 | 114 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | 2 | 115 | | _url = _url.Replace("{evaluationId}", System.Uri.EscapeDataString(evaluationId)); |
| | | 116 | | // Create HTTP transport objects |
| | 2 | 117 | | var _httpRequest = new HttpRequestMessage(); |
| | 2 | 118 | | HttpResponseMessage _httpResponse = null; |
| | 2 | 119 | | _httpRequest.Method = new HttpMethod("GET"); |
| | 2 | 120 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 121 | | // Set Headers |
| | | 122 | | |
| | | 123 | | |
| | 2 | 124 | | if (customHeaders != null) |
| | | 125 | | { |
| | 0 | 126 | | foreach(var _header in customHeaders) |
| | | 127 | | { |
| | 0 | 128 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 129 | | { |
| | 0 | 130 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 131 | | } |
| | 0 | 132 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | // Serialize Request |
| | 2 | 137 | | string _requestContent = null; |
| | | 138 | | // Set Credentials |
| | 2 | 139 | | if (Client.Credentials != null) |
| | | 140 | | { |
| | 2 | 141 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 142 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 143 | | } |
| | | 144 | | // Send Request |
| | 2 | 145 | | if (_shouldTrace) |
| | | 146 | | { |
| | 0 | 147 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 148 | | } |
| | 2 | 149 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 150 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2 | 151 | | if (_shouldTrace) |
| | | 152 | | { |
| | 0 | 153 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 154 | | } |
| | 2 | 155 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 2 | 156 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 157 | | string _responseContent = null; |
| | 2 | 158 | | if ((int)_statusCode != 200) |
| | | 159 | | { |
| | 0 | 160 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | | 161 | | try |
| | | 162 | | { |
| | 0 | 163 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 164 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| | 0 | 165 | | if (_errorBody != null) |
| | | 166 | | { |
| | 0 | 167 | | ex.Body = _errorBody; |
| | | 168 | | } |
| | 0 | 169 | | } |
| | 0 | 170 | | catch (JsonException) |
| | | 171 | | { |
| | | 172 | | // Ignore the exception |
| | 0 | 173 | | } |
| | 0 | 174 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 175 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 176 | | if (_shouldTrace) |
| | | 177 | | { |
| | 0 | 178 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 179 | | } |
| | 0 | 180 | | _httpRequest.Dispose(); |
| | 0 | 181 | | if (_httpResponse != null) |
| | | 182 | | { |
| | 0 | 183 | | _httpResponse.Dispose(); |
| | | 184 | | } |
| | 0 | 185 | | throw ex; |
| | | 186 | | } |
| | | 187 | | // Create Result |
| | 2 | 188 | | var _result = new HttpOperationResponse<Evaluation>(); |
| | 2 | 189 | | _result.Request = _httpRequest; |
| | 2 | 190 | | _result.Response = _httpResponse; |
| | | 191 | | // Deserialize Response |
| | 2 | 192 | | if ((int)_statusCode == 200) |
| | | 193 | | { |
| | 2 | 194 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 195 | | try |
| | | 196 | | { |
| | 2 | 197 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<Evaluation>(_responseContent, Cl |
| | 2 | 198 | | } |
| | 0 | 199 | | catch (JsonException ex) |
| | | 200 | | { |
| | 0 | 201 | | _httpRequest.Dispose(); |
| | 0 | 202 | | if (_httpResponse != null) |
| | | 203 | | { |
| | 0 | 204 | | _httpResponse.Dispose(); |
| | | 205 | | } |
| | 0 | 206 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 207 | | } |
| | | 208 | | } |
| | 2 | 209 | | if (_shouldTrace) |
| | | 210 | | { |
| | 0 | 211 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 212 | | } |
| | 2 | 213 | | return _result; |
| | 2 | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Delete Evaluation. |
| | | 218 | | /// </summary> |
| | | 219 | | /// <remarks> |
| | | 220 | | /// Delete the evaluation associated with the Id. |
| | | 221 | | /// </remarks> |
| | | 222 | | /// <param name='evaluationId'> |
| | | 223 | | /// Id of the evaluation to delete. |
| | | 224 | | /// </param> |
| | | 225 | | /// <param name='customHeaders'> |
| | | 226 | | /// Headers that will be added to request. |
| | | 227 | | /// </param> |
| | | 228 | | /// <param name='cancellationToken'> |
| | | 229 | | /// The cancellation token. |
| | | 230 | | /// </param> |
| | | 231 | | /// <exception cref="HttpOperationException"> |
| | | 232 | | /// Thrown when the operation returned an invalid status code |
| | | 233 | | /// </exception> |
| | | 234 | | /// <exception cref="ValidationException"> |
| | | 235 | | /// Thrown when a required parameter is null |
| | | 236 | | /// </exception> |
| | | 237 | | /// <exception cref="System.ArgumentNullException"> |
| | | 238 | | /// Thrown when a required parameter is null |
| | | 239 | | /// </exception> |
| | | 240 | | /// <return> |
| | | 241 | | /// A response object containing the response body and response headers. |
| | | 242 | | /// </return> |
| | | 243 | | public async Task<HttpOperationResponse> DeleteWithHttpMessagesAsync(string evaluationId, Dictionary<string, Lis |
| | | 244 | | { |
| | 2 | 245 | | if (Client.Endpoint == null) |
| | | 246 | | { |
| | 0 | 247 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 248 | | } |
| | 2 | 249 | | if (evaluationId == null) |
| | | 250 | | { |
| | 0 | 251 | | throw new ValidationException(ValidationRules.CannotBeNull, "evaluationId"); |
| | | 252 | | } |
| | 2 | 253 | | if (evaluationId != null) |
| | | 254 | | { |
| | 2 | 255 | | if (evaluationId.Length > 256) |
| | | 256 | | { |
| | 0 | 257 | | throw new ValidationException(ValidationRules.MaxLength, "evaluationId", 256); |
| | | 258 | | } |
| | | 259 | | } |
| | | 260 | | // Tracing |
| | 2 | 261 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 2 | 262 | | string _invocationId = null; |
| | 2 | 263 | | if (_shouldTrace) |
| | | 264 | | { |
| | 0 | 265 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 266 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 267 | | tracingParameters.Add("evaluationId", evaluationId); |
| | 0 | 268 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 269 | | ServiceClientTracing.Enter(_invocationId, this, "Delete", tracingParameters); |
| | | 270 | | } |
| | | 271 | | // Construct URL |
| | 2 | 272 | | var _baseUrl = Client.BaseUri; |
| | 2 | 273 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "evaluations/{evaluationId}"; |
| | 2 | 274 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | 2 | 275 | | _url = _url.Replace("{evaluationId}", System.Uri.EscapeDataString(evaluationId)); |
| | | 276 | | // Create HTTP transport objects |
| | 2 | 277 | | var _httpRequest = new HttpRequestMessage(); |
| | 2 | 278 | | HttpResponseMessage _httpResponse = null; |
| | 2 | 279 | | _httpRequest.Method = new HttpMethod("DELETE"); |
| | 2 | 280 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 281 | | // Set Headers |
| | | 282 | | |
| | | 283 | | |
| | 2 | 284 | | if (customHeaders != null) |
| | | 285 | | { |
| | 0 | 286 | | foreach(var _header in customHeaders) |
| | | 287 | | { |
| | 0 | 288 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 289 | | { |
| | 0 | 290 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 291 | | } |
| | 0 | 292 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 293 | | } |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | // Serialize Request |
| | 2 | 297 | | string _requestContent = null; |
| | | 298 | | // Set Credentials |
| | 2 | 299 | | if (Client.Credentials != null) |
| | | 300 | | { |
| | 2 | 301 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 302 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 303 | | } |
| | | 304 | | // Send Request |
| | 2 | 305 | | if (_shouldTrace) |
| | | 306 | | { |
| | 0 | 307 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 308 | | } |
| | 2 | 309 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 310 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2 | 311 | | if (_shouldTrace) |
| | | 312 | | { |
| | 0 | 313 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 314 | | } |
| | 2 | 315 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 2 | 316 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 317 | | string _responseContent = null; |
| | 2 | 318 | | if ((int)_statusCode != 204) |
| | | 319 | | { |
| | 0 | 320 | | var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 0 | 321 | | if (_httpResponse.Content != null) { |
| | 0 | 322 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 323 | | } |
| | | 324 | | else { |
| | 0 | 325 | | _responseContent = string.Empty; |
| | | 326 | | } |
| | 0 | 327 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 328 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 329 | | if (_shouldTrace) |
| | | 330 | | { |
| | 0 | 331 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 332 | | } |
| | 0 | 333 | | _httpRequest.Dispose(); |
| | 0 | 334 | | if (_httpResponse != null) |
| | | 335 | | { |
| | 0 | 336 | | _httpResponse.Dispose(); |
| | | 337 | | } |
| | 0 | 338 | | throw ex; |
| | | 339 | | } |
| | | 340 | | // Create Result |
| | 2 | 341 | | var _result = new HttpOperationResponse(); |
| | 2 | 342 | | _result.Request = _httpRequest; |
| | 2 | 343 | | _result.Response = _httpResponse; |
| | 2 | 344 | | if (_shouldTrace) |
| | | 345 | | { |
| | 0 | 346 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 347 | | } |
| | 2 | 348 | | return _result; |
| | 2 | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// List Evaluations. |
| | | 353 | | /// </summary> |
| | | 354 | | /// <remarks> |
| | | 355 | | /// List all the submitted evaluations. |
| | | 356 | | /// </remarks> |
| | | 357 | | /// <param name='customHeaders'> |
| | | 358 | | /// Headers that will be added to request. |
| | | 359 | | /// </param> |
| | | 360 | | /// <param name='cancellationToken'> |
| | | 361 | | /// The cancellation token. |
| | | 362 | | /// </param> |
| | | 363 | | /// <exception cref="HttpOperationException"> |
| | | 364 | | /// Thrown when the operation returned an invalid status code |
| | | 365 | | /// </exception> |
| | | 366 | | /// <exception cref="SerializationException"> |
| | | 367 | | /// Thrown when unable to deserialize the response |
| | | 368 | | /// </exception> |
| | | 369 | | /// <exception cref="ValidationException"> |
| | | 370 | | /// Thrown when a required parameter is null |
| | | 371 | | /// </exception> |
| | | 372 | | /// <exception cref="System.ArgumentNullException"> |
| | | 373 | | /// Thrown when a required parameter is null |
| | | 374 | | /// </exception> |
| | | 375 | | /// <return> |
| | | 376 | | /// A response object containing the response body and response headers. |
| | | 377 | | /// </return> |
| | | 378 | | public async Task<HttpOperationResponse<IList<Evaluation>>> ListWithHttpMessagesAsync(Dictionary<string, List<st |
| | | 379 | | { |
| | 2 | 380 | | if (Client.Endpoint == null) |
| | | 381 | | { |
| | 0 | 382 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 383 | | } |
| | | 384 | | // Tracing |
| | 2 | 385 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 2 | 386 | | string _invocationId = null; |
| | 2 | 387 | | if (_shouldTrace) |
| | | 388 | | { |
| | 0 | 389 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 390 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 391 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 392 | | ServiceClientTracing.Enter(_invocationId, this, "List", tracingParameters); |
| | | 393 | | } |
| | | 394 | | // Construct URL |
| | 2 | 395 | | var _baseUrl = Client.BaseUri; |
| | 2 | 396 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "evaluations"; |
| | 2 | 397 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | | 398 | | // Create HTTP transport objects |
| | 2 | 399 | | var _httpRequest = new HttpRequestMessage(); |
| | 2 | 400 | | HttpResponseMessage _httpResponse = null; |
| | 2 | 401 | | _httpRequest.Method = new HttpMethod("GET"); |
| | 2 | 402 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 403 | | // Set Headers |
| | | 404 | | |
| | | 405 | | |
| | 2 | 406 | | if (customHeaders != null) |
| | | 407 | | { |
| | 0 | 408 | | foreach(var _header in customHeaders) |
| | | 409 | | { |
| | 0 | 410 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 411 | | { |
| | 0 | 412 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 413 | | } |
| | 0 | 414 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 415 | | } |
| | | 416 | | } |
| | | 417 | | |
| | | 418 | | // Serialize Request |
| | 2 | 419 | | string _requestContent = null; |
| | | 420 | | // Set Credentials |
| | 2 | 421 | | if (Client.Credentials != null) |
| | | 422 | | { |
| | 2 | 423 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 424 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 425 | | } |
| | | 426 | | // Send Request |
| | 2 | 427 | | if (_shouldTrace) |
| | | 428 | | { |
| | 0 | 429 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 430 | | } |
| | 2 | 431 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 432 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2 | 433 | | if (_shouldTrace) |
| | | 434 | | { |
| | 0 | 435 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 436 | | } |
| | 2 | 437 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 2 | 438 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 439 | | string _responseContent = null; |
| | 2 | 440 | | if ((int)_statusCode != 200) |
| | | 441 | | { |
| | 0 | 442 | | var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 0 | 443 | | if (_httpResponse.Content != null) { |
| | 0 | 444 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 445 | | } |
| | | 446 | | else { |
| | 0 | 447 | | _responseContent = string.Empty; |
| | | 448 | | } |
| | 0 | 449 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 450 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 451 | | if (_shouldTrace) |
| | | 452 | | { |
| | 0 | 453 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 454 | | } |
| | 0 | 455 | | _httpRequest.Dispose(); |
| | 0 | 456 | | if (_httpResponse != null) |
| | | 457 | | { |
| | 0 | 458 | | _httpResponse.Dispose(); |
| | | 459 | | } |
| | 0 | 460 | | throw ex; |
| | | 461 | | } |
| | | 462 | | // Create Result |
| | 2 | 463 | | var _result = new HttpOperationResponse<IList<Evaluation>>(); |
| | 2 | 464 | | _result.Request = _httpRequest; |
| | 2 | 465 | | _result.Response = _httpResponse; |
| | | 466 | | // Deserialize Response |
| | 2 | 467 | | if ((int)_statusCode == 200) |
| | | 468 | | { |
| | 2 | 469 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 470 | | try |
| | | 471 | | { |
| | 2 | 472 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<IList<Evaluation>>(_responseCont |
| | 2 | 473 | | } |
| | 0 | 474 | | catch (JsonException ex) |
| | | 475 | | { |
| | 0 | 476 | | _httpRequest.Dispose(); |
| | 0 | 477 | | if (_httpResponse != null) |
| | | 478 | | { |
| | 0 | 479 | | _httpResponse.Dispose(); |
| | | 480 | | } |
| | 0 | 481 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 482 | | } |
| | | 483 | | } |
| | 2 | 484 | | if (_shouldTrace) |
| | | 485 | | { |
| | 0 | 486 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 487 | | } |
| | 2 | 488 | | return _result; |
| | 2 | 489 | | } |
| | | 490 | | |
| | | 491 | | /// <summary> |
| | | 492 | | /// Create Evaluation. |
| | | 493 | | /// </summary> |
| | | 494 | | /// <remarks> |
| | | 495 | | /// Submit a new evaluation job. |
| | | 496 | | /// </remarks> |
| | | 497 | | /// <param name='evaluation'> |
| | | 498 | | /// The evaluation job definition. |
| | | 499 | | /// </param> |
| | | 500 | | /// <param name='customHeaders'> |
| | | 501 | | /// Headers that will be added to request. |
| | | 502 | | /// </param> |
| | | 503 | | /// <param name='cancellationToken'> |
| | | 504 | | /// The cancellation token. |
| | | 505 | | /// </param> |
| | | 506 | | /// <exception cref="ErrorResponseException"> |
| | | 507 | | /// Thrown when the operation returned an invalid status code |
| | | 508 | | /// </exception> |
| | | 509 | | /// <exception cref="SerializationException"> |
| | | 510 | | /// Thrown when unable to deserialize the response |
| | | 511 | | /// </exception> |
| | | 512 | | /// <exception cref="ValidationException"> |
| | | 513 | | /// Thrown when a required parameter is null |
| | | 514 | | /// </exception> |
| | | 515 | | /// <exception cref="System.ArgumentNullException"> |
| | | 516 | | /// Thrown when a required parameter is null |
| | | 517 | | /// </exception> |
| | | 518 | | /// <return> |
| | | 519 | | /// A response object containing the response body and response headers. |
| | | 520 | | /// </return> |
| | | 521 | | public async Task<HttpOperationResponse<Evaluation,EvaluationsCreateHeaders>> CreateWithHttpMessagesAsync(Evalua |
| | | 522 | | { |
| | 2 | 523 | | if (Client.Endpoint == null) |
| | | 524 | | { |
| | 0 | 525 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 526 | | } |
| | 2 | 527 | | if (evaluation == null) |
| | | 528 | | { |
| | 0 | 529 | | throw new ValidationException(ValidationRules.CannotBeNull, "evaluation"); |
| | | 530 | | } |
| | 2 | 531 | | if (evaluation != null) |
| | | 532 | | { |
| | 2 | 533 | | evaluation.Validate(); |
| | | 534 | | } |
| | | 535 | | // Tracing |
| | 2 | 536 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 2 | 537 | | string _invocationId = null; |
| | 2 | 538 | | if (_shouldTrace) |
| | | 539 | | { |
| | 0 | 540 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 541 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 542 | | tracingParameters.Add("evaluation", evaluation); |
| | 0 | 543 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 544 | | ServiceClientTracing.Enter(_invocationId, this, "Create", tracingParameters); |
| | | 545 | | } |
| | | 546 | | // Construct URL |
| | 2 | 547 | | var _baseUrl = Client.BaseUri; |
| | 2 | 548 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "evaluations"; |
| | 2 | 549 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | | 550 | | // Create HTTP transport objects |
| | 2 | 551 | | var _httpRequest = new HttpRequestMessage(); |
| | 2 | 552 | | HttpResponseMessage _httpResponse = null; |
| | 2 | 553 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 2 | 554 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 555 | | // Set Headers |
| | | 556 | | |
| | | 557 | | |
| | 2 | 558 | | if (customHeaders != null) |
| | | 559 | | { |
| | 0 | 560 | | foreach(var _header in customHeaders) |
| | | 561 | | { |
| | 0 | 562 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 563 | | { |
| | 0 | 564 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 565 | | } |
| | 0 | 566 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 567 | | } |
| | | 568 | | } |
| | | 569 | | |
| | | 570 | | // Serialize Request |
| | 2 | 571 | | string _requestContent = null; |
| | 2 | 572 | | if(evaluation != null) |
| | | 573 | | { |
| | 2 | 574 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(evaluation, Client.SerializationSet |
| | 2 | 575 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 2 | 576 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 577 | | } |
| | | 578 | | // Set Credentials |
| | 2 | 579 | | if (Client.Credentials != null) |
| | | 580 | | { |
| | 2 | 581 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 582 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 583 | | } |
| | | 584 | | // Send Request |
| | 2 | 585 | | if (_shouldTrace) |
| | | 586 | | { |
| | 0 | 587 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 588 | | } |
| | 2 | 589 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 590 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2 | 591 | | if (_shouldTrace) |
| | | 592 | | { |
| | 0 | 593 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 594 | | } |
| | 2 | 595 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 2 | 596 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 597 | | string _responseContent = null; |
| | 2 | 598 | | if ((int)_statusCode != 201) |
| | | 599 | | { |
| | 0 | 600 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | | 601 | | try |
| | | 602 | | { |
| | 0 | 603 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 604 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| | 0 | 605 | | if (_errorBody != null) |
| | | 606 | | { |
| | 0 | 607 | | ex.Body = _errorBody; |
| | | 608 | | } |
| | 0 | 609 | | } |
| | 0 | 610 | | catch (JsonException) |
| | | 611 | | { |
| | | 612 | | // Ignore the exception |
| | 0 | 613 | | } |
| | 0 | 614 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 615 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 616 | | if (_shouldTrace) |
| | | 617 | | { |
| | 0 | 618 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 619 | | } |
| | 0 | 620 | | _httpRequest.Dispose(); |
| | 0 | 621 | | if (_httpResponse != null) |
| | | 622 | | { |
| | 0 | 623 | | _httpResponse.Dispose(); |
| | | 624 | | } |
| | 0 | 625 | | throw ex; |
| | | 626 | | } |
| | | 627 | | // Create Result |
| | 2 | 628 | | var _result = new HttpOperationResponse<Evaluation,EvaluationsCreateHeaders>(); |
| | 2 | 629 | | _result.Request = _httpRequest; |
| | 2 | 630 | | _result.Response = _httpResponse; |
| | | 631 | | // Deserialize Response |
| | 2 | 632 | | if ((int)_statusCode == 201) |
| | | 633 | | { |
| | 2 | 634 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 635 | | try |
| | | 636 | | { |
| | 2 | 637 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<Evaluation>(_responseContent, Cl |
| | 2 | 638 | | } |
| | 0 | 639 | | catch (JsonException ex) |
| | | 640 | | { |
| | 0 | 641 | | _httpRequest.Dispose(); |
| | 0 | 642 | | if (_httpResponse != null) |
| | | 643 | | { |
| | 0 | 644 | | _httpResponse.Dispose(); |
| | | 645 | | } |
| | 0 | 646 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 647 | | } |
| | | 648 | | } |
| | | 649 | | try |
| | | 650 | | { |
| | 2 | 651 | | _result.Headers = _httpResponse.GetHeadersAsJson().ToObject<EvaluationsCreateHeaders>(JsonSerializer.Cre |
| | 2 | 652 | | } |
| | 0 | 653 | | catch (JsonException ex) |
| | | 654 | | { |
| | 0 | 655 | | _httpRequest.Dispose(); |
| | 0 | 656 | | if (_httpResponse != null) |
| | | 657 | | { |
| | 0 | 658 | | _httpResponse.Dispose(); |
| | | 659 | | } |
| | 0 | 660 | | throw new SerializationException("Unable to deserialize the headers.", _httpResponse.GetHeadersAsJson(). |
| | | 661 | | } |
| | 2 | 662 | | if (_shouldTrace) |
| | | 663 | | { |
| | 0 | 664 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 665 | | } |
| | 2 | 666 | | return _result; |
| | 2 | 667 | | } |
| | | 668 | | |
| | | 669 | | } |
| | | 670 | | } |