| | 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.FormRecognizer |
| | 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.IO; |
| | 20 | | using System.Net; |
| | 21 | | using System.Net.Http; |
| | 22 | | using System.Net.Http.Headers; |
| | 23 | | using System.Threading; |
| | 24 | | using System.Threading.Tasks; |
| | 25 | |
|
| | 26 | | public partial class FormRecognizerClient : ServiceClient<FormRecognizerClient>, IFormRecognizerClient |
| | 27 | | { |
| | 28 | | /// <summary> |
| | 29 | | /// The base URI of the service. |
| | 30 | | /// </summary> |
| 0 | 31 | | internal string BaseUri {get; set;} |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Gets or sets json serialization settings. |
| | 35 | | /// </summary> |
| 6 | 36 | | public JsonSerializerSettings SerializationSettings { get; private set; } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets or sets json deserialization settings. |
| | 40 | | /// </summary> |
| 6 | 41 | | public JsonSerializerSettings DeserializationSettings { get; private set; } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Supported Cognitive Services endpoints (protocol and hostname, for example: |
| | 45 | | /// https://westus2.api.cognitive.microsoft.com). |
| | 46 | | /// </summary> |
| 0 | 47 | | public string Endpoint { get; set; } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Subscription credentials which uniquely identify client subscription. |
| | 51 | | /// </summary> |
| 6 | 52 | | public ServiceClientCredentials Credentials { get; private set; } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Initializes a new instance of the FormRecognizerClient class. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name='httpClient'> |
| | 58 | | /// HttpClient to be used |
| | 59 | | /// </param> |
| | 60 | | /// <param name='disposeHttpClient'> |
| | 61 | | /// True: will dispose the provided httpClient on calling FormRecognizerClient.Dispose(). False: will not dispos |
| 0 | 62 | | protected FormRecognizerClient(HttpClient httpClient, bool disposeHttpClient) : base(httpClient, disposeHttpClie |
| | 63 | | { |
| 0 | 64 | | Initialize(); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Initializes a new instance of the FormRecognizerClient class. |
| | 69 | | /// </summary> |
| | 70 | | /// <param name='handlers'> |
| | 71 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 72 | | /// </param> |
| 2 | 73 | | protected FormRecognizerClient(params DelegatingHandler[] handlers) : base(handlers) |
| | 74 | | { |
| 2 | 75 | | Initialize(); |
| 2 | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Initializes a new instance of the FormRecognizerClient class. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name='rootHandler'> |
| | 82 | | /// Optional. The http client handler used to handle http transport. |
| | 83 | | /// </param> |
| | 84 | | /// <param name='handlers'> |
| | 85 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 86 | | /// </param> |
| 0 | 87 | | protected FormRecognizerClient(HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : base(rootHa |
| | 88 | | { |
| 0 | 89 | | Initialize(); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Initializes a new instance of the FormRecognizerClient class. |
| | 94 | | /// </summary> |
| | 95 | | /// <param name='credentials'> |
| | 96 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | 97 | | /// </param> |
| | 98 | | /// <param name='handlers'> |
| | 99 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 100 | | /// </param> |
| | 101 | | /// <exception cref="System.ArgumentNullException"> |
| | 102 | | /// Thrown when a required parameter is null |
| | 103 | | /// </exception> |
| 2 | 104 | | public FormRecognizerClient(ServiceClientCredentials credentials, params DelegatingHandler[] handlers) : this(ha |
| | 105 | | { |
| 2 | 106 | | if (credentials == null) |
| | 107 | | { |
| 0 | 108 | | throw new System.ArgumentNullException("credentials"); |
| | 109 | | } |
| 2 | 110 | | Credentials = credentials; |
| 2 | 111 | | if (Credentials != null) |
| | 112 | | { |
| 2 | 113 | | Credentials.InitializeServiceClient(this); |
| | 114 | | } |
| 2 | 115 | | } |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Initializes a new instance of the FormRecognizerClient class. |
| | 119 | | /// </summary> |
| | 120 | | /// <param name='credentials'> |
| | 121 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | 122 | | /// </param> |
| | 123 | | /// <param name='httpClient'> |
| | 124 | | /// HttpClient to be used |
| | 125 | | /// </param> |
| | 126 | | /// <param name='disposeHttpClient'> |
| | 127 | | /// True: will dispose the provided httpClient on calling FormRecognizerClient.Dispose(). False: will not dispos |
| | 128 | | /// <exception cref="System.ArgumentNullException"> |
| | 129 | | /// Thrown when a required parameter is null |
| | 130 | | /// </exception> |
| 0 | 131 | | public FormRecognizerClient(ServiceClientCredentials credentials, HttpClient httpClient, bool disposeHttpClient) |
| | 132 | | { |
| 0 | 133 | | if (credentials == null) |
| | 134 | | { |
| 0 | 135 | | throw new System.ArgumentNullException("credentials"); |
| | 136 | | } |
| 0 | 137 | | Credentials = credentials; |
| 0 | 138 | | if (Credentials != null) |
| | 139 | | { |
| 0 | 140 | | Credentials.InitializeServiceClient(this); |
| | 141 | | } |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// Initializes a new instance of the FormRecognizerClient class. |
| | 146 | | /// </summary> |
| | 147 | | /// <param name='credentials'> |
| | 148 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | 149 | | /// </param> |
| | 150 | | /// <param name='rootHandler'> |
| | 151 | | /// Optional. The http client handler used to handle http transport. |
| | 152 | | /// </param> |
| | 153 | | /// <param name='handlers'> |
| | 154 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 155 | | /// </param> |
| | 156 | | /// <exception cref="System.ArgumentNullException"> |
| | 157 | | /// Thrown when a required parameter is null |
| | 158 | | /// </exception> |
| 0 | 159 | | public FormRecognizerClient(ServiceClientCredentials credentials, HttpClientHandler rootHandler, params Delegati |
| | 160 | | { |
| 0 | 161 | | if (credentials == null) |
| | 162 | | { |
| 0 | 163 | | throw new System.ArgumentNullException("credentials"); |
| | 164 | | } |
| 0 | 165 | | Credentials = credentials; |
| 0 | 166 | | if (Credentials != null) |
| | 167 | | { |
| 0 | 168 | | Credentials.InitializeServiceClient(this); |
| | 169 | | } |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | /// <summary> |
| | 173 | | /// An optional partial-method to perform custom initialization. |
| | 174 | | ///</summary> |
| | 175 | | partial void CustomInitialize(); |
| | 176 | | /// <summary> |
| | 177 | | /// Initializes client properties. |
| | 178 | | /// </summary> |
| | 179 | | private void Initialize() |
| | 180 | | { |
| 2 | 181 | | BaseUri = "{Endpoint}/formrecognizer/v1.0-preview"; |
| 2 | 182 | | SerializationSettings = new JsonSerializerSettings |
| 2 | 183 | | { |
| 2 | 184 | | Formatting = Newtonsoft.Json.Formatting.Indented, |
| 2 | 185 | | DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, |
| 2 | 186 | | DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, |
| 2 | 187 | | NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, |
| 2 | 188 | | ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, |
| 2 | 189 | | ContractResolver = new ReadOnlyJsonContractResolver(), |
| 2 | 190 | | Converters = new List<JsonConverter> |
| 2 | 191 | | { |
| 2 | 192 | | new Iso8601TimeSpanConverter() |
| 2 | 193 | | } |
| 2 | 194 | | }; |
| 2 | 195 | | DeserializationSettings = new JsonSerializerSettings |
| 2 | 196 | | { |
| 2 | 197 | | DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, |
| 2 | 198 | | DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, |
| 2 | 199 | | NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, |
| 2 | 200 | | ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, |
| 2 | 201 | | ContractResolver = new ReadOnlyJsonContractResolver(), |
| 2 | 202 | | Converters = new List<JsonConverter> |
| 2 | 203 | | { |
| 2 | 204 | | new Iso8601TimeSpanConverter() |
| 2 | 205 | | } |
| 2 | 206 | | }; |
| 2 | 207 | | SerializationSettings.Converters.Add(new PolymorphicSerializeJsonConverter<FieldValue>("valueType")); |
| 2 | 208 | | DeserializationSettings.Converters.Add(new PolymorphicDeserializeJsonConverter<FieldValue>("valueType")); |
| 2 | 209 | | CustomInitialize(); |
| 2 | 210 | | } |
| | 211 | | /// <summary> |
| | 212 | | /// Train Model |
| | 213 | | /// </summary> |
| | 214 | | /// <remarks> |
| | 215 | | /// Create and train a custom model. The train request must include a source |
| | 216 | | /// parameter that is either an externally accessible Azure Storage blob |
| | 217 | | /// container Uri (preferably a Shared Access Signature Uri) or valid path to a |
| | 218 | | /// data folder in a locally mounted drive. When local paths are specified, |
| | 219 | | /// they must follow the Linux/Unix path format and be an absolute path rooted |
| | 220 | | /// to the input mount configuration |
| | 221 | | /// setting value e.g., if '{Mounts:Input}' configuration setting value is |
| | 222 | | /// '/input' then a valid source path would be '/input/contosodataset'. All |
| | 223 | | /// data to be trained is expected to be directly under the source folder. |
| | 224 | | /// Subfolders are not supported. Models are trained using documents that are |
| | 225 | | /// of the following content type - 'application/pdf', 'image/jpeg' and |
| | 226 | | /// 'image/png'." |
| | 227 | | /// Other type of content is ignored. |
| | 228 | | /// </remarks> |
| | 229 | | /// <param name='trainRequest'> |
| | 230 | | /// Request object for training. |
| | 231 | | /// </param> |
| | 232 | | /// <param name='customHeaders'> |
| | 233 | | /// Headers that will be added to request. |
| | 234 | | /// </param> |
| | 235 | | /// <param name='cancellationToken'> |
| | 236 | | /// The cancellation token. |
| | 237 | | /// </param> |
| | 238 | | /// <exception cref="ErrorResponseException"> |
| | 239 | | /// Thrown when the operation returned an invalid status code |
| | 240 | | /// </exception> |
| | 241 | | /// <exception cref="SerializationException"> |
| | 242 | | /// Thrown when unable to deserialize the response |
| | 243 | | /// </exception> |
| | 244 | | /// <exception cref="ValidationException"> |
| | 245 | | /// Thrown when a required parameter is null |
| | 246 | | /// </exception> |
| | 247 | | /// <exception cref="System.ArgumentNullException"> |
| | 248 | | /// Thrown when a required parameter is null |
| | 249 | | /// </exception> |
| | 250 | | /// <return> |
| | 251 | | /// A response object containing the response body and response headers. |
| | 252 | | /// </return> |
| | 253 | | public async Task<HttpOperationResponse<TrainResult>> TrainCustomModelWithHttpMessagesAsync(TrainRequest trainRe |
| | 254 | | { |
| 0 | 255 | | if (Endpoint == null) |
| | 256 | | { |
| 0 | 257 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 258 | | } |
| 0 | 259 | | if (trainRequest == null) |
| | 260 | | { |
| 0 | 261 | | throw new ValidationException(ValidationRules.CannotBeNull, "trainRequest"); |
| | 262 | | } |
| 0 | 263 | | if (trainRequest != null) |
| | 264 | | { |
| 0 | 265 | | trainRequest.Validate(); |
| | 266 | | } |
| | 267 | | // Tracing |
| 0 | 268 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 269 | | string _invocationId = null; |
| 0 | 270 | | if (_shouldTrace) |
| | 271 | | { |
| 0 | 272 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 273 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 274 | | tracingParameters.Add("trainRequest", trainRequest); |
| 0 | 275 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 276 | | ServiceClientTracing.Enter(_invocationId, this, "TrainCustomModel", tracingParameters); |
| | 277 | | } |
| | 278 | | // Construct URL |
| 0 | 279 | | var _baseUrl = BaseUri; |
| 0 | 280 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "custom/train"; |
| 0 | 281 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | 282 | | // Create HTTP transport objects |
| 0 | 283 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 284 | | HttpResponseMessage _httpResponse = null; |
| 0 | 285 | | _httpRequest.Method = new HttpMethod("POST"); |
| 0 | 286 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 287 | | // Set Headers |
| | 288 | |
|
| | 289 | |
|
| 0 | 290 | | if (customHeaders != null) |
| | 291 | | { |
| 0 | 292 | | foreach(var _header in customHeaders) |
| | 293 | | { |
| 0 | 294 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 295 | | { |
| 0 | 296 | | _httpRequest.Headers.Remove(_header.Key); |
| | 297 | | } |
| 0 | 298 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 299 | | } |
| | 300 | | } |
| | 301 | |
|
| | 302 | | // Serialize Request |
| 0 | 303 | | string _requestContent = null; |
| 0 | 304 | | if(trainRequest != null) |
| | 305 | | { |
| 0 | 306 | | _requestContent = SafeJsonConvert.SerializeObject(trainRequest, SerializationSettings); |
| 0 | 307 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 0 | 308 | | _httpRequest.Content.Headers.ContentType =MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); |
| | 309 | | } |
| | 310 | | // Set Credentials |
| 0 | 311 | | if (Credentials != null) |
| | 312 | | { |
| 0 | 313 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 314 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 315 | | } |
| | 316 | | // Send Request |
| 0 | 317 | | if (_shouldTrace) |
| | 318 | | { |
| 0 | 319 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 320 | | } |
| 0 | 321 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 322 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 323 | | if (_shouldTrace) |
| | 324 | | { |
| 0 | 325 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 326 | | } |
| 0 | 327 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 328 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 329 | | string _responseContent = null; |
| 0 | 330 | | if ((int)_statusCode != 200) |
| | 331 | | { |
| 0 | 332 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 333 | | try |
| | 334 | | { |
| 0 | 335 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 336 | | ErrorResponse _errorBody = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, Deser |
| 0 | 337 | | if (_errorBody != null) |
| | 338 | | { |
| 0 | 339 | | ex.Body = _errorBody; |
| | 340 | | } |
| 0 | 341 | | } |
| 0 | 342 | | catch (JsonException) |
| | 343 | | { |
| | 344 | | // Ignore the exception |
| 0 | 345 | | } |
| 0 | 346 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 347 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 348 | | if (_shouldTrace) |
| | 349 | | { |
| 0 | 350 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 351 | | } |
| 0 | 352 | | _httpRequest.Dispose(); |
| 0 | 353 | | if (_httpResponse != null) |
| | 354 | | { |
| 0 | 355 | | _httpResponse.Dispose(); |
| | 356 | | } |
| 0 | 357 | | throw ex; |
| | 358 | | } |
| | 359 | | // Create Result |
| 0 | 360 | | var _result = new HttpOperationResponse<TrainResult>(); |
| 0 | 361 | | _result.Request = _httpRequest; |
| 0 | 362 | | _result.Response = _httpResponse; |
| | 363 | | // Deserialize Response |
| 0 | 364 | | if ((int)_statusCode == 200) |
| | 365 | | { |
| 0 | 366 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 367 | | try |
| | 368 | | { |
| 0 | 369 | | _result.Body = SafeJsonConvert.DeserializeObject<TrainResult>(_responseContent, DeserializationSetti |
| 0 | 370 | | } |
| 0 | 371 | | catch (JsonException ex) |
| | 372 | | { |
| 0 | 373 | | _httpRequest.Dispose(); |
| 0 | 374 | | if (_httpResponse != null) |
| | 375 | | { |
| 0 | 376 | | _httpResponse.Dispose(); |
| | 377 | | } |
| 0 | 378 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 379 | | } |
| | 380 | | } |
| 0 | 381 | | if (_shouldTrace) |
| | 382 | | { |
| 0 | 383 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 384 | | } |
| 0 | 385 | | return _result; |
| 0 | 386 | | } |
| | 387 | |
|
| | 388 | | /// <summary> |
| | 389 | | /// Get Keys |
| | 390 | | /// </summary> |
| | 391 | | /// <remarks> |
| | 392 | | /// Retrieve the keys that were |
| | 393 | | /// extracted during the training of the specified model. |
| | 394 | | /// </remarks> |
| | 395 | | /// <param name='id'> |
| | 396 | | /// Model identifier. |
| | 397 | | /// </param> |
| | 398 | | /// <param name='customHeaders'> |
| | 399 | | /// Headers that will be added to request. |
| | 400 | | /// </param> |
| | 401 | | /// <param name='cancellationToken'> |
| | 402 | | /// The cancellation token. |
| | 403 | | /// </param> |
| | 404 | | /// <exception cref="ErrorResponseException"> |
| | 405 | | /// Thrown when the operation returned an invalid status code |
| | 406 | | /// </exception> |
| | 407 | | /// <exception cref="SerializationException"> |
| | 408 | | /// Thrown when unable to deserialize the response |
| | 409 | | /// </exception> |
| | 410 | | /// <exception cref="ValidationException"> |
| | 411 | | /// Thrown when a required parameter is null |
| | 412 | | /// </exception> |
| | 413 | | /// <exception cref="System.ArgumentNullException"> |
| | 414 | | /// Thrown when a required parameter is null |
| | 415 | | /// </exception> |
| | 416 | | /// <return> |
| | 417 | | /// A response object containing the response body and response headers. |
| | 418 | | /// </return> |
| | 419 | | public async Task<HttpOperationResponse<KeysResult>> GetExtractedKeysWithHttpMessagesAsync(System.Guid id, Dicti |
| | 420 | | { |
| 0 | 421 | | if (Endpoint == null) |
| | 422 | | { |
| 0 | 423 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 424 | | } |
| | 425 | | // Tracing |
| 0 | 426 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 427 | | string _invocationId = null; |
| 0 | 428 | | if (_shouldTrace) |
| | 429 | | { |
| 0 | 430 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 431 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 432 | | tracingParameters.Add("id", id); |
| 0 | 433 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 434 | | ServiceClientTracing.Enter(_invocationId, this, "GetExtractedKeys", tracingParameters); |
| | 435 | | } |
| | 436 | | // Construct URL |
| 0 | 437 | | var _baseUrl = BaseUri; |
| 0 | 438 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "custom/models/{id}/keys"; |
| 0 | 439 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 0 | 440 | | _url = _url.Replace("{id}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject(id, SerializationSet |
| | 441 | | // Create HTTP transport objects |
| 0 | 442 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 443 | | HttpResponseMessage _httpResponse = null; |
| 0 | 444 | | _httpRequest.Method = new HttpMethod("GET"); |
| 0 | 445 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 446 | | // Set Headers |
| | 447 | |
|
| | 448 | |
|
| 0 | 449 | | if (customHeaders != null) |
| | 450 | | { |
| 0 | 451 | | foreach(var _header in customHeaders) |
| | 452 | | { |
| 0 | 453 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 454 | | { |
| 0 | 455 | | _httpRequest.Headers.Remove(_header.Key); |
| | 456 | | } |
| 0 | 457 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 458 | | } |
| | 459 | | } |
| | 460 | |
|
| | 461 | | // Serialize Request |
| 0 | 462 | | string _requestContent = null; |
| | 463 | | // Set Credentials |
| 0 | 464 | | if (Credentials != null) |
| | 465 | | { |
| 0 | 466 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 467 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 468 | | } |
| | 469 | | // Send Request |
| 0 | 470 | | if (_shouldTrace) |
| | 471 | | { |
| 0 | 472 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 473 | | } |
| 0 | 474 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 475 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 476 | | if (_shouldTrace) |
| | 477 | | { |
| 0 | 478 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 479 | | } |
| 0 | 480 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 481 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 482 | | string _responseContent = null; |
| 0 | 483 | | if ((int)_statusCode != 200) |
| | 484 | | { |
| 0 | 485 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 486 | | try |
| | 487 | | { |
| 0 | 488 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 489 | | ErrorResponse _errorBody = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, Deser |
| 0 | 490 | | if (_errorBody != null) |
| | 491 | | { |
| 0 | 492 | | ex.Body = _errorBody; |
| | 493 | | } |
| 0 | 494 | | } |
| 0 | 495 | | catch (JsonException) |
| | 496 | | { |
| | 497 | | // Ignore the exception |
| 0 | 498 | | } |
| 0 | 499 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 500 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 501 | | if (_shouldTrace) |
| | 502 | | { |
| 0 | 503 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 504 | | } |
| 0 | 505 | | _httpRequest.Dispose(); |
| 0 | 506 | | if (_httpResponse != null) |
| | 507 | | { |
| 0 | 508 | | _httpResponse.Dispose(); |
| | 509 | | } |
| 0 | 510 | | throw ex; |
| | 511 | | } |
| | 512 | | // Create Result |
| 0 | 513 | | var _result = new HttpOperationResponse<KeysResult>(); |
| 0 | 514 | | _result.Request = _httpRequest; |
| 0 | 515 | | _result.Response = _httpResponse; |
| | 516 | | // Deserialize Response |
| 0 | 517 | | if ((int)_statusCode == 200) |
| | 518 | | { |
| 0 | 519 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 520 | | try |
| | 521 | | { |
| 0 | 522 | | _result.Body = SafeJsonConvert.DeserializeObject<KeysResult>(_responseContent, DeserializationSettin |
| 0 | 523 | | } |
| 0 | 524 | | catch (JsonException ex) |
| | 525 | | { |
| 0 | 526 | | _httpRequest.Dispose(); |
| 0 | 527 | | if (_httpResponse != null) |
| | 528 | | { |
| 0 | 529 | | _httpResponse.Dispose(); |
| | 530 | | } |
| 0 | 531 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 532 | | } |
| | 533 | | } |
| 0 | 534 | | if (_shouldTrace) |
| | 535 | | { |
| 0 | 536 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 537 | | } |
| 0 | 538 | | return _result; |
| 0 | 539 | | } |
| | 540 | |
|
| | 541 | | /// <summary> |
| | 542 | | /// Get Models |
| | 543 | | /// </summary> |
| | 544 | | /// <remarks> |
| | 545 | | /// Get information about all trained custom models |
| | 546 | | /// </remarks> |
| | 547 | | /// <param name='customHeaders'> |
| | 548 | | /// Headers that will be added to request. |
| | 549 | | /// </param> |
| | 550 | | /// <param name='cancellationToken'> |
| | 551 | | /// The cancellation token. |
| | 552 | | /// </param> |
| | 553 | | /// <exception cref="ErrorResponseException"> |
| | 554 | | /// Thrown when the operation returned an invalid status code |
| | 555 | | /// </exception> |
| | 556 | | /// <exception cref="SerializationException"> |
| | 557 | | /// Thrown when unable to deserialize the response |
| | 558 | | /// </exception> |
| | 559 | | /// <exception cref="ValidationException"> |
| | 560 | | /// Thrown when a required parameter is null |
| | 561 | | /// </exception> |
| | 562 | | /// <exception cref="System.ArgumentNullException"> |
| | 563 | | /// Thrown when a required parameter is null |
| | 564 | | /// </exception> |
| | 565 | | /// <return> |
| | 566 | | /// A response object containing the response body and response headers. |
| | 567 | | /// </return> |
| | 568 | | public async Task<HttpOperationResponse<ModelsResult>> GetCustomModelsWithHttpMessagesAsync(Dictionary<string, L |
| | 569 | | { |
| 0 | 570 | | if (Endpoint == null) |
| | 571 | | { |
| 0 | 572 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 573 | | } |
| | 574 | | // Tracing |
| 0 | 575 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 576 | | string _invocationId = null; |
| 0 | 577 | | if (_shouldTrace) |
| | 578 | | { |
| 0 | 579 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 580 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 581 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 582 | | ServiceClientTracing.Enter(_invocationId, this, "GetCustomModels", tracingParameters); |
| | 583 | | } |
| | 584 | | // Construct URL |
| 0 | 585 | | var _baseUrl = BaseUri; |
| 0 | 586 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "custom/models"; |
| 0 | 587 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | 588 | | // Create HTTP transport objects |
| 0 | 589 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 590 | | HttpResponseMessage _httpResponse = null; |
| 0 | 591 | | _httpRequest.Method = new HttpMethod("GET"); |
| 0 | 592 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 593 | | // Set Headers |
| | 594 | |
|
| | 595 | |
|
| 0 | 596 | | if (customHeaders != null) |
| | 597 | | { |
| 0 | 598 | | foreach(var _header in customHeaders) |
| | 599 | | { |
| 0 | 600 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 601 | | { |
| 0 | 602 | | _httpRequest.Headers.Remove(_header.Key); |
| | 603 | | } |
| 0 | 604 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 605 | | } |
| | 606 | | } |
| | 607 | |
|
| | 608 | | // Serialize Request |
| 0 | 609 | | string _requestContent = null; |
| | 610 | | // Set Credentials |
| 0 | 611 | | if (Credentials != null) |
| | 612 | | { |
| 0 | 613 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 614 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 615 | | } |
| | 616 | | // Send Request |
| 0 | 617 | | if (_shouldTrace) |
| | 618 | | { |
| 0 | 619 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 620 | | } |
| 0 | 621 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 622 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 623 | | if (_shouldTrace) |
| | 624 | | { |
| 0 | 625 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 626 | | } |
| 0 | 627 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 628 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 629 | | string _responseContent = null; |
| 0 | 630 | | if ((int)_statusCode != 200) |
| | 631 | | { |
| 0 | 632 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 633 | | try |
| | 634 | | { |
| 0 | 635 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 636 | | ErrorResponse _errorBody = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, Deser |
| 0 | 637 | | if (_errorBody != null) |
| | 638 | | { |
| 0 | 639 | | ex.Body = _errorBody; |
| | 640 | | } |
| 0 | 641 | | } |
| 0 | 642 | | catch (JsonException) |
| | 643 | | { |
| | 644 | | // Ignore the exception |
| 0 | 645 | | } |
| 0 | 646 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 647 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 648 | | if (_shouldTrace) |
| | 649 | | { |
| 0 | 650 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 651 | | } |
| 0 | 652 | | _httpRequest.Dispose(); |
| 0 | 653 | | if (_httpResponse != null) |
| | 654 | | { |
| 0 | 655 | | _httpResponse.Dispose(); |
| | 656 | | } |
| 0 | 657 | | throw ex; |
| | 658 | | } |
| | 659 | | // Create Result |
| 0 | 660 | | var _result = new HttpOperationResponse<ModelsResult>(); |
| 0 | 661 | | _result.Request = _httpRequest; |
| 0 | 662 | | _result.Response = _httpResponse; |
| | 663 | | // Deserialize Response |
| 0 | 664 | | if ((int)_statusCode == 200) |
| | 665 | | { |
| 0 | 666 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 667 | | try |
| | 668 | | { |
| 0 | 669 | | _result.Body = SafeJsonConvert.DeserializeObject<ModelsResult>(_responseContent, DeserializationSett |
| 0 | 670 | | } |
| 0 | 671 | | catch (JsonException ex) |
| | 672 | | { |
| 0 | 673 | | _httpRequest.Dispose(); |
| 0 | 674 | | if (_httpResponse != null) |
| | 675 | | { |
| 0 | 676 | | _httpResponse.Dispose(); |
| | 677 | | } |
| 0 | 678 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 679 | | } |
| | 680 | | } |
| 0 | 681 | | if (_shouldTrace) |
| | 682 | | { |
| 0 | 683 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 684 | | } |
| 0 | 685 | | return _result; |
| 0 | 686 | | } |
| | 687 | |
|
| | 688 | | /// <summary> |
| | 689 | | /// Get Model |
| | 690 | | /// </summary> |
| | 691 | | /// <remarks> |
| | 692 | | /// Get information about a model. |
| | 693 | | /// </remarks> |
| | 694 | | /// <param name='id'> |
| | 695 | | /// Model identifier. |
| | 696 | | /// </param> |
| | 697 | | /// <param name='customHeaders'> |
| | 698 | | /// Headers that will be added to request. |
| | 699 | | /// </param> |
| | 700 | | /// <param name='cancellationToken'> |
| | 701 | | /// The cancellation token. |
| | 702 | | /// </param> |
| | 703 | | /// <exception cref="ErrorResponseException"> |
| | 704 | | /// Thrown when the operation returned an invalid status code |
| | 705 | | /// </exception> |
| | 706 | | /// <exception cref="SerializationException"> |
| | 707 | | /// Thrown when unable to deserialize the response |
| | 708 | | /// </exception> |
| | 709 | | /// <exception cref="ValidationException"> |
| | 710 | | /// Thrown when a required parameter is null |
| | 711 | | /// </exception> |
| | 712 | | /// <exception cref="System.ArgumentNullException"> |
| | 713 | | /// Thrown when a required parameter is null |
| | 714 | | /// </exception> |
| | 715 | | /// <return> |
| | 716 | | /// A response object containing the response body and response headers. |
| | 717 | | /// </return> |
| | 718 | | public async Task<HttpOperationResponse<ModelResult>> GetCustomModelWithHttpMessagesAsync(System.Guid id, Dictio |
| | 719 | | { |
| 0 | 720 | | if (Endpoint == null) |
| | 721 | | { |
| 0 | 722 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 723 | | } |
| | 724 | | // Tracing |
| 0 | 725 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 726 | | string _invocationId = null; |
| 0 | 727 | | if (_shouldTrace) |
| | 728 | | { |
| 0 | 729 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 730 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 731 | | tracingParameters.Add("id", id); |
| 0 | 732 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 733 | | ServiceClientTracing.Enter(_invocationId, this, "GetCustomModel", tracingParameters); |
| | 734 | | } |
| | 735 | | // Construct URL |
| 0 | 736 | | var _baseUrl = BaseUri; |
| 0 | 737 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "custom/models/{id}"; |
| 0 | 738 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 0 | 739 | | _url = _url.Replace("{id}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject(id, SerializationSet |
| | 740 | | // Create HTTP transport objects |
| 0 | 741 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 742 | | HttpResponseMessage _httpResponse = null; |
| 0 | 743 | | _httpRequest.Method = new HttpMethod("GET"); |
| 0 | 744 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 745 | | // Set Headers |
| | 746 | |
|
| | 747 | |
|
| 0 | 748 | | if (customHeaders != null) |
| | 749 | | { |
| 0 | 750 | | foreach(var _header in customHeaders) |
| | 751 | | { |
| 0 | 752 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 753 | | { |
| 0 | 754 | | _httpRequest.Headers.Remove(_header.Key); |
| | 755 | | } |
| 0 | 756 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 757 | | } |
| | 758 | | } |
| | 759 | |
|
| | 760 | | // Serialize Request |
| 0 | 761 | | string _requestContent = null; |
| | 762 | | // Set Credentials |
| 0 | 763 | | if (Credentials != null) |
| | 764 | | { |
| 0 | 765 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 766 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 767 | | } |
| | 768 | | // Send Request |
| 0 | 769 | | if (_shouldTrace) |
| | 770 | | { |
| 0 | 771 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 772 | | } |
| 0 | 773 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 774 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 775 | | if (_shouldTrace) |
| | 776 | | { |
| 0 | 777 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 778 | | } |
| 0 | 779 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 780 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 781 | | string _responseContent = null; |
| 0 | 782 | | if ((int)_statusCode != 200) |
| | 783 | | { |
| 0 | 784 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 785 | | try |
| | 786 | | { |
| 0 | 787 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 788 | | ErrorResponse _errorBody = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, Deser |
| 0 | 789 | | if (_errorBody != null) |
| | 790 | | { |
| 0 | 791 | | ex.Body = _errorBody; |
| | 792 | | } |
| 0 | 793 | | } |
| 0 | 794 | | catch (JsonException) |
| | 795 | | { |
| | 796 | | // Ignore the exception |
| 0 | 797 | | } |
| 0 | 798 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 799 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 800 | | if (_shouldTrace) |
| | 801 | | { |
| 0 | 802 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 803 | | } |
| 0 | 804 | | _httpRequest.Dispose(); |
| 0 | 805 | | if (_httpResponse != null) |
| | 806 | | { |
| 0 | 807 | | _httpResponse.Dispose(); |
| | 808 | | } |
| 0 | 809 | | throw ex; |
| | 810 | | } |
| | 811 | | // Create Result |
| 0 | 812 | | var _result = new HttpOperationResponse<ModelResult>(); |
| 0 | 813 | | _result.Request = _httpRequest; |
| 0 | 814 | | _result.Response = _httpResponse; |
| | 815 | | // Deserialize Response |
| 0 | 816 | | if ((int)_statusCode == 200) |
| | 817 | | { |
| 0 | 818 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 819 | | try |
| | 820 | | { |
| 0 | 821 | | _result.Body = SafeJsonConvert.DeserializeObject<ModelResult>(_responseContent, DeserializationSetti |
| 0 | 822 | | } |
| 0 | 823 | | catch (JsonException ex) |
| | 824 | | { |
| 0 | 825 | | _httpRequest.Dispose(); |
| 0 | 826 | | if (_httpResponse != null) |
| | 827 | | { |
| 0 | 828 | | _httpResponse.Dispose(); |
| | 829 | | } |
| 0 | 830 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 831 | | } |
| | 832 | | } |
| 0 | 833 | | if (_shouldTrace) |
| | 834 | | { |
| 0 | 835 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 836 | | } |
| 0 | 837 | | return _result; |
| 0 | 838 | | } |
| | 839 | |
|
| | 840 | | /// <summary> |
| | 841 | | /// Delete Model |
| | 842 | | /// </summary> |
| | 843 | | /// <remarks> |
| | 844 | | /// Delete model artifacts. |
| | 845 | | /// </remarks> |
| | 846 | | /// <param name='id'> |
| | 847 | | /// The identifier of the model to delete. |
| | 848 | | /// </param> |
| | 849 | | /// <param name='customHeaders'> |
| | 850 | | /// Headers that will be added to request. |
| | 851 | | /// </param> |
| | 852 | | /// <param name='cancellationToken'> |
| | 853 | | /// The cancellation token. |
| | 854 | | /// </param> |
| | 855 | | /// <exception cref="ErrorResponseException"> |
| | 856 | | /// Thrown when the operation returned an invalid status code |
| | 857 | | /// </exception> |
| | 858 | | /// <exception cref="ValidationException"> |
| | 859 | | /// Thrown when a required parameter is null |
| | 860 | | /// </exception> |
| | 861 | | /// <exception cref="System.ArgumentNullException"> |
| | 862 | | /// Thrown when a required parameter is null |
| | 863 | | /// </exception> |
| | 864 | | /// <return> |
| | 865 | | /// A response object containing the response body and response headers. |
| | 866 | | /// </return> |
| | 867 | | public async Task<HttpOperationResponse> DeleteCustomModelWithHttpMessagesAsync(System.Guid id, Dictionary<strin |
| | 868 | | { |
| 0 | 869 | | if (Endpoint == null) |
| | 870 | | { |
| 0 | 871 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 872 | | } |
| | 873 | | // Tracing |
| 0 | 874 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 875 | | string _invocationId = null; |
| 0 | 876 | | if (_shouldTrace) |
| | 877 | | { |
| 0 | 878 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 879 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 880 | | tracingParameters.Add("id", id); |
| 0 | 881 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 882 | | ServiceClientTracing.Enter(_invocationId, this, "DeleteCustomModel", tracingParameters); |
| | 883 | | } |
| | 884 | | // Construct URL |
| 0 | 885 | | var _baseUrl = BaseUri; |
| 0 | 886 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "custom/models/{id}"; |
| 0 | 887 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 0 | 888 | | _url = _url.Replace("{id}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject(id, SerializationSet |
| | 889 | | // Create HTTP transport objects |
| 0 | 890 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 891 | | HttpResponseMessage _httpResponse = null; |
| 0 | 892 | | _httpRequest.Method = new HttpMethod("DELETE"); |
| 0 | 893 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 894 | | // Set Headers |
| | 895 | |
|
| | 896 | |
|
| 0 | 897 | | if (customHeaders != null) |
| | 898 | | { |
| 0 | 899 | | foreach(var _header in customHeaders) |
| | 900 | | { |
| 0 | 901 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 902 | | { |
| 0 | 903 | | _httpRequest.Headers.Remove(_header.Key); |
| | 904 | | } |
| 0 | 905 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 906 | | } |
| | 907 | | } |
| | 908 | |
|
| | 909 | | // Serialize Request |
| 0 | 910 | | string _requestContent = null; |
| | 911 | | // Set Credentials |
| 0 | 912 | | if (Credentials != null) |
| | 913 | | { |
| 0 | 914 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 915 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 916 | | } |
| | 917 | | // Send Request |
| 0 | 918 | | if (_shouldTrace) |
| | 919 | | { |
| 0 | 920 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 921 | | } |
| 0 | 922 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 923 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 924 | | if (_shouldTrace) |
| | 925 | | { |
| 0 | 926 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 927 | | } |
| 0 | 928 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 929 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 930 | | string _responseContent = null; |
| 0 | 931 | | if ((int)_statusCode != 204) |
| | 932 | | { |
| 0 | 933 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 934 | | try |
| | 935 | | { |
| 0 | 936 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 937 | | ErrorResponse _errorBody = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, Deser |
| 0 | 938 | | if (_errorBody != null) |
| | 939 | | { |
| 0 | 940 | | ex.Body = _errorBody; |
| | 941 | | } |
| 0 | 942 | | } |
| 0 | 943 | | catch (JsonException) |
| | 944 | | { |
| | 945 | | // Ignore the exception |
| 0 | 946 | | } |
| 0 | 947 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 948 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 949 | | if (_shouldTrace) |
| | 950 | | { |
| 0 | 951 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 952 | | } |
| 0 | 953 | | _httpRequest.Dispose(); |
| 0 | 954 | | if (_httpResponse != null) |
| | 955 | | { |
| 0 | 956 | | _httpResponse.Dispose(); |
| | 957 | | } |
| 0 | 958 | | throw ex; |
| | 959 | | } |
| | 960 | | // Create Result |
| 0 | 961 | | var _result = new HttpOperationResponse(); |
| 0 | 962 | | _result.Request = _httpRequest; |
| 0 | 963 | | _result.Response = _httpResponse; |
| 0 | 964 | | if (_shouldTrace) |
| | 965 | | { |
| 0 | 966 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 967 | | } |
| 0 | 968 | | return _result; |
| 0 | 969 | | } |
| | 970 | |
|
| | 971 | | /// <summary> |
| | 972 | | /// Analyze Form |
| | 973 | | /// </summary> |
| | 974 | | /// <remarks> |
| | 975 | | /// Extract key-value pairs from a given document. The input document must be |
| | 976 | | /// of one of the supported content types - 'application/pdf', 'image/jpeg' or |
| | 977 | | /// 'image/png'. A success response is returned in JSON. |
| | 978 | | /// </remarks> |
| | 979 | | /// <param name='id'> |
| | 980 | | /// Model Identifier to analyze the document with. |
| | 981 | | /// </param> |
| | 982 | | /// <param name='formStream'> |
| | 983 | | /// A pdf document or image (jpg,png) file to analyze. |
| | 984 | | /// </param> |
| | 985 | | /// <param name='keys'> |
| | 986 | | /// An optional list of known keys to extract the values for. |
| | 987 | | /// </param> |
| | 988 | | /// <param name='customHeaders'> |
| | 989 | | /// Headers that will be added to request. |
| | 990 | | /// </param> |
| | 991 | | /// <param name='cancellationToken'> |
| | 992 | | /// The cancellation token. |
| | 993 | | /// </param> |
| | 994 | | /// <exception cref="ErrorResponseException"> |
| | 995 | | /// Thrown when the operation returned an invalid status code |
| | 996 | | /// </exception> |
| | 997 | | /// <exception cref="SerializationException"> |
| | 998 | | /// Thrown when unable to deserialize the response |
| | 999 | | /// </exception> |
| | 1000 | | /// <exception cref="ValidationException"> |
| | 1001 | | /// Thrown when a required parameter is null |
| | 1002 | | /// </exception> |
| | 1003 | | /// <exception cref="System.ArgumentNullException"> |
| | 1004 | | /// Thrown when a required parameter is null |
| | 1005 | | /// </exception> |
| | 1006 | | /// <return> |
| | 1007 | | /// A response object containing the response body and response headers. |
| | 1008 | | /// </return> |
| | 1009 | | public async Task<HttpOperationResponse<AnalyzeResult>> AnalyzeWithCustomModelWithHttpMessagesAsync(System.Guid |
| | 1010 | | { |
| | 1011 | | if (Endpoint == null) |
| | 1012 | | { |
| | 1013 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 1014 | | } |
| | 1015 | | if (formStream == null) |
| | 1016 | | { |
| | 1017 | | throw new ValidationException(ValidationRules.CannotBeNull, "formStream"); |
| | 1018 | | } |
| | 1019 | | // Tracing |
| | 1020 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 1021 | | string _invocationId = null; |
| | 1022 | | if (_shouldTrace) |
| | 1023 | | { |
| | 1024 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 1025 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 1026 | | tracingParameters.Add("id", id); |
| | 1027 | | tracingParameters.Add("keys", keys); |
| | 1028 | | tracingParameters.Add("formStream", formStream); |
| | 1029 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 1030 | | ServiceClientTracing.Enter(_invocationId, this, "AnalyzeWithCustomModel", tracingParameters); |
| | 1031 | | } |
| | 1032 | | // Construct URL |
| | 1033 | | var _baseUrl = BaseUri; |
| | 1034 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "custom/models/{id}/analyze"; |
| | 1035 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | 1036 | | _url = _url.Replace("{id}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject(id, SerializationSet |
| | 1037 | | List<string> _queryParameters = new List<string>(); |
| | 1038 | | if (keys != null) |
| | 1039 | | { |
| | 1040 | | _queryParameters.Add(string.Format("keys={0}", System.Uri.EscapeDataString(string.Join(",", keys)))); |
| | 1041 | | } |
| | 1042 | | if (_queryParameters.Count > 0) |
| | 1043 | | { |
| | 1044 | | _url += "?" + string.Join("&", _queryParameters); |
| | 1045 | | } |
| | 1046 | | // Create HTTP transport objects |
| | 1047 | | var _httpRequest = new HttpRequestMessage(); |
| | 1048 | | HttpResponseMessage _httpResponse = null; |
| | 1049 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 1050 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1051 | | // Set Headers |
| | 1052 | |
|
| | 1053 | |
|
| | 1054 | | if (customHeaders != null) |
| | 1055 | | { |
| | 1056 | | foreach(var _header in customHeaders) |
| | 1057 | | { |
| | 1058 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1059 | | { |
| | 1060 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1061 | | } |
| | 1062 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1063 | | } |
| | 1064 | | } |
| | 1065 | |
|
| | 1066 | | // Serialize Request |
| | 1067 | | string _requestContent = null; |
| | 1068 | | MultipartFormDataContent _multiPartContent = new MultipartFormDataContent(); |
| | 1069 | | if (formStream != null) |
| | 1070 | | { |
| | 1071 | | StreamContent _formStream = new StreamContent(formStream); |
| | 1072 | | _formStream.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); |
| | 1073 | | ContentDispositionHeaderValue _contentDispositionHeaderValue = new ContentDispositionHeaderValue("form-d |
| | 1074 | | _contentDispositionHeaderValue.Name = "form_stream"; |
| | 1075 | | // get filename from stream if it's a file otherwise, just use 'unknown' |
| | 1076 | | var _fileStream = formStream as FileStream; |
| | 1077 | | var _fileName = (_fileStream != null ? _fileStream.Name : null) ?? "unknown"; |
| 0 | 1078 | | if(System.Linq.Enumerable.Any(_fileName, c => c > 127) ) |
| | 1079 | | { |
| | 1080 | | // non ASCII chars detected, need UTF encoding: |
| | 1081 | | _contentDispositionHeaderValue.FileNameStar = _fileName; |
| | 1082 | | } |
| | 1083 | | else |
| | 1084 | | { |
| | 1085 | | // ASCII only |
| | 1086 | | _contentDispositionHeaderValue.FileName = _fileName; |
| | 1087 | | } |
| | 1088 | | _formStream.Headers.ContentDisposition = _contentDispositionHeaderValue; |
| | 1089 | | _multiPartContent.Add(_formStream, "form_stream"); |
| | 1090 | | } |
| | 1091 | | _httpRequest.Content = _multiPartContent; |
| | 1092 | | // Set Credentials |
| | 1093 | | if (Credentials != null) |
| | 1094 | | { |
| | 1095 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 1096 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1097 | | } |
| | 1098 | | // Send Request |
| | 1099 | | if (_shouldTrace) |
| | 1100 | | { |
| | 1101 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1102 | | } |
| | 1103 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 1104 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1105 | | if (_shouldTrace) |
| | 1106 | | { |
| | 1107 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1108 | | } |
| | 1109 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 1110 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 1111 | | string _responseContent = null; |
| | 1112 | | if ((int)_statusCode != 200) |
| | 1113 | | { |
| | 1114 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 1115 | | try |
| | 1116 | | { |
| | 1117 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1118 | | ErrorResponse _errorBody = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, Deser |
| | 1119 | | if (_errorBody != null) |
| | 1120 | | { |
| | 1121 | | ex.Body = _errorBody; |
| | 1122 | | } |
| | 1123 | | } |
| | 1124 | | catch (JsonException) |
| | 1125 | | { |
| | 1126 | | // Ignore the exception |
| | 1127 | | } |
| | 1128 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 1129 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 1130 | | if (_shouldTrace) |
| | 1131 | | { |
| | 1132 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1133 | | } |
| | 1134 | | _httpRequest.Dispose(); |
| | 1135 | | if (_httpResponse != null) |
| | 1136 | | { |
| | 1137 | | _httpResponse.Dispose(); |
| | 1138 | | } |
| | 1139 | | throw ex; |
| | 1140 | | } |
| | 1141 | | // Create Result |
| | 1142 | | var _result = new HttpOperationResponse<AnalyzeResult>(); |
| | 1143 | | _result.Request = _httpRequest; |
| | 1144 | | _result.Response = _httpResponse; |
| | 1145 | | // Deserialize Response |
| | 1146 | | if ((int)_statusCode == 200) |
| | 1147 | | { |
| | 1148 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1149 | | try |
| | 1150 | | { |
| | 1151 | | _result.Body = SafeJsonConvert.DeserializeObject<AnalyzeResult>(_responseContent, DeserializationSet |
| | 1152 | | } |
| | 1153 | | catch (JsonException ex) |
| | 1154 | | { |
| | 1155 | | _httpRequest.Dispose(); |
| | 1156 | | if (_httpResponse != null) |
| | 1157 | | { |
| | 1158 | | _httpResponse.Dispose(); |
| | 1159 | | } |
| | 1160 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1161 | | } |
| | 1162 | | } |
| | 1163 | | if (_shouldTrace) |
| | 1164 | | { |
| | 1165 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1166 | | } |
| | 1167 | | return _result; |
| | 1168 | | } |
| | 1169 | |
|
| | 1170 | | /// <summary> |
| | 1171 | | /// Batch Read Receipt operation. The response contains a field called |
| | 1172 | | /// 'Operation-Location', which contains the URL that you must use for your |
| | 1173 | | /// 'Get Read Receipt Result' operation. |
| | 1174 | | /// </summary> |
| | 1175 | | /// <param name='url'> |
| | 1176 | | /// Publicly reachable URL of an image. |
| | 1177 | | /// </param> |
| | 1178 | | /// <param name='customHeaders'> |
| | 1179 | | /// Headers that will be added to request. |
| | 1180 | | /// </param> |
| | 1181 | | /// <param name='cancellationToken'> |
| | 1182 | | /// The cancellation token. |
| | 1183 | | /// </param> |
| | 1184 | | /// <exception cref="ComputerVisionErrorException"> |
| | 1185 | | /// Thrown when the operation returned an invalid status code |
| | 1186 | | /// </exception> |
| | 1187 | | /// <exception cref="ValidationException"> |
| | 1188 | | /// Thrown when a required parameter is null |
| | 1189 | | /// </exception> |
| | 1190 | | /// <exception cref="System.ArgumentNullException"> |
| | 1191 | | /// Thrown when a required parameter is null |
| | 1192 | | /// </exception> |
| | 1193 | | /// <return> |
| | 1194 | | /// A response object containing the response body and response headers. |
| | 1195 | | /// </return> |
| | 1196 | | public async Task<HttpOperationHeaderResponse<BatchReadReceiptHeaders>> BatchReadReceiptWithHttpMessagesAsync(st |
| | 1197 | | { |
| 0 | 1198 | | if (Endpoint == null) |
| | 1199 | | { |
| 0 | 1200 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 1201 | | } |
| 0 | 1202 | | if (url == null) |
| | 1203 | | { |
| 0 | 1204 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 1205 | | } |
| 0 | 1206 | | ImageUrl imageUrl = new ImageUrl(); |
| 0 | 1207 | | if (url != null) |
| | 1208 | | { |
| 0 | 1209 | | imageUrl.Url = url; |
| | 1210 | | } |
| | 1211 | | // Tracing |
| 0 | 1212 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 1213 | | string _invocationId = null; |
| 0 | 1214 | | if (_shouldTrace) |
| | 1215 | | { |
| 0 | 1216 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1217 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1218 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 1219 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1220 | | ServiceClientTracing.Enter(_invocationId, this, "BatchReadReceipt", tracingParameters); |
| | 1221 | | } |
| | 1222 | | // Construct URL |
| 0 | 1223 | | var _baseUrl = BaseUri; |
| 0 | 1224 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "prebuilt/receipt/asyncBatchAnalyze"; |
| 0 | 1225 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | 1226 | | // Create HTTP transport objects |
| 0 | 1227 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 1228 | | HttpResponseMessage _httpResponse = null; |
| 0 | 1229 | | _httpRequest.Method = new HttpMethod("POST"); |
| 0 | 1230 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1231 | | // Set Headers |
| | 1232 | |
|
| | 1233 | |
|
| 0 | 1234 | | if (customHeaders != null) |
| | 1235 | | { |
| 0 | 1236 | | foreach(var _header in customHeaders) |
| | 1237 | | { |
| 0 | 1238 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1239 | | { |
| 0 | 1240 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1241 | | } |
| 0 | 1242 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1243 | | } |
| | 1244 | | } |
| | 1245 | |
|
| | 1246 | | // Serialize Request |
| 0 | 1247 | | string _requestContent = null; |
| 0 | 1248 | | if(imageUrl != null) |
| | 1249 | | { |
| 0 | 1250 | | _requestContent = SafeJsonConvert.SerializeObject(imageUrl, SerializationSettings); |
| 0 | 1251 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 0 | 1252 | | _httpRequest.Content.Headers.ContentType =MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); |
| | 1253 | | } |
| | 1254 | | // Set Credentials |
| 0 | 1255 | | if (Credentials != null) |
| | 1256 | | { |
| 0 | 1257 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1258 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1259 | | } |
| | 1260 | | // Send Request |
| 0 | 1261 | | if (_shouldTrace) |
| | 1262 | | { |
| 0 | 1263 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1264 | | } |
| 0 | 1265 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1266 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 1267 | | if (_shouldTrace) |
| | 1268 | | { |
| 0 | 1269 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1270 | | } |
| 0 | 1271 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 1272 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1273 | | string _responseContent = null; |
| 0 | 1274 | | if ((int)_statusCode != 202) |
| | 1275 | | { |
| 0 | 1276 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 1277 | | try |
| | 1278 | | { |
| 0 | 1279 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1280 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 1281 | | if (_errorBody != null) |
| | 1282 | | { |
| 0 | 1283 | | ex.Body = _errorBody; |
| | 1284 | | } |
| 0 | 1285 | | } |
| 0 | 1286 | | catch (JsonException) |
| | 1287 | | { |
| | 1288 | | // Ignore the exception |
| 0 | 1289 | | } |
| 0 | 1290 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1291 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1292 | | if (_shouldTrace) |
| | 1293 | | { |
| 0 | 1294 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1295 | | } |
| 0 | 1296 | | _httpRequest.Dispose(); |
| 0 | 1297 | | if (_httpResponse != null) |
| | 1298 | | { |
| 0 | 1299 | | _httpResponse.Dispose(); |
| | 1300 | | } |
| 0 | 1301 | | throw ex; |
| | 1302 | | } |
| | 1303 | | // Create Result |
| 0 | 1304 | | var _result = new HttpOperationHeaderResponse<BatchReadReceiptHeaders>(); |
| 0 | 1305 | | _result.Request = _httpRequest; |
| 0 | 1306 | | _result.Response = _httpResponse; |
| | 1307 | | try |
| | 1308 | | { |
| 0 | 1309 | | _result.Headers = _httpResponse.GetHeadersAsJson().ToObject<BatchReadReceiptHeaders>(JsonSerializer.Crea |
| 0 | 1310 | | } |
| 0 | 1311 | | catch (JsonException ex) |
| | 1312 | | { |
| 0 | 1313 | | _httpRequest.Dispose(); |
| 0 | 1314 | | if (_httpResponse != null) |
| | 1315 | | { |
| 0 | 1316 | | _httpResponse.Dispose(); |
| | 1317 | | } |
| 0 | 1318 | | throw new SerializationException("Unable to deserialize the headers.", _httpResponse.GetHeadersAsJson(). |
| | 1319 | | } |
| 0 | 1320 | | if (_shouldTrace) |
| | 1321 | | { |
| 0 | 1322 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1323 | | } |
| 0 | 1324 | | return _result; |
| 0 | 1325 | | } |
| | 1326 | |
|
| | 1327 | | /// <summary> |
| | 1328 | | /// This interface is used for getting the analysis results of a 'Batch Read |
| | 1329 | | /// Receipt' operation. The URL to this interface should be retrieved from the |
| | 1330 | | /// 'Operation-Location' field returned from the 'Batch Read Receipt' |
| | 1331 | | /// operation. |
| | 1332 | | /// </summary> |
| | 1333 | | /// <param name='operationId'> |
| | 1334 | | /// Id of read operation returned in the response of a 'Batch Read Receipt' |
| | 1335 | | /// operation. |
| | 1336 | | /// </param> |
| | 1337 | | /// <param name='customHeaders'> |
| | 1338 | | /// Headers that will be added to request. |
| | 1339 | | /// </param> |
| | 1340 | | /// <param name='cancellationToken'> |
| | 1341 | | /// The cancellation token. |
| | 1342 | | /// </param> |
| | 1343 | | /// <exception cref="ComputerVisionErrorException"> |
| | 1344 | | /// Thrown when the operation returned an invalid status code |
| | 1345 | | /// </exception> |
| | 1346 | | /// <exception cref="SerializationException"> |
| | 1347 | | /// Thrown when unable to deserialize the response |
| | 1348 | | /// </exception> |
| | 1349 | | /// <exception cref="ValidationException"> |
| | 1350 | | /// Thrown when a required parameter is null |
| | 1351 | | /// </exception> |
| | 1352 | | /// <exception cref="System.ArgumentNullException"> |
| | 1353 | | /// Thrown when a required parameter is null |
| | 1354 | | /// </exception> |
| | 1355 | | /// <return> |
| | 1356 | | /// A response object containing the response body and response headers. |
| | 1357 | | /// </return> |
| | 1358 | | public async Task<HttpOperationResponse<ReadReceiptResult>> GetReadReceiptResultWithHttpMessagesAsync(string ope |
| | 1359 | | { |
| 0 | 1360 | | if (Endpoint == null) |
| | 1361 | | { |
| 0 | 1362 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 1363 | | } |
| 0 | 1364 | | if (operationId == null) |
| | 1365 | | { |
| 0 | 1366 | | throw new ValidationException(ValidationRules.CannotBeNull, "operationId"); |
| | 1367 | | } |
| | 1368 | | // Tracing |
| 0 | 1369 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 1370 | | string _invocationId = null; |
| 0 | 1371 | | if (_shouldTrace) |
| | 1372 | | { |
| 0 | 1373 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1374 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1375 | | tracingParameters.Add("operationId", operationId); |
| 0 | 1376 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1377 | | ServiceClientTracing.Enter(_invocationId, this, "GetReadReceiptResult", tracingParameters); |
| | 1378 | | } |
| | 1379 | | // Construct URL |
| 0 | 1380 | | var _baseUrl = BaseUri; |
| 0 | 1381 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "prebuilt/receipt/operations/{operationId}"; |
| 0 | 1382 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 0 | 1383 | | _url = _url.Replace("{operationId}", System.Uri.EscapeDataString(operationId)); |
| | 1384 | | // Create HTTP transport objects |
| 0 | 1385 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 1386 | | HttpResponseMessage _httpResponse = null; |
| 0 | 1387 | | _httpRequest.Method = new HttpMethod("GET"); |
| 0 | 1388 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1389 | | // Set Headers |
| | 1390 | |
|
| | 1391 | |
|
| 0 | 1392 | | if (customHeaders != null) |
| | 1393 | | { |
| 0 | 1394 | | foreach(var _header in customHeaders) |
| | 1395 | | { |
| 0 | 1396 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1397 | | { |
| 0 | 1398 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1399 | | } |
| 0 | 1400 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1401 | | } |
| | 1402 | | } |
| | 1403 | |
|
| | 1404 | | // Serialize Request |
| 0 | 1405 | | string _requestContent = null; |
| | 1406 | | // Set Credentials |
| 0 | 1407 | | if (Credentials != null) |
| | 1408 | | { |
| 0 | 1409 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1410 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1411 | | } |
| | 1412 | | // Send Request |
| 0 | 1413 | | if (_shouldTrace) |
| | 1414 | | { |
| 0 | 1415 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1416 | | } |
| 0 | 1417 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1418 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 1419 | | if (_shouldTrace) |
| | 1420 | | { |
| 0 | 1421 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1422 | | } |
| 0 | 1423 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 1424 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1425 | | string _responseContent = null; |
| 0 | 1426 | | if ((int)_statusCode != 200) |
| | 1427 | | { |
| 0 | 1428 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 1429 | | try |
| | 1430 | | { |
| 0 | 1431 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1432 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 1433 | | if (_errorBody != null) |
| | 1434 | | { |
| 0 | 1435 | | ex.Body = _errorBody; |
| | 1436 | | } |
| 0 | 1437 | | } |
| 0 | 1438 | | catch (JsonException) |
| | 1439 | | { |
| | 1440 | | // Ignore the exception |
| 0 | 1441 | | } |
| 0 | 1442 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1443 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1444 | | if (_shouldTrace) |
| | 1445 | | { |
| 0 | 1446 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1447 | | } |
| 0 | 1448 | | _httpRequest.Dispose(); |
| 0 | 1449 | | if (_httpResponse != null) |
| | 1450 | | { |
| 0 | 1451 | | _httpResponse.Dispose(); |
| | 1452 | | } |
| 0 | 1453 | | throw ex; |
| | 1454 | | } |
| | 1455 | | // Create Result |
| 0 | 1456 | | var _result = new HttpOperationResponse<ReadReceiptResult>(); |
| 0 | 1457 | | _result.Request = _httpRequest; |
| 0 | 1458 | | _result.Response = _httpResponse; |
| | 1459 | | // Deserialize Response |
| 0 | 1460 | | if ((int)_statusCode == 200) |
| | 1461 | | { |
| 0 | 1462 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1463 | | try |
| | 1464 | | { |
| 0 | 1465 | | _result.Body = SafeJsonConvert.DeserializeObject<ReadReceiptResult>(_responseContent, Deserializatio |
| 0 | 1466 | | } |
| 0 | 1467 | | catch (JsonException ex) |
| | 1468 | | { |
| 0 | 1469 | | _httpRequest.Dispose(); |
| 0 | 1470 | | if (_httpResponse != null) |
| | 1471 | | { |
| 0 | 1472 | | _httpResponse.Dispose(); |
| | 1473 | | } |
| 0 | 1474 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1475 | | } |
| | 1476 | | } |
| 0 | 1477 | | if (_shouldTrace) |
| | 1478 | | { |
| 0 | 1479 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1480 | | } |
| 0 | 1481 | | return _result; |
| 0 | 1482 | | } |
| | 1483 | |
|
| | 1484 | | /// <summary> |
| | 1485 | | /// Read Receipt operation. When you use the 'Batch Read Receipt' interface, |
| | 1486 | | /// the response contains a field called 'Operation-Location'. The |
| | 1487 | | /// 'Operation-Location' field contains the URL that you must use for your 'Get |
| | 1488 | | /// Read Receipt Result' operation. |
| | 1489 | | /// </summary> |
| | 1490 | | /// <param name='image'> |
| | 1491 | | /// An image stream. |
| | 1492 | | /// </param> |
| | 1493 | | /// <param name='customHeaders'> |
| | 1494 | | /// Headers that will be added to request. |
| | 1495 | | /// </param> |
| | 1496 | | /// <param name='cancellationToken'> |
| | 1497 | | /// The cancellation token. |
| | 1498 | | /// </param> |
| | 1499 | | /// <exception cref="ComputerVisionErrorException"> |
| | 1500 | | /// Thrown when the operation returned an invalid status code |
| | 1501 | | /// </exception> |
| | 1502 | | /// <exception cref="ValidationException"> |
| | 1503 | | /// Thrown when a required parameter is null |
| | 1504 | | /// </exception> |
| | 1505 | | /// <exception cref="System.ArgumentNullException"> |
| | 1506 | | /// Thrown when a required parameter is null |
| | 1507 | | /// </exception> |
| | 1508 | | /// <return> |
| | 1509 | | /// A response object containing the response body and response headers. |
| | 1510 | | /// </return> |
| | 1511 | | public async Task<HttpOperationHeaderResponse<BatchReadReceiptInStreamHeaders>> BatchReadReceiptInStreamWithHttp |
| | 1512 | | { |
| 0 | 1513 | | if (Endpoint == null) |
| | 1514 | | { |
| 0 | 1515 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 1516 | | } |
| 0 | 1517 | | if (image == null) |
| | 1518 | | { |
| 0 | 1519 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 1520 | | } |
| | 1521 | | // Tracing |
| 0 | 1522 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 1523 | | string _invocationId = null; |
| 0 | 1524 | | if (_shouldTrace) |
| | 1525 | | { |
| 0 | 1526 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1527 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1528 | | tracingParameters.Add("image", image); |
| 0 | 1529 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1530 | | ServiceClientTracing.Enter(_invocationId, this, "BatchReadReceiptInStream", tracingParameters); |
| | 1531 | | } |
| | 1532 | | // Construct URL |
| 0 | 1533 | | var _baseUrl = BaseUri; |
| 0 | 1534 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "prebuilt/receipt/asyncBatchAnalyze"; |
| 0 | 1535 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | 1536 | | // Create HTTP transport objects |
| 0 | 1537 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 1538 | | HttpResponseMessage _httpResponse = null; |
| 0 | 1539 | | _httpRequest.Method = new HttpMethod("POST"); |
| 0 | 1540 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1541 | | // Set Headers |
| | 1542 | |
|
| | 1543 | |
|
| 0 | 1544 | | if (customHeaders != null) |
| | 1545 | | { |
| 0 | 1546 | | foreach(var _header in customHeaders) |
| | 1547 | | { |
| 0 | 1548 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1549 | | { |
| 0 | 1550 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1551 | | } |
| 0 | 1552 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1553 | | } |
| | 1554 | | } |
| | 1555 | |
|
| | 1556 | | // Serialize Request |
| 0 | 1557 | | string _requestContent = null; |
| 0 | 1558 | | if(image == null) |
| | 1559 | | { |
| 0 | 1560 | | throw new System.ArgumentNullException("image"); |
| | 1561 | | } |
| 0 | 1562 | | if (image != null && image != Stream.Null) |
| | 1563 | | { |
| 0 | 1564 | | _httpRequest.Content = new StreamContent(image); |
| 0 | 1565 | | _httpRequest.Content.Headers.ContentType =MediaTypeHeaderValue.Parse("application/octet-stream"); |
| | 1566 | | } |
| | 1567 | | // Set Credentials |
| 0 | 1568 | | if (Credentials != null) |
| | 1569 | | { |
| 0 | 1570 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1571 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1572 | | } |
| | 1573 | | // Send Request |
| 0 | 1574 | | if (_shouldTrace) |
| | 1575 | | { |
| 0 | 1576 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1577 | | } |
| 0 | 1578 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1579 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 1580 | | if (_shouldTrace) |
| | 1581 | | { |
| 0 | 1582 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1583 | | } |
| 0 | 1584 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 1585 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1586 | | string _responseContent = null; |
| 0 | 1587 | | if ((int)_statusCode != 202) |
| | 1588 | | { |
| 0 | 1589 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 1590 | | try |
| | 1591 | | { |
| 0 | 1592 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1593 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 1594 | | if (_errorBody != null) |
| | 1595 | | { |
| 0 | 1596 | | ex.Body = _errorBody; |
| | 1597 | | } |
| 0 | 1598 | | } |
| 0 | 1599 | | catch (JsonException) |
| | 1600 | | { |
| | 1601 | | // Ignore the exception |
| 0 | 1602 | | } |
| 0 | 1603 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1604 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1605 | | if (_shouldTrace) |
| | 1606 | | { |
| 0 | 1607 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1608 | | } |
| 0 | 1609 | | _httpRequest.Dispose(); |
| 0 | 1610 | | if (_httpResponse != null) |
| | 1611 | | { |
| 0 | 1612 | | _httpResponse.Dispose(); |
| | 1613 | | } |
| 0 | 1614 | | throw ex; |
| | 1615 | | } |
| | 1616 | | // Create Result |
| 0 | 1617 | | var _result = new HttpOperationHeaderResponse<BatchReadReceiptInStreamHeaders>(); |
| 0 | 1618 | | _result.Request = _httpRequest; |
| 0 | 1619 | | _result.Response = _httpResponse; |
| | 1620 | | try |
| | 1621 | | { |
| 0 | 1622 | | _result.Headers = _httpResponse.GetHeadersAsJson().ToObject<BatchReadReceiptInStreamHeaders>(JsonSeriali |
| 0 | 1623 | | } |
| 0 | 1624 | | catch (JsonException ex) |
| | 1625 | | { |
| 0 | 1626 | | _httpRequest.Dispose(); |
| 0 | 1627 | | if (_httpResponse != null) |
| | 1628 | | { |
| 0 | 1629 | | _httpResponse.Dispose(); |
| | 1630 | | } |
| 0 | 1631 | | throw new SerializationException("Unable to deserialize the headers.", _httpResponse.GetHeadersAsJson(). |
| | 1632 | | } |
| 0 | 1633 | | if (_shouldTrace) |
| | 1634 | | { |
| 0 | 1635 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1636 | | } |
| 0 | 1637 | | return _result; |
| 0 | 1638 | | } |
| | 1639 | |
|
| | 1640 | | } |
| | 1641 | | } |