| | 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.Vision.ComputerVision |
| | 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.Threading; |
| | 23 | | using System.Threading.Tasks; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// The Computer Vision API provides state-of-the-art algorithms to process |
| | 27 | | /// images and return information. For example, it can be used to determine |
| | 28 | | /// if an image contains mature content, or it can be used to find all the |
| | 29 | | /// faces in an image. It also has other features like estimating dominant |
| | 30 | | /// and accent colors, categorizing the content of images, and describing |
| | 31 | | /// an image with complete English sentences. Additionally, it can also |
| | 32 | | /// intelligently generate images thumbnails for displaying large images |
| | 33 | | /// effectively. |
| | 34 | | /// </summary> |
| | 35 | | public partial class ComputerVisionClient : ServiceClient<ComputerVisionClient>, IComputerVisionClient |
| | 36 | | { |
| | 37 | | /// <summary> |
| | 38 | | /// The base URI of the service. |
| | 39 | | /// </summary> |
| 80 | 40 | | internal string BaseUri {get; set;} |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets or sets json serialization settings. |
| | 44 | | /// </summary> |
| 134 | 45 | | public JsonSerializerSettings SerializationSettings { get; private set; } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Gets or sets json deserialization settings. |
| | 49 | | /// </summary> |
| 118 | 50 | | public JsonSerializerSettings DeserializationSettings { get; private set; } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Supported Cognitive Services endpoints. |
| | 54 | | /// </summary> |
| 128 | 55 | | public string Endpoint { get; set; } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Subscription credentials which uniquely identify client subscription. |
| | 59 | | /// </summary> |
| 202 | 60 | | public ServiceClientCredentials Credentials { get; private set; } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Initializes a new instance of the ComputerVisionClient class. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name='httpClient'> |
| | 66 | | /// HttpClient to be used |
| | 67 | | /// </param> |
| | 68 | | /// <param name='disposeHttpClient'> |
| | 69 | | /// True: will dispose the provided httpClient on calling ComputerVisionClient.Dispose(). False: will not dispos |
| 0 | 70 | | protected ComputerVisionClient(HttpClient httpClient, bool disposeHttpClient) : base(httpClient, disposeHttpClie |
| | 71 | | { |
| 0 | 72 | | Initialize(); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Initializes a new instance of the ComputerVisionClient class. |
| | 77 | | /// </summary> |
| | 78 | | /// <param name='handlers'> |
| | 79 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 80 | | /// </param> |
| 42 | 81 | | protected ComputerVisionClient(params DelegatingHandler[] handlers) : base(handlers) |
| | 82 | | { |
| 42 | 83 | | Initialize(); |
| 42 | 84 | | } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Initializes a new instance of the ComputerVisionClient class. |
| | 88 | | /// </summary> |
| | 89 | | /// <param name='rootHandler'> |
| | 90 | | /// Optional. The http client handler used to handle http transport. |
| | 91 | | /// </param> |
| | 92 | | /// <param name='handlers'> |
| | 93 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 94 | | /// </param> |
| 0 | 95 | | protected ComputerVisionClient(HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : base(rootHa |
| | 96 | | { |
| 0 | 97 | | Initialize(); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Initializes a new instance of the ComputerVisionClient class. |
| | 102 | | /// </summary> |
| | 103 | | /// <param name='credentials'> |
| | 104 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | 105 | | /// </param> |
| | 106 | | /// <param name='handlers'> |
| | 107 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 108 | | /// </param> |
| | 109 | | /// <exception cref="System.ArgumentNullException"> |
| | 110 | | /// Thrown when a required parameter is null |
| | 111 | | /// </exception> |
| 42 | 112 | | public ComputerVisionClient(ServiceClientCredentials credentials, params DelegatingHandler[] handlers) : this(ha |
| | 113 | | { |
| 42 | 114 | | if (credentials == null) |
| | 115 | | { |
| 0 | 116 | | throw new System.ArgumentNullException("credentials"); |
| | 117 | | } |
| 42 | 118 | | Credentials = credentials; |
| 42 | 119 | | if (Credentials != null) |
| | 120 | | { |
| 42 | 121 | | Credentials.InitializeServiceClient(this); |
| | 122 | | } |
| 42 | 123 | | } |
| | 124 | |
|
| | 125 | | /// <summary> |
| | 126 | | /// Initializes a new instance of the ComputerVisionClient class. |
| | 127 | | /// </summary> |
| | 128 | | /// <param name='credentials'> |
| | 129 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | 130 | | /// </param> |
| | 131 | | /// <param name='httpClient'> |
| | 132 | | /// HttpClient to be used |
| | 133 | | /// </param> |
| | 134 | | /// <param name='disposeHttpClient'> |
| | 135 | | /// True: will dispose the provided httpClient on calling ComputerVisionClient.Dispose(). False: will not dispos |
| | 136 | | /// <exception cref="System.ArgumentNullException"> |
| | 137 | | /// Thrown when a required parameter is null |
| | 138 | | /// </exception> |
| 0 | 139 | | public ComputerVisionClient(ServiceClientCredentials credentials, HttpClient httpClient, bool disposeHttpClient) |
| | 140 | | { |
| 0 | 141 | | if (credentials == null) |
| | 142 | | { |
| 0 | 143 | | throw new System.ArgumentNullException("credentials"); |
| | 144 | | } |
| 0 | 145 | | Credentials = credentials; |
| 0 | 146 | | if (Credentials != null) |
| | 147 | | { |
| 0 | 148 | | Credentials.InitializeServiceClient(this); |
| | 149 | | } |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | /// <summary> |
| | 153 | | /// Initializes a new instance of the ComputerVisionClient class. |
| | 154 | | /// </summary> |
| | 155 | | /// <param name='credentials'> |
| | 156 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | 157 | | /// </param> |
| | 158 | | /// <param name='rootHandler'> |
| | 159 | | /// Optional. The http client handler used to handle http transport. |
| | 160 | | /// </param> |
| | 161 | | /// <param name='handlers'> |
| | 162 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 163 | | /// </param> |
| | 164 | | /// <exception cref="System.ArgumentNullException"> |
| | 165 | | /// Thrown when a required parameter is null |
| | 166 | | /// </exception> |
| 0 | 167 | | public ComputerVisionClient(ServiceClientCredentials credentials, HttpClientHandler rootHandler, params Delegati |
| | 168 | | { |
| 0 | 169 | | if (credentials == null) |
| | 170 | | { |
| 0 | 171 | | throw new System.ArgumentNullException("credentials"); |
| | 172 | | } |
| 0 | 173 | | Credentials = credentials; |
| 0 | 174 | | if (Credentials != null) |
| | 175 | | { |
| 0 | 176 | | Credentials.InitializeServiceClient(this); |
| | 177 | | } |
| 0 | 178 | | } |
| | 179 | |
|
| | 180 | | /// <summary> |
| | 181 | | /// An optional partial-method to perform custom initialization. |
| | 182 | | ///</summary> |
| | 183 | | partial void CustomInitialize(); |
| | 184 | | /// <summary> |
| | 185 | | /// Initializes client properties. |
| | 186 | | /// </summary> |
| | 187 | | private void Initialize() |
| | 188 | | { |
| 42 | 189 | | BaseUri = "{Endpoint}/vision/v3.0"; |
| 42 | 190 | | SerializationSettings = new JsonSerializerSettings |
| 42 | 191 | | { |
| 42 | 192 | | Formatting = Newtonsoft.Json.Formatting.Indented, |
| 42 | 193 | | DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, |
| 42 | 194 | | DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, |
| 42 | 195 | | NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, |
| 42 | 196 | | ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, |
| 42 | 197 | | ContractResolver = new ReadOnlyJsonContractResolver(), |
| 42 | 198 | | Converters = new List<JsonConverter> |
| 42 | 199 | | { |
| 42 | 200 | | new Iso8601TimeSpanConverter() |
| 42 | 201 | | } |
| 42 | 202 | | }; |
| 42 | 203 | | SerializationSettings.Converters.Add(new TransformationJsonConverter()); |
| 42 | 204 | | DeserializationSettings = new JsonSerializerSettings |
| 42 | 205 | | { |
| 42 | 206 | | DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, |
| 42 | 207 | | DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, |
| 42 | 208 | | NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, |
| 42 | 209 | | ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, |
| 42 | 210 | | ContractResolver = new ReadOnlyJsonContractResolver(), |
| 42 | 211 | | Converters = new List<JsonConverter> |
| 42 | 212 | | { |
| 42 | 213 | | new Iso8601TimeSpanConverter() |
| 42 | 214 | | } |
| 42 | 215 | | }; |
| | 216 | | CustomInitialize(); |
| 42 | 217 | | DeserializationSettings.Converters.Add(new TransformationJsonConverter()); |
| 42 | 218 | | } |
| | 219 | | /// <summary> |
| | 220 | | /// This operation extracts a rich set of visual features based on the image |
| | 221 | | /// content. |
| | 222 | | /// Two input methods are supported -- (1) Uploading an image or (2) specifying |
| | 223 | | /// an image URL. Within your request, there is an optional parameter to allow |
| | 224 | | /// you to choose which features to return. By default, image categories are |
| | 225 | | /// returned in the response. |
| | 226 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 227 | | /// response will contain an error code and a message to help understand what |
| | 228 | | /// went wrong. |
| | 229 | | /// </summary> |
| | 230 | | /// <param name='url'> |
| | 231 | | /// Publicly reachable URL of an image. |
| | 232 | | /// </param> |
| | 233 | | /// <param name='visualFeatures'> |
| | 234 | | /// A string indicating what visual feature types to return. Multiple values |
| | 235 | | /// should be comma-separated. Valid visual feature types include: Categories - |
| | 236 | | /// categorizes image content according to a taxonomy defined in documentation. |
| | 237 | | /// Tags - tags the image with a detailed list of words related to the image |
| | 238 | | /// content. Description - describes the image content with a complete English |
| | 239 | | /// sentence. Faces - detects if faces are present. If present, generate |
| | 240 | | /// coordinates, gender and age. ImageType - detects if image is clipart or a |
| | 241 | | /// line drawing. Color - determines the accent color, dominant color, and |
| | 242 | | /// whether an image is black&white. Adult - detects if the image is |
| | 243 | | /// pornographic in nature (depicts nudity or a sex act), or is gory (depicts |
| | 244 | | /// extreme violence or blood). Sexually suggestive content (aka racy content) |
| | 245 | | /// is also detected. Objects - detects various objects within an image, |
| | 246 | | /// including the approximate location. The Objects argument is only available |
| | 247 | | /// in English. Brands - detects various brands within an image, including the |
| | 248 | | /// approximate location. The Brands argument is only available in English. |
| | 249 | | /// </param> |
| | 250 | | /// <param name='details'> |
| | 251 | | /// A string indicating which domain-specific details to return. Multiple |
| | 252 | | /// values should be comma-separated. Valid visual feature types include: |
| | 253 | | /// Celebrities - identifies celebrities if detected in the image, Landmarks - |
| | 254 | | /// identifies notable landmarks in the image. |
| | 255 | | /// </param> |
| | 256 | | /// <param name='language'> |
| | 257 | | /// The desired language for output generation. If this parameter is not |
| | 258 | | /// specified, the default value is &quot;en&quot;.Supported |
| | 259 | | /// languages:en - English, Default. es - Spanish, ja - Japanese, pt - |
| | 260 | | /// Portuguese, zh - Simplified Chinese. Possible values include: 'en', 'es', |
| | 261 | | /// 'ja', 'pt', 'zh' |
| | 262 | | /// </param> |
| | 263 | | /// <param name='descriptionExclude'> |
| | 264 | | /// Turn off specified domain models when generating the description. |
| | 265 | | /// </param> |
| | 266 | | /// <param name='customHeaders'> |
| | 267 | | /// Headers that will be added to request. |
| | 268 | | /// </param> |
| | 269 | | /// <param name='cancellationToken'> |
| | 270 | | /// The cancellation token. |
| | 271 | | /// </param> |
| | 272 | | /// <exception cref="ComputerVisionErrorException"> |
| | 273 | | /// Thrown when the operation returned an invalid status code |
| | 274 | | /// </exception> |
| | 275 | | /// <exception cref="SerializationException"> |
| | 276 | | /// Thrown when unable to deserialize the response |
| | 277 | | /// </exception> |
| | 278 | | /// <exception cref="ValidationException"> |
| | 279 | | /// Thrown when a required parameter is null |
| | 280 | | /// </exception> |
| | 281 | | /// <exception cref="System.ArgumentNullException"> |
| | 282 | | /// Thrown when a required parameter is null |
| | 283 | | /// </exception> |
| | 284 | | /// <return> |
| | 285 | | /// A response object containing the response body and response headers. |
| | 286 | | /// </return> |
| | 287 | | public async Task<HttpOperationResponse<ImageAnalysis>> AnalyzeImageWithHttpMessagesAsync(string url, IList<Visu |
| | 288 | | { |
| 4 | 289 | | if (Endpoint == null) |
| | 290 | | { |
| 0 | 291 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 292 | | } |
| 4 | 293 | | if (url == null) |
| | 294 | | { |
| 2 | 295 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 296 | | } |
| 2 | 297 | | ImageUrl imageUrl = new ImageUrl(); |
| 2 | 298 | | if (url != null) |
| | 299 | | { |
| 2 | 300 | | imageUrl.Url = url; |
| | 301 | | } |
| | 302 | | // Tracing |
| 2 | 303 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 304 | | string _invocationId = null; |
| 2 | 305 | | if (_shouldTrace) |
| | 306 | | { |
| 0 | 307 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 308 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 309 | | tracingParameters.Add("visualFeatures", visualFeatures); |
| 0 | 310 | | tracingParameters.Add("details", details); |
| 0 | 311 | | tracingParameters.Add("language", language); |
| 0 | 312 | | tracingParameters.Add("descriptionExclude", descriptionExclude); |
| 0 | 313 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 314 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 315 | | ServiceClientTracing.Enter(_invocationId, this, "AnalyzeImage", tracingParameters); |
| | 316 | | } |
| | 317 | | // Construct URL |
| 2 | 318 | | var _baseUrl = BaseUri; |
| 2 | 319 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "analyze"; |
| 2 | 320 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 321 | | List<string> _queryParameters = new List<string>(); |
| 2 | 322 | | if (visualFeatures != null) |
| | 323 | | { |
| 2 | 324 | | _queryParameters.Add(string.Format("visualFeatures={0}", System.Uri.EscapeDataString(string.Join(",", vi |
| | 325 | | } |
| 2 | 326 | | if (details != null) |
| | 327 | | { |
| 0 | 328 | | _queryParameters.Add(string.Format("details={0}", System.Uri.EscapeDataString(string.Join(",", details)) |
| | 329 | | } |
| 2 | 330 | | if (language != null) |
| | 331 | | { |
| 0 | 332 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(SafeJsonConvert.Serialize |
| | 333 | | } |
| 2 | 334 | | if (descriptionExclude != null) |
| | 335 | | { |
| 0 | 336 | | _queryParameters.Add(string.Format("descriptionExclude={0}", System.Uri.EscapeDataString(string.Join("," |
| | 337 | | } |
| 2 | 338 | | if (_queryParameters.Count > 0) |
| | 339 | | { |
| 2 | 340 | | _url += "?" + string.Join("&", _queryParameters); |
| | 341 | | } |
| | 342 | | // Create HTTP transport objects |
| 2 | 343 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 344 | | HttpResponseMessage _httpResponse = null; |
| 2 | 345 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 346 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 347 | | // Set Headers |
| | 348 | |
|
| | 349 | |
|
| 2 | 350 | | if (customHeaders != null) |
| | 351 | | { |
| 0 | 352 | | foreach(var _header in customHeaders) |
| | 353 | | { |
| 0 | 354 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 355 | | { |
| 0 | 356 | | _httpRequest.Headers.Remove(_header.Key); |
| | 357 | | } |
| 0 | 358 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 359 | | } |
| | 360 | | } |
| | 361 | |
|
| | 362 | | // Serialize Request |
| 2 | 363 | | string _requestContent = null; |
| 2 | 364 | | if(imageUrl != null) |
| | 365 | | { |
| 2 | 366 | | _requestContent = SafeJsonConvert.SerializeObject(imageUrl, SerializationSettings); |
| 2 | 367 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 2 | 368 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 369 | | } |
| | 370 | | // Set Credentials |
| 2 | 371 | | if (Credentials != null) |
| | 372 | | { |
| 2 | 373 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 374 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 375 | | } |
| | 376 | | // Send Request |
| 2 | 377 | | if (_shouldTrace) |
| | 378 | | { |
| 0 | 379 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 380 | | } |
| 2 | 381 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 382 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 383 | | if (_shouldTrace) |
| | 384 | | { |
| 0 | 385 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 386 | | } |
| 2 | 387 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 388 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 389 | | string _responseContent = null; |
| 2 | 390 | | if ((int)_statusCode != 200) |
| | 391 | | { |
| 0 | 392 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 393 | | try |
| | 394 | | { |
| 0 | 395 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 396 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 397 | | if (_errorBody != null) |
| | 398 | | { |
| 0 | 399 | | ex.Body = _errorBody; |
| | 400 | | } |
| 0 | 401 | | } |
| 0 | 402 | | catch (JsonException) |
| | 403 | | { |
| | 404 | | // Ignore the exception |
| 0 | 405 | | } |
| 0 | 406 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 407 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 408 | | if (_shouldTrace) |
| | 409 | | { |
| 0 | 410 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 411 | | } |
| 0 | 412 | | _httpRequest.Dispose(); |
| 0 | 413 | | if (_httpResponse != null) |
| | 414 | | { |
| 0 | 415 | | _httpResponse.Dispose(); |
| | 416 | | } |
| 0 | 417 | | throw ex; |
| | 418 | | } |
| | 419 | | // Create Result |
| 2 | 420 | | var _result = new HttpOperationResponse<ImageAnalysis>(); |
| 2 | 421 | | _result.Request = _httpRequest; |
| 2 | 422 | | _result.Response = _httpResponse; |
| | 423 | | // Deserialize Response |
| 2 | 424 | | if ((int)_statusCode == 200) |
| | 425 | | { |
| 2 | 426 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 427 | | try |
| | 428 | | { |
| 2 | 429 | | _result.Body = SafeJsonConvert.DeserializeObject<ImageAnalysis>(_responseContent, DeserializationSet |
| 2 | 430 | | } |
| 0 | 431 | | catch (JsonException ex) |
| | 432 | | { |
| 0 | 433 | | _httpRequest.Dispose(); |
| 0 | 434 | | if (_httpResponse != null) |
| | 435 | | { |
| 0 | 436 | | _httpResponse.Dispose(); |
| | 437 | | } |
| 0 | 438 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 439 | | } |
| | 440 | | } |
| 2 | 441 | | if (_shouldTrace) |
| | 442 | | { |
| 0 | 443 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 444 | | } |
| 2 | 445 | | return _result; |
| 2 | 446 | | } |
| | 447 | |
|
| | 448 | | /// <summary> |
| | 449 | | /// This operation generates a description of an image in human readable |
| | 450 | | /// language with complete sentences. The description is based on a collection |
| | 451 | | /// of content tags, which are also returned by the operation. More than one |
| | 452 | | /// description can be generated for each image. Descriptions are ordered by |
| | 453 | | /// their confidence score. Descriptions may include results from celebrity and |
| | 454 | | /// landmark domain models, if applicable. |
| | 455 | | /// Two input methods are supported -- (1) Uploading an image or (2) specifying |
| | 456 | | /// an image URL. |
| | 457 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 458 | | /// response will contain an error code and a message to help understand what |
| | 459 | | /// went wrong. |
| | 460 | | /// </summary> |
| | 461 | | /// <param name='url'> |
| | 462 | | /// Publicly reachable URL of an image. |
| | 463 | | /// </param> |
| | 464 | | /// <param name='maxCandidates'> |
| | 465 | | /// Maximum number of candidate descriptions to be returned. The default is 1. |
| | 466 | | /// </param> |
| | 467 | | /// <param name='language'> |
| | 468 | | /// The desired language for output generation. If this parameter is not |
| | 469 | | /// specified, the default value is &quot;en&quot;.Supported |
| | 470 | | /// languages:en - English, Default. es - Spanish, ja - Japanese, pt - |
| | 471 | | /// Portuguese, zh - Simplified Chinese. Possible values include: 'en', 'es', |
| | 472 | | /// 'ja', 'pt', 'zh' |
| | 473 | | /// </param> |
| | 474 | | /// <param name='descriptionExclude'> |
| | 475 | | /// Turn off specified domain models when generating the description. |
| | 476 | | /// </param> |
| | 477 | | /// <param name='customHeaders'> |
| | 478 | | /// Headers that will be added to request. |
| | 479 | | /// </param> |
| | 480 | | /// <param name='cancellationToken'> |
| | 481 | | /// The cancellation token. |
| | 482 | | /// </param> |
| | 483 | | /// <exception cref="ComputerVisionErrorException"> |
| | 484 | | /// Thrown when the operation returned an invalid status code |
| | 485 | | /// </exception> |
| | 486 | | /// <exception cref="SerializationException"> |
| | 487 | | /// Thrown when unable to deserialize the response |
| | 488 | | /// </exception> |
| | 489 | | /// <exception cref="ValidationException"> |
| | 490 | | /// Thrown when a required parameter is null |
| | 491 | | /// </exception> |
| | 492 | | /// <exception cref="System.ArgumentNullException"> |
| | 493 | | /// Thrown when a required parameter is null |
| | 494 | | /// </exception> |
| | 495 | | /// <return> |
| | 496 | | /// A response object containing the response body and response headers. |
| | 497 | | /// </return> |
| | 498 | | public async Task<HttpOperationResponse<ImageDescription>> DescribeImageWithHttpMessagesAsync(string url, int? m |
| | 499 | | { |
| 4 | 500 | | if (Endpoint == null) |
| | 501 | | { |
| 0 | 502 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 503 | | } |
| 4 | 504 | | if (url == null) |
| | 505 | | { |
| 0 | 506 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 507 | | } |
| 4 | 508 | | ImageUrl imageUrl = new ImageUrl(); |
| 4 | 509 | | if (url != null) |
| | 510 | | { |
| 4 | 511 | | imageUrl.Url = url; |
| | 512 | | } |
| | 513 | | // Tracing |
| 4 | 514 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 4 | 515 | | string _invocationId = null; |
| 4 | 516 | | if (_shouldTrace) |
| | 517 | | { |
| 0 | 518 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 519 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 520 | | tracingParameters.Add("maxCandidates", maxCandidates); |
| 0 | 521 | | tracingParameters.Add("language", language); |
| 0 | 522 | | tracingParameters.Add("descriptionExclude", descriptionExclude); |
| 0 | 523 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 524 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 525 | | ServiceClientTracing.Enter(_invocationId, this, "DescribeImage", tracingParameters); |
| | 526 | | } |
| | 527 | | // Construct URL |
| 4 | 528 | | var _baseUrl = BaseUri; |
| 4 | 529 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "describe"; |
| 4 | 530 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 4 | 531 | | List<string> _queryParameters = new List<string>(); |
| 4 | 532 | | if (maxCandidates != null) |
| | 533 | | { |
| 4 | 534 | | _queryParameters.Add(string.Format("maxCandidates={0}", System.Uri.EscapeDataString(SafeJsonConvert.Seri |
| | 535 | | } |
| 4 | 536 | | if (language != null) |
| | 537 | | { |
| 2 | 538 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(SafeJsonConvert.Serialize |
| | 539 | | } |
| 4 | 540 | | if (descriptionExclude != null) |
| | 541 | | { |
| 0 | 542 | | _queryParameters.Add(string.Format("descriptionExclude={0}", System.Uri.EscapeDataString(string.Join("," |
| | 543 | | } |
| 4 | 544 | | if (_queryParameters.Count > 0) |
| | 545 | | { |
| 4 | 546 | | _url += "?" + string.Join("&", _queryParameters); |
| | 547 | | } |
| | 548 | | // Create HTTP transport objects |
| 4 | 549 | | var _httpRequest = new HttpRequestMessage(); |
| 4 | 550 | | HttpResponseMessage _httpResponse = null; |
| 4 | 551 | | _httpRequest.Method = new HttpMethod("POST"); |
| 4 | 552 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 553 | | // Set Headers |
| | 554 | |
|
| | 555 | |
|
| 4 | 556 | | if (customHeaders != null) |
| | 557 | | { |
| 0 | 558 | | foreach(var _header in customHeaders) |
| | 559 | | { |
| 0 | 560 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 561 | | { |
| 0 | 562 | | _httpRequest.Headers.Remove(_header.Key); |
| | 563 | | } |
| 0 | 564 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 565 | | } |
| | 566 | | } |
| | 567 | |
|
| | 568 | | // Serialize Request |
| 4 | 569 | | string _requestContent = null; |
| 4 | 570 | | if(imageUrl != null) |
| | 571 | | { |
| 4 | 572 | | _requestContent = SafeJsonConvert.SerializeObject(imageUrl, SerializationSettings); |
| 4 | 573 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 4 | 574 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 575 | | } |
| | 576 | | // Set Credentials |
| 4 | 577 | | if (Credentials != null) |
| | 578 | | { |
| 4 | 579 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 580 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 581 | | } |
| | 582 | | // Send Request |
| 4 | 583 | | if (_shouldTrace) |
| | 584 | | { |
| 0 | 585 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 586 | | } |
| 4 | 587 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 588 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 4 | 589 | | if (_shouldTrace) |
| | 590 | | { |
| 0 | 591 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 592 | | } |
| 4 | 593 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 4 | 594 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 595 | | string _responseContent = null; |
| 4 | 596 | | if ((int)_statusCode != 200) |
| | 597 | | { |
| 0 | 598 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 599 | | try |
| | 600 | | { |
| 0 | 601 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 602 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 603 | | if (_errorBody != null) |
| | 604 | | { |
| 0 | 605 | | ex.Body = _errorBody; |
| | 606 | | } |
| 0 | 607 | | } |
| 0 | 608 | | catch (JsonException) |
| | 609 | | { |
| | 610 | | // Ignore the exception |
| 0 | 611 | | } |
| 0 | 612 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 613 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 614 | | if (_shouldTrace) |
| | 615 | | { |
| 0 | 616 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 617 | | } |
| 0 | 618 | | _httpRequest.Dispose(); |
| 0 | 619 | | if (_httpResponse != null) |
| | 620 | | { |
| 0 | 621 | | _httpResponse.Dispose(); |
| | 622 | | } |
| 0 | 623 | | throw ex; |
| | 624 | | } |
| | 625 | | // Create Result |
| 4 | 626 | | var _result = new HttpOperationResponse<ImageDescription>(); |
| 4 | 627 | | _result.Request = _httpRequest; |
| 4 | 628 | | _result.Response = _httpResponse; |
| | 629 | | // Deserialize Response |
| 4 | 630 | | if ((int)_statusCode == 200) |
| | 631 | | { |
| 4 | 632 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 633 | | try |
| | 634 | | { |
| 4 | 635 | | _result.Body = SafeJsonConvert.DeserializeObject<ImageDescription>(_responseContent, Deserialization |
| 4 | 636 | | } |
| 0 | 637 | | catch (JsonException ex) |
| | 638 | | { |
| 0 | 639 | | _httpRequest.Dispose(); |
| 0 | 640 | | if (_httpResponse != null) |
| | 641 | | { |
| 0 | 642 | | _httpResponse.Dispose(); |
| | 643 | | } |
| 0 | 644 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 645 | | } |
| | 646 | | } |
| 4 | 647 | | if (_shouldTrace) |
| | 648 | | { |
| 0 | 649 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 650 | | } |
| 4 | 651 | | return _result; |
| 4 | 652 | | } |
| | 653 | |
|
| | 654 | | /// <summary> |
| | 655 | | /// Performs object detection on the specified image. |
| | 656 | | /// Two input methods are supported -- (1) Uploading an image or (2) specifying |
| | 657 | | /// an image URL. |
| | 658 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 659 | | /// response will contain an error code and a message to help understand what |
| | 660 | | /// went wrong. |
| | 661 | | /// </summary> |
| | 662 | | /// <param name='url'> |
| | 663 | | /// Publicly reachable URL of an image. |
| | 664 | | /// </param> |
| | 665 | | /// <param name='customHeaders'> |
| | 666 | | /// Headers that will be added to request. |
| | 667 | | /// </param> |
| | 668 | | /// <param name='cancellationToken'> |
| | 669 | | /// The cancellation token. |
| | 670 | | /// </param> |
| | 671 | | /// <exception cref="ComputerVisionErrorException"> |
| | 672 | | /// Thrown when the operation returned an invalid status code |
| | 673 | | /// </exception> |
| | 674 | | /// <exception cref="SerializationException"> |
| | 675 | | /// Thrown when unable to deserialize the response |
| | 676 | | /// </exception> |
| | 677 | | /// <exception cref="ValidationException"> |
| | 678 | | /// Thrown when a required parameter is null |
| | 679 | | /// </exception> |
| | 680 | | /// <exception cref="System.ArgumentNullException"> |
| | 681 | | /// Thrown when a required parameter is null |
| | 682 | | /// </exception> |
| | 683 | | /// <return> |
| | 684 | | /// A response object containing the response body and response headers. |
| | 685 | | /// </return> |
| | 686 | | public async Task<HttpOperationResponse<DetectResult>> DetectObjectsWithHttpMessagesAsync(string url, Dictionary |
| | 687 | | { |
| 2 | 688 | | if (Endpoint == null) |
| | 689 | | { |
| 0 | 690 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 691 | | } |
| 2 | 692 | | if (url == null) |
| | 693 | | { |
| 0 | 694 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 695 | | } |
| 2 | 696 | | ImageUrl imageUrl = new ImageUrl(); |
| 2 | 697 | | if (url != null) |
| | 698 | | { |
| 2 | 699 | | imageUrl.Url = url; |
| | 700 | | } |
| | 701 | | // Tracing |
| 2 | 702 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 703 | | string _invocationId = null; |
| 2 | 704 | | if (_shouldTrace) |
| | 705 | | { |
| 0 | 706 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 707 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 708 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 709 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 710 | | ServiceClientTracing.Enter(_invocationId, this, "DetectObjects", tracingParameters); |
| | 711 | | } |
| | 712 | | // Construct URL |
| 2 | 713 | | var _baseUrl = BaseUri; |
| 2 | 714 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "detect"; |
| 2 | 715 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | 716 | | // Create HTTP transport objects |
| 2 | 717 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 718 | | HttpResponseMessage _httpResponse = null; |
| 2 | 719 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 720 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 721 | | // Set Headers |
| | 722 | |
|
| | 723 | |
|
| 2 | 724 | | if (customHeaders != null) |
| | 725 | | { |
| 0 | 726 | | foreach(var _header in customHeaders) |
| | 727 | | { |
| 0 | 728 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 729 | | { |
| 0 | 730 | | _httpRequest.Headers.Remove(_header.Key); |
| | 731 | | } |
| 0 | 732 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 733 | | } |
| | 734 | | } |
| | 735 | |
|
| | 736 | | // Serialize Request |
| 2 | 737 | | string _requestContent = null; |
| 2 | 738 | | if(imageUrl != null) |
| | 739 | | { |
| 2 | 740 | | _requestContent = SafeJsonConvert.SerializeObject(imageUrl, SerializationSettings); |
| 2 | 741 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 2 | 742 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 743 | | } |
| | 744 | | // Set Credentials |
| 2 | 745 | | if (Credentials != null) |
| | 746 | | { |
| 2 | 747 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 748 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 749 | | } |
| | 750 | | // Send Request |
| 2 | 751 | | if (_shouldTrace) |
| | 752 | | { |
| 0 | 753 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 754 | | } |
| 2 | 755 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 756 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 757 | | if (_shouldTrace) |
| | 758 | | { |
| 0 | 759 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 760 | | } |
| 2 | 761 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 762 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 763 | | string _responseContent = null; |
| 2 | 764 | | if ((int)_statusCode != 200) |
| | 765 | | { |
| 0 | 766 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 767 | | try |
| | 768 | | { |
| 0 | 769 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 770 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 771 | | if (_errorBody != null) |
| | 772 | | { |
| 0 | 773 | | ex.Body = _errorBody; |
| | 774 | | } |
| 0 | 775 | | } |
| 0 | 776 | | catch (JsonException) |
| | 777 | | { |
| | 778 | | // Ignore the exception |
| 0 | 779 | | } |
| 0 | 780 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 781 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 782 | | if (_shouldTrace) |
| | 783 | | { |
| 0 | 784 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 785 | | } |
| 0 | 786 | | _httpRequest.Dispose(); |
| 0 | 787 | | if (_httpResponse != null) |
| | 788 | | { |
| 0 | 789 | | _httpResponse.Dispose(); |
| | 790 | | } |
| 0 | 791 | | throw ex; |
| | 792 | | } |
| | 793 | | // Create Result |
| 2 | 794 | | var _result = new HttpOperationResponse<DetectResult>(); |
| 2 | 795 | | _result.Request = _httpRequest; |
| 2 | 796 | | _result.Response = _httpResponse; |
| | 797 | | // Deserialize Response |
| 2 | 798 | | if ((int)_statusCode == 200) |
| | 799 | | { |
| 2 | 800 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 801 | | try |
| | 802 | | { |
| 2 | 803 | | _result.Body = SafeJsonConvert.DeserializeObject<DetectResult>(_responseContent, DeserializationSett |
| 2 | 804 | | } |
| 0 | 805 | | catch (JsonException ex) |
| | 806 | | { |
| 0 | 807 | | _httpRequest.Dispose(); |
| 0 | 808 | | if (_httpResponse != null) |
| | 809 | | { |
| 0 | 810 | | _httpResponse.Dispose(); |
| | 811 | | } |
| 0 | 812 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 813 | | } |
| | 814 | | } |
| 2 | 815 | | if (_shouldTrace) |
| | 816 | | { |
| 0 | 817 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 818 | | } |
| 2 | 819 | | return _result; |
| 2 | 820 | | } |
| | 821 | |
|
| | 822 | | /// <summary> |
| | 823 | | /// This operation returns the list of domain-specific models that are |
| | 824 | | /// supported by the Computer Vision API. Currently, the API supports following |
| | 825 | | /// domain-specific models: celebrity recognizer, landmark recognizer. |
| | 826 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 827 | | /// response will contain an error code and a message to help understand what |
| | 828 | | /// went wrong. |
| | 829 | | /// </summary> |
| | 830 | | /// <param name='customHeaders'> |
| | 831 | | /// Headers that will be added to request. |
| | 832 | | /// </param> |
| | 833 | | /// <param name='cancellationToken'> |
| | 834 | | /// The cancellation token. |
| | 835 | | /// </param> |
| | 836 | | /// <exception cref="ComputerVisionErrorException"> |
| | 837 | | /// Thrown when the operation returned an invalid status code |
| | 838 | | /// </exception> |
| | 839 | | /// <exception cref="SerializationException"> |
| | 840 | | /// Thrown when unable to deserialize the response |
| | 841 | | /// </exception> |
| | 842 | | /// <exception cref="ValidationException"> |
| | 843 | | /// Thrown when a required parameter is null |
| | 844 | | /// </exception> |
| | 845 | | /// <exception cref="System.ArgumentNullException"> |
| | 846 | | /// Thrown when a required parameter is null |
| | 847 | | /// </exception> |
| | 848 | | /// <return> |
| | 849 | | /// A response object containing the response body and response headers. |
| | 850 | | /// </return> |
| | 851 | | public async Task<HttpOperationResponse<ListModelsResult>> ListModelsWithHttpMessagesAsync(Dictionary<string, Li |
| | 852 | | { |
| 0 | 853 | | if (Endpoint == null) |
| | 854 | | { |
| 0 | 855 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 856 | | } |
| | 857 | | // Tracing |
| 0 | 858 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 859 | | string _invocationId = null; |
| 0 | 860 | | if (_shouldTrace) |
| | 861 | | { |
| 0 | 862 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 863 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 864 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 865 | | ServiceClientTracing.Enter(_invocationId, this, "ListModels", tracingParameters); |
| | 866 | | } |
| | 867 | | // Construct URL |
| 0 | 868 | | var _baseUrl = BaseUri; |
| 0 | 869 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "models"; |
| 0 | 870 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | 871 | | // Create HTTP transport objects |
| 0 | 872 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 873 | | HttpResponseMessage _httpResponse = null; |
| 0 | 874 | | _httpRequest.Method = new HttpMethod("GET"); |
| 0 | 875 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 876 | | // Set Headers |
| | 877 | |
|
| | 878 | |
|
| 0 | 879 | | if (customHeaders != null) |
| | 880 | | { |
| 0 | 881 | | foreach(var _header in customHeaders) |
| | 882 | | { |
| 0 | 883 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 884 | | { |
| 0 | 885 | | _httpRequest.Headers.Remove(_header.Key); |
| | 886 | | } |
| 0 | 887 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 888 | | } |
| | 889 | | } |
| | 890 | |
|
| | 891 | | // Serialize Request |
| 0 | 892 | | string _requestContent = null; |
| | 893 | | // Set Credentials |
| 0 | 894 | | if (Credentials != null) |
| | 895 | | { |
| 0 | 896 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 897 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 898 | | } |
| | 899 | | // Send Request |
| 0 | 900 | | if (_shouldTrace) |
| | 901 | | { |
| 0 | 902 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 903 | | } |
| 0 | 904 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 905 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 906 | | if (_shouldTrace) |
| | 907 | | { |
| 0 | 908 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 909 | | } |
| 0 | 910 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 911 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 912 | | string _responseContent = null; |
| 0 | 913 | | if ((int)_statusCode != 200) |
| | 914 | | { |
| 0 | 915 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 916 | | try |
| | 917 | | { |
| 0 | 918 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 919 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 920 | | if (_errorBody != null) |
| | 921 | | { |
| 0 | 922 | | ex.Body = _errorBody; |
| | 923 | | } |
| 0 | 924 | | } |
| 0 | 925 | | catch (JsonException) |
| | 926 | | { |
| | 927 | | // Ignore the exception |
| 0 | 928 | | } |
| 0 | 929 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 930 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 931 | | if (_shouldTrace) |
| | 932 | | { |
| 0 | 933 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 934 | | } |
| 0 | 935 | | _httpRequest.Dispose(); |
| 0 | 936 | | if (_httpResponse != null) |
| | 937 | | { |
| 0 | 938 | | _httpResponse.Dispose(); |
| | 939 | | } |
| 0 | 940 | | throw ex; |
| | 941 | | } |
| | 942 | | // Create Result |
| 0 | 943 | | var _result = new HttpOperationResponse<ListModelsResult>(); |
| 0 | 944 | | _result.Request = _httpRequest; |
| 0 | 945 | | _result.Response = _httpResponse; |
| | 946 | | // Deserialize Response |
| 0 | 947 | | if ((int)_statusCode == 200) |
| | 948 | | { |
| 0 | 949 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 950 | | try |
| | 951 | | { |
| 0 | 952 | | _result.Body = SafeJsonConvert.DeserializeObject<ListModelsResult>(_responseContent, Deserialization |
| 0 | 953 | | } |
| 0 | 954 | | catch (JsonException ex) |
| | 955 | | { |
| 0 | 956 | | _httpRequest.Dispose(); |
| 0 | 957 | | if (_httpResponse != null) |
| | 958 | | { |
| 0 | 959 | | _httpResponse.Dispose(); |
| | 960 | | } |
| 0 | 961 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 962 | | } |
| | 963 | | } |
| 0 | 964 | | if (_shouldTrace) |
| | 965 | | { |
| 0 | 966 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 967 | | } |
| 0 | 968 | | return _result; |
| 0 | 969 | | } |
| | 970 | |
|
| | 971 | | /// <summary> |
| | 972 | | /// This operation recognizes content within an image by applying a |
| | 973 | | /// domain-specific model. The list of domain-specific models that are |
| | 974 | | /// supported by the Computer Vision API can be retrieved using the /models GET |
| | 975 | | /// request. Currently, the API provides following domain-specific models: |
| | 976 | | /// celebrities, landmarks. |
| | 977 | | /// Two input methods are supported -- (1) Uploading an image or (2) specifying |
| | 978 | | /// an image URL. |
| | 979 | | /// A successful response will be returned in JSON. |
| | 980 | | /// If the request failed, the response will contain an error code and a |
| | 981 | | /// message to help understand what went wrong. |
| | 982 | | /// </summary> |
| | 983 | | /// <param name='model'> |
| | 984 | | /// The domain-specific content to recognize. |
| | 985 | | /// </param> |
| | 986 | | /// <param name='url'> |
| | 987 | | /// Publicly reachable URL of an image. |
| | 988 | | /// </param> |
| | 989 | | /// <param name='language'> |
| | 990 | | /// The desired language for output generation. If this parameter is not |
| | 991 | | /// specified, the default value is &quot;en&quot;.Supported |
| | 992 | | /// languages:en - English, Default. es - Spanish, ja - Japanese, pt - |
| | 993 | | /// Portuguese, zh - Simplified Chinese. Possible values include: 'en', 'es', |
| | 994 | | /// 'ja', 'pt', 'zh' |
| | 995 | | /// </param> |
| | 996 | | /// <param name='customHeaders'> |
| | 997 | | /// Headers that will be added to request. |
| | 998 | | /// </param> |
| | 999 | | /// <param name='cancellationToken'> |
| | 1000 | | /// The cancellation token. |
| | 1001 | | /// </param> |
| | 1002 | | /// <exception cref="ComputerVisionErrorException"> |
| | 1003 | | /// Thrown when the operation returned an invalid status code |
| | 1004 | | /// </exception> |
| | 1005 | | /// <exception cref="SerializationException"> |
| | 1006 | | /// Thrown when unable to deserialize the response |
| | 1007 | | /// </exception> |
| | 1008 | | /// <exception cref="ValidationException"> |
| | 1009 | | /// Thrown when a required parameter is null |
| | 1010 | | /// </exception> |
| | 1011 | | /// <exception cref="System.ArgumentNullException"> |
| | 1012 | | /// Thrown when a required parameter is null |
| | 1013 | | /// </exception> |
| | 1014 | | /// <return> |
| | 1015 | | /// A response object containing the response body and response headers. |
| | 1016 | | /// </return> |
| | 1017 | | public async Task<HttpOperationResponse<DomainModelResults>> AnalyzeImageByDomainWithHttpMessagesAsync(string mo |
| | 1018 | | { |
| 4 | 1019 | | if (Endpoint == null) |
| | 1020 | | { |
| 0 | 1021 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 1022 | | } |
| 4 | 1023 | | if (model == null) |
| | 1024 | | { |
| 0 | 1025 | | throw new ValidationException(ValidationRules.CannotBeNull, "model"); |
| | 1026 | | } |
| 4 | 1027 | | if (url == null) |
| | 1028 | | { |
| 0 | 1029 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 1030 | | } |
| 4 | 1031 | | ImageUrl imageUrl = new ImageUrl(); |
| 4 | 1032 | | if (url != null) |
| | 1033 | | { |
| 4 | 1034 | | imageUrl.Url = url; |
| | 1035 | | } |
| | 1036 | | // Tracing |
| 4 | 1037 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 4 | 1038 | | string _invocationId = null; |
| 4 | 1039 | | if (_shouldTrace) |
| | 1040 | | { |
| 0 | 1041 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1042 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1043 | | tracingParameters.Add("model", model); |
| 0 | 1044 | | tracingParameters.Add("language", language); |
| 0 | 1045 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 1046 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1047 | | ServiceClientTracing.Enter(_invocationId, this, "AnalyzeImageByDomain", tracingParameters); |
| | 1048 | | } |
| | 1049 | | // Construct URL |
| 4 | 1050 | | var _baseUrl = BaseUri; |
| 4 | 1051 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "models/{model}/analyze"; |
| 4 | 1052 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 4 | 1053 | | _url = _url.Replace("{model}", System.Uri.EscapeDataString(model)); |
| 4 | 1054 | | List<string> _queryParameters = new List<string>(); |
| 4 | 1055 | | if (language != null) |
| | 1056 | | { |
| 2 | 1057 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(SafeJsonConvert.Serialize |
| | 1058 | | } |
| 4 | 1059 | | if (_queryParameters.Count > 0) |
| | 1060 | | { |
| 2 | 1061 | | _url += "?" + string.Join("&", _queryParameters); |
| | 1062 | | } |
| | 1063 | | // Create HTTP transport objects |
| 4 | 1064 | | var _httpRequest = new HttpRequestMessage(); |
| 4 | 1065 | | HttpResponseMessage _httpResponse = null; |
| 4 | 1066 | | _httpRequest.Method = new HttpMethod("POST"); |
| 4 | 1067 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1068 | | // Set Headers |
| | 1069 | |
|
| | 1070 | |
|
| 4 | 1071 | | if (customHeaders != null) |
| | 1072 | | { |
| 0 | 1073 | | foreach(var _header in customHeaders) |
| | 1074 | | { |
| 0 | 1075 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1076 | | { |
| 0 | 1077 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1078 | | } |
| 0 | 1079 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1080 | | } |
| | 1081 | | } |
| | 1082 | |
|
| | 1083 | | // Serialize Request |
| 4 | 1084 | | string _requestContent = null; |
| 4 | 1085 | | if(imageUrl != null) |
| | 1086 | | { |
| 4 | 1087 | | _requestContent = SafeJsonConvert.SerializeObject(imageUrl, SerializationSettings); |
| 4 | 1088 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 4 | 1089 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 1090 | | } |
| | 1091 | | // Set Credentials |
| 4 | 1092 | | if (Credentials != null) |
| | 1093 | | { |
| 4 | 1094 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 1095 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1096 | | } |
| | 1097 | | // Send Request |
| 4 | 1098 | | if (_shouldTrace) |
| | 1099 | | { |
| 0 | 1100 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1101 | | } |
| 4 | 1102 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 1103 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 4 | 1104 | | if (_shouldTrace) |
| | 1105 | | { |
| 0 | 1106 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1107 | | } |
| 4 | 1108 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 4 | 1109 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 1110 | | string _responseContent = null; |
| 4 | 1111 | | if ((int)_statusCode != 200) |
| | 1112 | | { |
| 0 | 1113 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 1114 | | try |
| | 1115 | | { |
| 0 | 1116 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1117 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 1118 | | if (_errorBody != null) |
| | 1119 | | { |
| 0 | 1120 | | ex.Body = _errorBody; |
| | 1121 | | } |
| 0 | 1122 | | } |
| 0 | 1123 | | catch (JsonException) |
| | 1124 | | { |
| | 1125 | | // Ignore the exception |
| 0 | 1126 | | } |
| 0 | 1127 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1128 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1129 | | if (_shouldTrace) |
| | 1130 | | { |
| 0 | 1131 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1132 | | } |
| 0 | 1133 | | _httpRequest.Dispose(); |
| 0 | 1134 | | if (_httpResponse != null) |
| | 1135 | | { |
| 0 | 1136 | | _httpResponse.Dispose(); |
| | 1137 | | } |
| 0 | 1138 | | throw ex; |
| | 1139 | | } |
| | 1140 | | // Create Result |
| 4 | 1141 | | var _result = new HttpOperationResponse<DomainModelResults>(); |
| 4 | 1142 | | _result.Request = _httpRequest; |
| 4 | 1143 | | _result.Response = _httpResponse; |
| | 1144 | | // Deserialize Response |
| 4 | 1145 | | if ((int)_statusCode == 200) |
| | 1146 | | { |
| 4 | 1147 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1148 | | try |
| | 1149 | | { |
| 4 | 1150 | | _result.Body = SafeJsonConvert.DeserializeObject<DomainModelResults>(_responseContent, Deserializati |
| 4 | 1151 | | } |
| 0 | 1152 | | catch (JsonException ex) |
| | 1153 | | { |
| 0 | 1154 | | _httpRequest.Dispose(); |
| 0 | 1155 | | if (_httpResponse != null) |
| | 1156 | | { |
| 0 | 1157 | | _httpResponse.Dispose(); |
| | 1158 | | } |
| 0 | 1159 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1160 | | } |
| | 1161 | | } |
| 4 | 1162 | | if (_shouldTrace) |
| | 1163 | | { |
| 0 | 1164 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1165 | | } |
| 4 | 1166 | | return _result; |
| 4 | 1167 | | } |
| | 1168 | |
|
| | 1169 | | /// <summary> |
| | 1170 | | /// Optical Character Recognition (OCR) detects text in an image and extracts |
| | 1171 | | /// the recognized characters into a machine-usable character stream. |
| | 1172 | | /// Upon success, the OCR results will be returned. |
| | 1173 | | /// Upon failure, the error code together with an error message will be |
| | 1174 | | /// returned. The error code can be one of InvalidImageUrl, InvalidImageFormat, |
| | 1175 | | /// InvalidImageSize, NotSupportedImage, NotSupportedLanguage, or |
| | 1176 | | /// InternalServerError. |
| | 1177 | | /// </summary> |
| | 1178 | | /// <param name='detectOrientation'> |
| | 1179 | | /// Whether detect the text orientation in the image. With |
| | 1180 | | /// detectOrientation=true the OCR service tries to detect the image |
| | 1181 | | /// orientation and correct it before further processing (e.g. if it's |
| | 1182 | | /// upside-down). |
| | 1183 | | /// </param> |
| | 1184 | | /// <param name='url'> |
| | 1185 | | /// Publicly reachable URL of an image. |
| | 1186 | | /// </param> |
| | 1187 | | /// <param name='language'> |
| | 1188 | | /// The BCP-47 language code of the text to be detected in the image. The |
| | 1189 | | /// default value is 'unk'. Possible values include: 'unk', 'zh-Hans', |
| | 1190 | | /// 'zh-Hant', 'cs', 'da', 'nl', 'en', 'fi', 'fr', 'de', 'el', 'hu', 'it', |
| | 1191 | | /// 'ja', 'ko', 'nb', 'pl', 'pt', 'ru', 'es', 'sv', 'tr', 'ar', 'ro', |
| | 1192 | | /// 'sr-Cyrl', 'sr-Latn', 'sk' |
| | 1193 | | /// </param> |
| | 1194 | | /// <param name='customHeaders'> |
| | 1195 | | /// Headers that will be added to request. |
| | 1196 | | /// </param> |
| | 1197 | | /// <param name='cancellationToken'> |
| | 1198 | | /// The cancellation token. |
| | 1199 | | /// </param> |
| | 1200 | | /// <exception cref="ComputerVisionErrorException"> |
| | 1201 | | /// Thrown when the operation returned an invalid status code |
| | 1202 | | /// </exception> |
| | 1203 | | /// <exception cref="SerializationException"> |
| | 1204 | | /// Thrown when unable to deserialize the response |
| | 1205 | | /// </exception> |
| | 1206 | | /// <exception cref="ValidationException"> |
| | 1207 | | /// Thrown when a required parameter is null |
| | 1208 | | /// </exception> |
| | 1209 | | /// <exception cref="System.ArgumentNullException"> |
| | 1210 | | /// Thrown when a required parameter is null |
| | 1211 | | /// </exception> |
| | 1212 | | /// <return> |
| | 1213 | | /// A response object containing the response body and response headers. |
| | 1214 | | /// </return> |
| | 1215 | | public async Task<HttpOperationResponse<OcrResult>> RecognizePrintedTextWithHttpMessagesAsync(bool detectOrienta |
| | 1216 | | { |
| 2 | 1217 | | if (Endpoint == null) |
| | 1218 | | { |
| 0 | 1219 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 1220 | | } |
| 2 | 1221 | | if (url == null) |
| | 1222 | | { |
| 0 | 1223 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 1224 | | } |
| 2 | 1225 | | ImageUrl imageUrl = new ImageUrl(); |
| 2 | 1226 | | if (url != null) |
| | 1227 | | { |
| 2 | 1228 | | imageUrl.Url = url; |
| | 1229 | | } |
| | 1230 | | // Tracing |
| 2 | 1231 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 1232 | | string _invocationId = null; |
| 2 | 1233 | | if (_shouldTrace) |
| | 1234 | | { |
| 0 | 1235 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1236 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1237 | | tracingParameters.Add("detectOrientation", detectOrientation); |
| 0 | 1238 | | tracingParameters.Add("language", language); |
| 0 | 1239 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 1240 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1241 | | ServiceClientTracing.Enter(_invocationId, this, "RecognizePrintedText", tracingParameters); |
| | 1242 | | } |
| | 1243 | | // Construct URL |
| 2 | 1244 | | var _baseUrl = BaseUri; |
| 2 | 1245 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "ocr"; |
| 2 | 1246 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 1247 | | List<string> _queryParameters = new List<string>(); |
| 2 | 1248 | | _queryParameters.Add(string.Format("detectOrientation={0}", System.Uri.EscapeDataString(SafeJsonConvert.Seri |
| 2 | 1249 | | if (language != null) |
| | 1250 | | { |
| 2 | 1251 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(SafeJsonConvert.Serialize |
| | 1252 | | } |
| 2 | 1253 | | if (_queryParameters.Count > 0) |
| | 1254 | | { |
| 2 | 1255 | | _url += "?" + string.Join("&", _queryParameters); |
| | 1256 | | } |
| | 1257 | | // Create HTTP transport objects |
| 2 | 1258 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 1259 | | HttpResponseMessage _httpResponse = null; |
| 2 | 1260 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 1261 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1262 | | // Set Headers |
| | 1263 | |
|
| | 1264 | |
|
| 2 | 1265 | | if (customHeaders != null) |
| | 1266 | | { |
| 0 | 1267 | | foreach(var _header in customHeaders) |
| | 1268 | | { |
| 0 | 1269 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1270 | | { |
| 0 | 1271 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1272 | | } |
| 0 | 1273 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1274 | | } |
| | 1275 | | } |
| | 1276 | |
|
| | 1277 | | // Serialize Request |
| 2 | 1278 | | string _requestContent = null; |
| 2 | 1279 | | if(imageUrl != null) |
| | 1280 | | { |
| 2 | 1281 | | _requestContent = SafeJsonConvert.SerializeObject(imageUrl, SerializationSettings); |
| 2 | 1282 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 2 | 1283 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 1284 | | } |
| | 1285 | | // Set Credentials |
| 2 | 1286 | | if (Credentials != null) |
| | 1287 | | { |
| 2 | 1288 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1289 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1290 | | } |
| | 1291 | | // Send Request |
| 2 | 1292 | | if (_shouldTrace) |
| | 1293 | | { |
| 0 | 1294 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1295 | | } |
| 2 | 1296 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1297 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 1298 | | if (_shouldTrace) |
| | 1299 | | { |
| 0 | 1300 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1301 | | } |
| 2 | 1302 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 1303 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1304 | | string _responseContent = null; |
| 2 | 1305 | | if ((int)_statusCode != 200) |
| | 1306 | | { |
| 0 | 1307 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 1308 | | try |
| | 1309 | | { |
| 0 | 1310 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1311 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 1312 | | if (_errorBody != null) |
| | 1313 | | { |
| 0 | 1314 | | ex.Body = _errorBody; |
| | 1315 | | } |
| 0 | 1316 | | } |
| 0 | 1317 | | catch (JsonException) |
| | 1318 | | { |
| | 1319 | | // Ignore the exception |
| 0 | 1320 | | } |
| 0 | 1321 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1322 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1323 | | if (_shouldTrace) |
| | 1324 | | { |
| 0 | 1325 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1326 | | } |
| 0 | 1327 | | _httpRequest.Dispose(); |
| 0 | 1328 | | if (_httpResponse != null) |
| | 1329 | | { |
| 0 | 1330 | | _httpResponse.Dispose(); |
| | 1331 | | } |
| 0 | 1332 | | throw ex; |
| | 1333 | | } |
| | 1334 | | // Create Result |
| 2 | 1335 | | var _result = new HttpOperationResponse<OcrResult>(); |
| 2 | 1336 | | _result.Request = _httpRequest; |
| 2 | 1337 | | _result.Response = _httpResponse; |
| | 1338 | | // Deserialize Response |
| 2 | 1339 | | if ((int)_statusCode == 200) |
| | 1340 | | { |
| 2 | 1341 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1342 | | try |
| | 1343 | | { |
| 2 | 1344 | | _result.Body = SafeJsonConvert.DeserializeObject<OcrResult>(_responseContent, DeserializationSetting |
| 2 | 1345 | | } |
| 0 | 1346 | | catch (JsonException ex) |
| | 1347 | | { |
| 0 | 1348 | | _httpRequest.Dispose(); |
| 0 | 1349 | | if (_httpResponse != null) |
| | 1350 | | { |
| 0 | 1351 | | _httpResponse.Dispose(); |
| | 1352 | | } |
| 0 | 1353 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1354 | | } |
| | 1355 | | } |
| 2 | 1356 | | if (_shouldTrace) |
| | 1357 | | { |
| 0 | 1358 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1359 | | } |
| 2 | 1360 | | return _result; |
| 2 | 1361 | | } |
| | 1362 | |
|
| | 1363 | | /// <summary> |
| | 1364 | | /// This operation generates a list of words, or tags, that are relevant to the |
| | 1365 | | /// content of the supplied image. The Computer Vision API can return tags |
| | 1366 | | /// based on objects, living beings, scenery or actions found in images. Unlike |
| | 1367 | | /// categories, tags are not organized according to a hierarchical |
| | 1368 | | /// classification system, but correspond to image content. Tags may contain |
| | 1369 | | /// hints to avoid ambiguity or provide context, for example the tag |
| | 1370 | | /// "ascomycete" may be accompanied by the hint "fungus". |
| | 1371 | | /// Two input methods are supported -- (1) Uploading an image or (2) specifying |
| | 1372 | | /// an image URL. |
| | 1373 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 1374 | | /// response will contain an error code and a message to help understand what |
| | 1375 | | /// went wrong. |
| | 1376 | | /// </summary> |
| | 1377 | | /// <param name='url'> |
| | 1378 | | /// Publicly reachable URL of an image. |
| | 1379 | | /// </param> |
| | 1380 | | /// <param name='language'> |
| | 1381 | | /// The desired language for output generation. If this parameter is not |
| | 1382 | | /// specified, the default value is &quot;en&quot;.Supported |
| | 1383 | | /// languages:en - English, Default. es - Spanish, ja - Japanese, pt - |
| | 1384 | | /// Portuguese, zh - Simplified Chinese. Possible values include: 'en', 'es', |
| | 1385 | | /// 'ja', 'pt', 'zh' |
| | 1386 | | /// </param> |
| | 1387 | | /// <param name='customHeaders'> |
| | 1388 | | /// Headers that will be added to request. |
| | 1389 | | /// </param> |
| | 1390 | | /// <param name='cancellationToken'> |
| | 1391 | | /// The cancellation token. |
| | 1392 | | /// </param> |
| | 1393 | | /// <exception cref="ComputerVisionErrorException"> |
| | 1394 | | /// Thrown when the operation returned an invalid status code |
| | 1395 | | /// </exception> |
| | 1396 | | /// <exception cref="SerializationException"> |
| | 1397 | | /// Thrown when unable to deserialize the response |
| | 1398 | | /// </exception> |
| | 1399 | | /// <exception cref="ValidationException"> |
| | 1400 | | /// Thrown when a required parameter is null |
| | 1401 | | /// </exception> |
| | 1402 | | /// <exception cref="System.ArgumentNullException"> |
| | 1403 | | /// Thrown when a required parameter is null |
| | 1404 | | /// </exception> |
| | 1405 | | /// <return> |
| | 1406 | | /// A response object containing the response body and response headers. |
| | 1407 | | /// </return> |
| | 1408 | | public async Task<HttpOperationResponse<TagResult>> TagImageWithHttpMessagesAsync(string url, string language = |
| | 1409 | | { |
| 2 | 1410 | | if (Endpoint == null) |
| | 1411 | | { |
| 0 | 1412 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 1413 | | } |
| 2 | 1414 | | if (url == null) |
| | 1415 | | { |
| 0 | 1416 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 1417 | | } |
| 2 | 1418 | | ImageUrl imageUrl = new ImageUrl(); |
| 2 | 1419 | | if (url != null) |
| | 1420 | | { |
| 2 | 1421 | | imageUrl.Url = url; |
| | 1422 | | } |
| | 1423 | | // Tracing |
| 2 | 1424 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 1425 | | string _invocationId = null; |
| 2 | 1426 | | if (_shouldTrace) |
| | 1427 | | { |
| 0 | 1428 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1429 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1430 | | tracingParameters.Add("language", language); |
| 0 | 1431 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 1432 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1433 | | ServiceClientTracing.Enter(_invocationId, this, "TagImage", tracingParameters); |
| | 1434 | | } |
| | 1435 | | // Construct URL |
| 2 | 1436 | | var _baseUrl = BaseUri; |
| 2 | 1437 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "tag"; |
| 2 | 1438 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 1439 | | List<string> _queryParameters = new List<string>(); |
| 2 | 1440 | | if (language != null) |
| | 1441 | | { |
| 0 | 1442 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(SafeJsonConvert.Serialize |
| | 1443 | | } |
| 2 | 1444 | | if (_queryParameters.Count > 0) |
| | 1445 | | { |
| 0 | 1446 | | _url += "?" + string.Join("&", _queryParameters); |
| | 1447 | | } |
| | 1448 | | // Create HTTP transport objects |
| 2 | 1449 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 1450 | | HttpResponseMessage _httpResponse = null; |
| 2 | 1451 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 1452 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1453 | | // Set Headers |
| | 1454 | |
|
| | 1455 | |
|
| 2 | 1456 | | if (customHeaders != null) |
| | 1457 | | { |
| 0 | 1458 | | foreach(var _header in customHeaders) |
| | 1459 | | { |
| 0 | 1460 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1461 | | { |
| 0 | 1462 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1463 | | } |
| 0 | 1464 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1465 | | } |
| | 1466 | | } |
| | 1467 | |
|
| | 1468 | | // Serialize Request |
| 2 | 1469 | | string _requestContent = null; |
| 2 | 1470 | | if(imageUrl != null) |
| | 1471 | | { |
| 2 | 1472 | | _requestContent = SafeJsonConvert.SerializeObject(imageUrl, SerializationSettings); |
| 2 | 1473 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 2 | 1474 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 1475 | | } |
| | 1476 | | // Set Credentials |
| 2 | 1477 | | if (Credentials != null) |
| | 1478 | | { |
| 2 | 1479 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1480 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1481 | | } |
| | 1482 | | // Send Request |
| 2 | 1483 | | if (_shouldTrace) |
| | 1484 | | { |
| 0 | 1485 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1486 | | } |
| 2 | 1487 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1488 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 1489 | | if (_shouldTrace) |
| | 1490 | | { |
| 0 | 1491 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1492 | | } |
| 2 | 1493 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 1494 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1495 | | string _responseContent = null; |
| 2 | 1496 | | if ((int)_statusCode != 200) |
| | 1497 | | { |
| 0 | 1498 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 1499 | | try |
| | 1500 | | { |
| 0 | 1501 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1502 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 1503 | | if (_errorBody != null) |
| | 1504 | | { |
| 0 | 1505 | | ex.Body = _errorBody; |
| | 1506 | | } |
| 0 | 1507 | | } |
| 0 | 1508 | | catch (JsonException) |
| | 1509 | | { |
| | 1510 | | // Ignore the exception |
| 0 | 1511 | | } |
| 0 | 1512 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1513 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1514 | | if (_shouldTrace) |
| | 1515 | | { |
| 0 | 1516 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1517 | | } |
| 0 | 1518 | | _httpRequest.Dispose(); |
| 0 | 1519 | | if (_httpResponse != null) |
| | 1520 | | { |
| 0 | 1521 | | _httpResponse.Dispose(); |
| | 1522 | | } |
| 0 | 1523 | | throw ex; |
| | 1524 | | } |
| | 1525 | | // Create Result |
| 2 | 1526 | | var _result = new HttpOperationResponse<TagResult>(); |
| 2 | 1527 | | _result.Request = _httpRequest; |
| 2 | 1528 | | _result.Response = _httpResponse; |
| | 1529 | | // Deserialize Response |
| 2 | 1530 | | if ((int)_statusCode == 200) |
| | 1531 | | { |
| 2 | 1532 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1533 | | try |
| | 1534 | | { |
| 2 | 1535 | | _result.Body = SafeJsonConvert.DeserializeObject<TagResult>(_responseContent, DeserializationSetting |
| 2 | 1536 | | } |
| 0 | 1537 | | catch (JsonException ex) |
| | 1538 | | { |
| 0 | 1539 | | _httpRequest.Dispose(); |
| 0 | 1540 | | if (_httpResponse != null) |
| | 1541 | | { |
| 0 | 1542 | | _httpResponse.Dispose(); |
| | 1543 | | } |
| 0 | 1544 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1545 | | } |
| | 1546 | | } |
| 2 | 1547 | | if (_shouldTrace) |
| | 1548 | | { |
| 0 | 1549 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1550 | | } |
| 2 | 1551 | | return _result; |
| 2 | 1552 | | } |
| | 1553 | |
|
| | 1554 | | /// <summary> |
| | 1555 | | /// This operation generates a thumbnail image with the user-specified width |
| | 1556 | | /// and height. By default, the service analyzes the image, identifies the |
| | 1557 | | /// region of interest (ROI), and generates smart cropping coordinates based on |
| | 1558 | | /// the ROI. Smart cropping helps when you specify an aspect ratio that differs |
| | 1559 | | /// from that of the input image. |
| | 1560 | | /// A successful response contains the thumbnail image binary. If the request |
| | 1561 | | /// failed, the response contains an error code and a message to help determine |
| | 1562 | | /// what went wrong. |
| | 1563 | | /// Upon failure, the error code and an error message are returned. The error |
| | 1564 | | /// code could be one of InvalidImageUrl, InvalidImageFormat, InvalidImageSize, |
| | 1565 | | /// InvalidThumbnailSize, NotSupportedImage, FailedToProcess, Timeout, or |
| | 1566 | | /// InternalServerError. |
| | 1567 | | /// </summary> |
| | 1568 | | /// <param name='width'> |
| | 1569 | | /// Width of the thumbnail, in pixels. It must be between 1 and 1024. |
| | 1570 | | /// Recommended minimum of 50. |
| | 1571 | | /// </param> |
| | 1572 | | /// <param name='height'> |
| | 1573 | | /// Height of the thumbnail, in pixels. It must be between 1 and 1024. |
| | 1574 | | /// Recommended minimum of 50. |
| | 1575 | | /// </param> |
| | 1576 | | /// <param name='url'> |
| | 1577 | | /// Publicly reachable URL of an image. |
| | 1578 | | /// </param> |
| | 1579 | | /// <param name='smartCropping'> |
| | 1580 | | /// Boolean flag for enabling smart cropping. |
| | 1581 | | /// </param> |
| | 1582 | | /// <param name='customHeaders'> |
| | 1583 | | /// Headers that will be added to request. |
| | 1584 | | /// </param> |
| | 1585 | | /// <param name='cancellationToken'> |
| | 1586 | | /// The cancellation token. |
| | 1587 | | /// </param> |
| | 1588 | | /// <exception cref="HttpOperationException"> |
| | 1589 | | /// Thrown when the operation returned an invalid status code |
| | 1590 | | /// </exception> |
| | 1591 | | /// <exception cref="SerializationException"> |
| | 1592 | | /// Thrown when unable to deserialize the response |
| | 1593 | | /// </exception> |
| | 1594 | | /// <exception cref="ValidationException"> |
| | 1595 | | /// Thrown when a required parameter is null |
| | 1596 | | /// </exception> |
| | 1597 | | /// <exception cref="System.ArgumentNullException"> |
| | 1598 | | /// Thrown when a required parameter is null |
| | 1599 | | /// </exception> |
| | 1600 | | /// <return> |
| | 1601 | | /// A response object containing the response body and response headers. |
| | 1602 | | /// </return> |
| | 1603 | | public async Task<HttpOperationResponse<Stream>> GenerateThumbnailWithHttpMessagesAsync(int width, int height, s |
| | 1604 | | { |
| 10 | 1605 | | if (Endpoint == null) |
| | 1606 | | { |
| 0 | 1607 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 1608 | | } |
| 10 | 1609 | | if (width > 1024) |
| | 1610 | | { |
| 2 | 1611 | | throw new ValidationException(ValidationRules.InclusiveMaximum, "width", 1024); |
| | 1612 | | } |
| 8 | 1613 | | if (width < 1) |
| | 1614 | | { |
| 2 | 1615 | | throw new ValidationException(ValidationRules.InclusiveMinimum, "width", 1); |
| | 1616 | | } |
| 6 | 1617 | | if (height > 1024) |
| | 1618 | | { |
| 2 | 1619 | | throw new ValidationException(ValidationRules.InclusiveMaximum, "height", 1024); |
| | 1620 | | } |
| 4 | 1621 | | if (height < 1) |
| | 1622 | | { |
| 2 | 1623 | | throw new ValidationException(ValidationRules.InclusiveMinimum, "height", 1); |
| | 1624 | | } |
| 2 | 1625 | | if (url == null) |
| | 1626 | | { |
| 0 | 1627 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 1628 | | } |
| 2 | 1629 | | ImageUrl imageUrl = new ImageUrl(); |
| 2 | 1630 | | if (url != null) |
| | 1631 | | { |
| 2 | 1632 | | imageUrl.Url = url; |
| | 1633 | | } |
| | 1634 | | // Tracing |
| 2 | 1635 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 1636 | | string _invocationId = null; |
| 2 | 1637 | | if (_shouldTrace) |
| | 1638 | | { |
| 0 | 1639 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1640 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1641 | | tracingParameters.Add("width", width); |
| 0 | 1642 | | tracingParameters.Add("height", height); |
| 0 | 1643 | | tracingParameters.Add("smartCropping", smartCropping); |
| 0 | 1644 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 1645 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1646 | | ServiceClientTracing.Enter(_invocationId, this, "GenerateThumbnail", tracingParameters); |
| | 1647 | | } |
| | 1648 | | // Construct URL |
| 2 | 1649 | | var _baseUrl = BaseUri; |
| 2 | 1650 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "generateThumbnail"; |
| 2 | 1651 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 1652 | | List<string> _queryParameters = new List<string>(); |
| 2 | 1653 | | _queryParameters.Add(string.Format("width={0}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject( |
| 2 | 1654 | | _queryParameters.Add(string.Format("height={0}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject |
| 2 | 1655 | | if (smartCropping != null) |
| | 1656 | | { |
| 2 | 1657 | | _queryParameters.Add(string.Format("smartCropping={0}", System.Uri.EscapeDataString(SafeJsonConvert.Seri |
| | 1658 | | } |
| 2 | 1659 | | if (_queryParameters.Count > 0) |
| | 1660 | | { |
| 2 | 1661 | | _url += "?" + string.Join("&", _queryParameters); |
| | 1662 | | } |
| | 1663 | | // Create HTTP transport objects |
| 2 | 1664 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 1665 | | HttpResponseMessage _httpResponse = null; |
| 2 | 1666 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 1667 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1668 | | // Set Headers |
| | 1669 | |
|
| | 1670 | |
|
| 2 | 1671 | | if (customHeaders != null) |
| | 1672 | | { |
| 0 | 1673 | | foreach(var _header in customHeaders) |
| | 1674 | | { |
| 0 | 1675 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1676 | | { |
| 0 | 1677 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1678 | | } |
| 0 | 1679 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1680 | | } |
| | 1681 | | } |
| | 1682 | |
|
| | 1683 | | // Serialize Request |
| 2 | 1684 | | string _requestContent = null; |
| 2 | 1685 | | if(imageUrl != null) |
| | 1686 | | { |
| 2 | 1687 | | _requestContent = SafeJsonConvert.SerializeObject(imageUrl, SerializationSettings); |
| 2 | 1688 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 2 | 1689 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 1690 | | } |
| | 1691 | | // Set Credentials |
| 2 | 1692 | | if (Credentials != null) |
| | 1693 | | { |
| 2 | 1694 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1695 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1696 | | } |
| | 1697 | | // Send Request |
| 2 | 1698 | | if (_shouldTrace) |
| | 1699 | | { |
| 0 | 1700 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1701 | | } |
| 2 | 1702 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1703 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, System.Net.Http.HttpCompletionOption.ResponseHeader |
| 2 | 1704 | | if (_shouldTrace) |
| | 1705 | | { |
| 0 | 1706 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1707 | | } |
| 2 | 1708 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 1709 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1710 | | string _responseContent = null; |
| 2 | 1711 | | if ((int)_statusCode != 200) |
| | 1712 | | { |
| 0 | 1713 | | var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _st |
| 0 | 1714 | | if (_httpResponse.Content != null) { |
| 0 | 1715 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1716 | | } |
| | 1717 | | else { |
| 0 | 1718 | | _responseContent = string.Empty; |
| | 1719 | | } |
| 0 | 1720 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1721 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1722 | | if (_shouldTrace) |
| | 1723 | | { |
| 0 | 1724 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1725 | | } |
| 0 | 1726 | | _httpRequest.Dispose(); |
| 0 | 1727 | | if (_httpResponse != null) |
| | 1728 | | { |
| 0 | 1729 | | _httpResponse.Dispose(); |
| | 1730 | | } |
| 0 | 1731 | | throw ex; |
| | 1732 | | } |
| | 1733 | | // Create Result |
| 2 | 1734 | | var _result = new HttpOperationResponse<Stream>(); |
| 2 | 1735 | | _result.Request = _httpRequest; |
| 2 | 1736 | | _result.Response = _httpResponse; |
| | 1737 | | // Deserialize Response |
| 2 | 1738 | | if ((int)_statusCode == 200) |
| | 1739 | | { |
| 2 | 1740 | | _result.Body = await _httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false); |
| | 1741 | | } |
| 2 | 1742 | | if (_shouldTrace) |
| | 1743 | | { |
| 0 | 1744 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1745 | | } |
| 2 | 1746 | | return _result; |
| 2 | 1747 | | } |
| | 1748 | |
|
| | 1749 | | /// <summary> |
| | 1750 | | /// This operation returns a bounding box around the most important area of the |
| | 1751 | | /// image. |
| | 1752 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 1753 | | /// response contains an error code and a message to help determine what went |
| | 1754 | | /// wrong. |
| | 1755 | | /// Upon failure, the error code and an error message are returned. The error |
| | 1756 | | /// code could be one of InvalidImageUrl, InvalidImageFormat, InvalidImageSize, |
| | 1757 | | /// NotSupportedImage, FailedToProcess, Timeout, or InternalServerError. |
| | 1758 | | /// </summary> |
| | 1759 | | /// <param name='url'> |
| | 1760 | | /// Publicly reachable URL of an image. |
| | 1761 | | /// </param> |
| | 1762 | | /// <param name='customHeaders'> |
| | 1763 | | /// Headers that will be added to request. |
| | 1764 | | /// </param> |
| | 1765 | | /// <param name='cancellationToken'> |
| | 1766 | | /// The cancellation token. |
| | 1767 | | /// </param> |
| | 1768 | | /// <exception cref="ComputerVisionErrorException"> |
| | 1769 | | /// Thrown when the operation returned an invalid status code |
| | 1770 | | /// </exception> |
| | 1771 | | /// <exception cref="SerializationException"> |
| | 1772 | | /// Thrown when unable to deserialize the response |
| | 1773 | | /// </exception> |
| | 1774 | | /// <exception cref="ValidationException"> |
| | 1775 | | /// Thrown when a required parameter is null |
| | 1776 | | /// </exception> |
| | 1777 | | /// <exception cref="System.ArgumentNullException"> |
| | 1778 | | /// Thrown when a required parameter is null |
| | 1779 | | /// </exception> |
| | 1780 | | /// <return> |
| | 1781 | | /// A response object containing the response body and response headers. |
| | 1782 | | /// </return> |
| | 1783 | | public async Task<HttpOperationResponse<AreaOfInterestResult>> GetAreaOfInterestWithHttpMessagesAsync(string url |
| | 1784 | | { |
| 2 | 1785 | | if (Endpoint == null) |
| | 1786 | | { |
| 0 | 1787 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 1788 | | } |
| 2 | 1789 | | if (url == null) |
| | 1790 | | { |
| 0 | 1791 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 1792 | | } |
| 2 | 1793 | | ImageUrl imageUrl = new ImageUrl(); |
| 2 | 1794 | | if (url != null) |
| | 1795 | | { |
| 2 | 1796 | | imageUrl.Url = url; |
| | 1797 | | } |
| | 1798 | | // Tracing |
| 2 | 1799 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 1800 | | string _invocationId = null; |
| 2 | 1801 | | if (_shouldTrace) |
| | 1802 | | { |
| 0 | 1803 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1804 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1805 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 1806 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1807 | | ServiceClientTracing.Enter(_invocationId, this, "GetAreaOfInterest", tracingParameters); |
| | 1808 | | } |
| | 1809 | | // Construct URL |
| 2 | 1810 | | var _baseUrl = BaseUri; |
| 2 | 1811 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "areaOfInterest"; |
| 2 | 1812 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | 1813 | | // Create HTTP transport objects |
| 2 | 1814 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 1815 | | HttpResponseMessage _httpResponse = null; |
| 2 | 1816 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 1817 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1818 | | // Set Headers |
| | 1819 | |
|
| | 1820 | |
|
| 2 | 1821 | | if (customHeaders != null) |
| | 1822 | | { |
| 0 | 1823 | | foreach(var _header in customHeaders) |
| | 1824 | | { |
| 0 | 1825 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1826 | | { |
| 0 | 1827 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1828 | | } |
| 0 | 1829 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1830 | | } |
| | 1831 | | } |
| | 1832 | |
|
| | 1833 | | // Serialize Request |
| 2 | 1834 | | string _requestContent = null; |
| 2 | 1835 | | if(imageUrl != null) |
| | 1836 | | { |
| 2 | 1837 | | _requestContent = SafeJsonConvert.SerializeObject(imageUrl, SerializationSettings); |
| 2 | 1838 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 2 | 1839 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 1840 | | } |
| | 1841 | | // Set Credentials |
| 2 | 1842 | | if (Credentials != null) |
| | 1843 | | { |
| 2 | 1844 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1845 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1846 | | } |
| | 1847 | | // Send Request |
| 2 | 1848 | | if (_shouldTrace) |
| | 1849 | | { |
| 0 | 1850 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1851 | | } |
| 2 | 1852 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1853 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 1854 | | if (_shouldTrace) |
| | 1855 | | { |
| 0 | 1856 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1857 | | } |
| 2 | 1858 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 1859 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1860 | | string _responseContent = null; |
| 2 | 1861 | | if ((int)_statusCode != 200) |
| | 1862 | | { |
| 0 | 1863 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 1864 | | try |
| | 1865 | | { |
| 0 | 1866 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1867 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 1868 | | if (_errorBody != null) |
| | 1869 | | { |
| 0 | 1870 | | ex.Body = _errorBody; |
| | 1871 | | } |
| 0 | 1872 | | } |
| 0 | 1873 | | catch (JsonException) |
| | 1874 | | { |
| | 1875 | | // Ignore the exception |
| 0 | 1876 | | } |
| 0 | 1877 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1878 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1879 | | if (_shouldTrace) |
| | 1880 | | { |
| 0 | 1881 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1882 | | } |
| 0 | 1883 | | _httpRequest.Dispose(); |
| 0 | 1884 | | if (_httpResponse != null) |
| | 1885 | | { |
| 0 | 1886 | | _httpResponse.Dispose(); |
| | 1887 | | } |
| 0 | 1888 | | throw ex; |
| | 1889 | | } |
| | 1890 | | // Create Result |
| 2 | 1891 | | var _result = new HttpOperationResponse<AreaOfInterestResult>(); |
| 2 | 1892 | | _result.Request = _httpRequest; |
| 2 | 1893 | | _result.Response = _httpResponse; |
| | 1894 | | // Deserialize Response |
| 2 | 1895 | | if ((int)_statusCode == 200) |
| | 1896 | | { |
| 2 | 1897 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1898 | | try |
| | 1899 | | { |
| 2 | 1900 | | _result.Body = SafeJsonConvert.DeserializeObject<AreaOfInterestResult>(_responseContent, Deserializa |
| 2 | 1901 | | } |
| 0 | 1902 | | catch (JsonException ex) |
| | 1903 | | { |
| 0 | 1904 | | _httpRequest.Dispose(); |
| 0 | 1905 | | if (_httpResponse != null) |
| | 1906 | | { |
| 0 | 1907 | | _httpResponse.Dispose(); |
| | 1908 | | } |
| 0 | 1909 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1910 | | } |
| | 1911 | | } |
| 2 | 1912 | | if (_shouldTrace) |
| | 1913 | | { |
| 0 | 1914 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1915 | | } |
| 2 | 1916 | | return _result; |
| 2 | 1917 | | } |
| | 1918 | |
|
| | 1919 | | /// <summary> |
| | 1920 | | /// This operation extracts a rich set of visual features based on the image |
| | 1921 | | /// content. |
| | 1922 | | /// Two input methods are supported -- (1) Uploading an image or (2) specifying |
| | 1923 | | /// an image URL. Within your request, there is an optional parameter to allow |
| | 1924 | | /// you to choose which features to return. By default, image categories are |
| | 1925 | | /// returned in the response. |
| | 1926 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 1927 | | /// response will contain an error code and a message to help understand what |
| | 1928 | | /// went wrong. |
| | 1929 | | /// </summary> |
| | 1930 | | /// <param name='image'> |
| | 1931 | | /// An image stream. |
| | 1932 | | /// </param> |
| | 1933 | | /// <param name='visualFeatures'> |
| | 1934 | | /// A string indicating what visual feature types to return. Multiple values |
| | 1935 | | /// should be comma-separated. Valid visual feature types include: Categories - |
| | 1936 | | /// categorizes image content according to a taxonomy defined in documentation. |
| | 1937 | | /// Tags - tags the image with a detailed list of words related to the image |
| | 1938 | | /// content. Description - describes the image content with a complete English |
| | 1939 | | /// sentence. Faces - detects if faces are present. If present, generate |
| | 1940 | | /// coordinates, gender and age. ImageType - detects if image is clipart or a |
| | 1941 | | /// line drawing. Color - determines the accent color, dominant color, and |
| | 1942 | | /// whether an image is black&white. Adult - detects if the image is |
| | 1943 | | /// pornographic in nature (depicts nudity or a sex act), or is gory (depicts |
| | 1944 | | /// extreme violence or blood). Sexually suggestive content (aka racy content) |
| | 1945 | | /// is also detected. Objects - detects various objects within an image, |
| | 1946 | | /// including the approximate location. The Objects argument is only available |
| | 1947 | | /// in English. Brands - detects various brands within an image, including the |
| | 1948 | | /// approximate location. The Brands argument is only available in English. |
| | 1949 | | /// </param> |
| | 1950 | | /// <param name='details'> |
| | 1951 | | /// A string indicating which domain-specific details to return. Multiple |
| | 1952 | | /// values should be comma-separated. Valid visual feature types include: |
| | 1953 | | /// Celebrities - identifies celebrities if detected in the image, Landmarks - |
| | 1954 | | /// identifies notable landmarks in the image. |
| | 1955 | | /// </param> |
| | 1956 | | /// <param name='language'> |
| | 1957 | | /// The desired language for output generation. If this parameter is not |
| | 1958 | | /// specified, the default value is &quot;en&quot;.Supported |
| | 1959 | | /// languages:en - English, Default. es - Spanish, ja - Japanese, pt - |
| | 1960 | | /// Portuguese, zh - Simplified Chinese. Possible values include: 'en', 'es', |
| | 1961 | | /// 'ja', 'pt', 'zh' |
| | 1962 | | /// </param> |
| | 1963 | | /// <param name='descriptionExclude'> |
| | 1964 | | /// Turn off specified domain models when generating the description. |
| | 1965 | | /// </param> |
| | 1966 | | /// <param name='customHeaders'> |
| | 1967 | | /// Headers that will be added to request. |
| | 1968 | | /// </param> |
| | 1969 | | /// <param name='cancellationToken'> |
| | 1970 | | /// The cancellation token. |
| | 1971 | | /// </param> |
| | 1972 | | /// <exception cref="ComputerVisionErrorException"> |
| | 1973 | | /// Thrown when the operation returned an invalid status code |
| | 1974 | | /// </exception> |
| | 1975 | | /// <exception cref="SerializationException"> |
| | 1976 | | /// Thrown when unable to deserialize the response |
| | 1977 | | /// </exception> |
| | 1978 | | /// <exception cref="ValidationException"> |
| | 1979 | | /// Thrown when a required parameter is null |
| | 1980 | | /// </exception> |
| | 1981 | | /// <exception cref="System.ArgumentNullException"> |
| | 1982 | | /// Thrown when a required parameter is null |
| | 1983 | | /// </exception> |
| | 1984 | | /// <return> |
| | 1985 | | /// A response object containing the response body and response headers. |
| | 1986 | | /// </return> |
| | 1987 | | public async Task<HttpOperationResponse<ImageAnalysis>> AnalyzeImageInStreamWithHttpMessagesAsync(Stream image, |
| | 1988 | | { |
| 2 | 1989 | | if (Endpoint == null) |
| | 1990 | | { |
| 0 | 1991 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 1992 | | } |
| 2 | 1993 | | if (image == null) |
| | 1994 | | { |
| 0 | 1995 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 1996 | | } |
| | 1997 | | // Tracing |
| 2 | 1998 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 1999 | | string _invocationId = null; |
| 2 | 2000 | | if (_shouldTrace) |
| | 2001 | | { |
| 0 | 2002 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 2003 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 2004 | | tracingParameters.Add("visualFeatures", visualFeatures); |
| 0 | 2005 | | tracingParameters.Add("details", details); |
| 0 | 2006 | | tracingParameters.Add("language", language); |
| 0 | 2007 | | tracingParameters.Add("descriptionExclude", descriptionExclude); |
| 0 | 2008 | | tracingParameters.Add("image", image); |
| 0 | 2009 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 2010 | | ServiceClientTracing.Enter(_invocationId, this, "AnalyzeImageInStream", tracingParameters); |
| | 2011 | | } |
| | 2012 | | // Construct URL |
| 2 | 2013 | | var _baseUrl = BaseUri; |
| 2 | 2014 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "analyze"; |
| 2 | 2015 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 2016 | | List<string> _queryParameters = new List<string>(); |
| 2 | 2017 | | if (visualFeatures != null) |
| | 2018 | | { |
| 2 | 2019 | | _queryParameters.Add(string.Format("visualFeatures={0}", System.Uri.EscapeDataString(string.Join(",", vi |
| | 2020 | | } |
| 2 | 2021 | | if (details != null) |
| | 2022 | | { |
| 0 | 2023 | | _queryParameters.Add(string.Format("details={0}", System.Uri.EscapeDataString(string.Join(",", details)) |
| | 2024 | | } |
| 2 | 2025 | | if (language != null) |
| | 2026 | | { |
| 0 | 2027 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(SafeJsonConvert.Serialize |
| | 2028 | | } |
| 2 | 2029 | | if (descriptionExclude != null) |
| | 2030 | | { |
| 0 | 2031 | | _queryParameters.Add(string.Format("descriptionExclude={0}", System.Uri.EscapeDataString(string.Join("," |
| | 2032 | | } |
| 2 | 2033 | | if (_queryParameters.Count > 0) |
| | 2034 | | { |
| 2 | 2035 | | _url += "?" + string.Join("&", _queryParameters); |
| | 2036 | | } |
| | 2037 | | // Create HTTP transport objects |
| 2 | 2038 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 2039 | | HttpResponseMessage _httpResponse = null; |
| 2 | 2040 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 2041 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 2042 | | // Set Headers |
| | 2043 | |
|
| | 2044 | |
|
| 2 | 2045 | | if (customHeaders != null) |
| | 2046 | | { |
| 0 | 2047 | | foreach(var _header in customHeaders) |
| | 2048 | | { |
| 0 | 2049 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 2050 | | { |
| 0 | 2051 | | _httpRequest.Headers.Remove(_header.Key); |
| | 2052 | | } |
| 0 | 2053 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 2054 | | } |
| | 2055 | | } |
| | 2056 | |
|
| | 2057 | | // Serialize Request |
| 2 | 2058 | | string _requestContent = null; |
| 2 | 2059 | | if(image == null) |
| | 2060 | | { |
| 0 | 2061 | | throw new System.ArgumentNullException("image"); |
| | 2062 | | } |
| 2 | 2063 | | if (image != null && image != Stream.Null) |
| | 2064 | | { |
| 2 | 2065 | | _httpRequest.Content = new StreamContent(image); |
| 2 | 2066 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 2067 | | } |
| | 2068 | | // Set Credentials |
| 2 | 2069 | | if (Credentials != null) |
| | 2070 | | { |
| 2 | 2071 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2072 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2073 | | } |
| | 2074 | | // Send Request |
| 2 | 2075 | | if (_shouldTrace) |
| | 2076 | | { |
| 0 | 2077 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 2078 | | } |
| 2 | 2079 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2080 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 2081 | | if (_shouldTrace) |
| | 2082 | | { |
| 0 | 2083 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 2084 | | } |
| 2 | 2085 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 2086 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2087 | | string _responseContent = null; |
| 2 | 2088 | | if ((int)_statusCode != 200) |
| | 2089 | | { |
| 0 | 2090 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 2091 | | try |
| | 2092 | | { |
| 0 | 2093 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 2094 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 2095 | | if (_errorBody != null) |
| | 2096 | | { |
| 0 | 2097 | | ex.Body = _errorBody; |
| | 2098 | | } |
| 0 | 2099 | | } |
| 0 | 2100 | | catch (JsonException) |
| | 2101 | | { |
| | 2102 | | // Ignore the exception |
| 0 | 2103 | | } |
| 0 | 2104 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 2105 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 2106 | | if (_shouldTrace) |
| | 2107 | | { |
| 0 | 2108 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 2109 | | } |
| 0 | 2110 | | _httpRequest.Dispose(); |
| 0 | 2111 | | if (_httpResponse != null) |
| | 2112 | | { |
| 0 | 2113 | | _httpResponse.Dispose(); |
| | 2114 | | } |
| 0 | 2115 | | throw ex; |
| | 2116 | | } |
| | 2117 | | // Create Result |
| 2 | 2118 | | var _result = new HttpOperationResponse<ImageAnalysis>(); |
| 2 | 2119 | | _result.Request = _httpRequest; |
| 2 | 2120 | | _result.Response = _httpResponse; |
| | 2121 | | // Deserialize Response |
| 2 | 2122 | | if ((int)_statusCode == 200) |
| | 2123 | | { |
| 2 | 2124 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 2125 | | try |
| | 2126 | | { |
| 2 | 2127 | | _result.Body = SafeJsonConvert.DeserializeObject<ImageAnalysis>(_responseContent, DeserializationSet |
| 2 | 2128 | | } |
| 0 | 2129 | | catch (JsonException ex) |
| | 2130 | | { |
| 0 | 2131 | | _httpRequest.Dispose(); |
| 0 | 2132 | | if (_httpResponse != null) |
| | 2133 | | { |
| 0 | 2134 | | _httpResponse.Dispose(); |
| | 2135 | | } |
| 0 | 2136 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 2137 | | } |
| | 2138 | | } |
| 2 | 2139 | | if (_shouldTrace) |
| | 2140 | | { |
| 0 | 2141 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 2142 | | } |
| 2 | 2143 | | return _result; |
| 2 | 2144 | | } |
| | 2145 | |
|
| | 2146 | | /// <summary> |
| | 2147 | | /// This operation returns a bounding box around the most important area of the |
| | 2148 | | /// image. |
| | 2149 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 2150 | | /// response contains an error code and a message to help determine what went |
| | 2151 | | /// wrong. |
| | 2152 | | /// Upon failure, the error code and an error message are returned. The error |
| | 2153 | | /// code could be one of InvalidImageUrl, InvalidImageFormat, InvalidImageSize, |
| | 2154 | | /// NotSupportedImage, FailedToProcess, Timeout, or InternalServerError. |
| | 2155 | | /// </summary> |
| | 2156 | | /// <param name='image'> |
| | 2157 | | /// An image stream. |
| | 2158 | | /// </param> |
| | 2159 | | /// <param name='customHeaders'> |
| | 2160 | | /// Headers that will be added to request. |
| | 2161 | | /// </param> |
| | 2162 | | /// <param name='cancellationToken'> |
| | 2163 | | /// The cancellation token. |
| | 2164 | | /// </param> |
| | 2165 | | /// <exception cref="ComputerVisionErrorException"> |
| | 2166 | | /// Thrown when the operation returned an invalid status code |
| | 2167 | | /// </exception> |
| | 2168 | | /// <exception cref="SerializationException"> |
| | 2169 | | /// Thrown when unable to deserialize the response |
| | 2170 | | /// </exception> |
| | 2171 | | /// <exception cref="ValidationException"> |
| | 2172 | | /// Thrown when a required parameter is null |
| | 2173 | | /// </exception> |
| | 2174 | | /// <exception cref="System.ArgumentNullException"> |
| | 2175 | | /// Thrown when a required parameter is null |
| | 2176 | | /// </exception> |
| | 2177 | | /// <return> |
| | 2178 | | /// A response object containing the response body and response headers. |
| | 2179 | | /// </return> |
| | 2180 | | public async Task<HttpOperationResponse<AreaOfInterestResult>> GetAreaOfInterestInStreamWithHttpMessagesAsync(St |
| | 2181 | | { |
| 4 | 2182 | | if (Endpoint == null) |
| | 2183 | | { |
| 0 | 2184 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 2185 | | } |
| 4 | 2186 | | if (image == null) |
| | 2187 | | { |
| 0 | 2188 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 2189 | | } |
| | 2190 | | // Tracing |
| 4 | 2191 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 4 | 2192 | | string _invocationId = null; |
| 4 | 2193 | | if (_shouldTrace) |
| | 2194 | | { |
| 0 | 2195 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 2196 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 2197 | | tracingParameters.Add("image", image); |
| 0 | 2198 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 2199 | | ServiceClientTracing.Enter(_invocationId, this, "GetAreaOfInterestInStream", tracingParameters); |
| | 2200 | | } |
| | 2201 | | // Construct URL |
| 4 | 2202 | | var _baseUrl = BaseUri; |
| 4 | 2203 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "areaOfInterest"; |
| 4 | 2204 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | 2205 | | // Create HTTP transport objects |
| 4 | 2206 | | var _httpRequest = new HttpRequestMessage(); |
| 4 | 2207 | | HttpResponseMessage _httpResponse = null; |
| 4 | 2208 | | _httpRequest.Method = new HttpMethod("POST"); |
| 4 | 2209 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 2210 | | // Set Headers |
| | 2211 | |
|
| | 2212 | |
|
| 4 | 2213 | | if (customHeaders != null) |
| | 2214 | | { |
| 0 | 2215 | | foreach(var _header in customHeaders) |
| | 2216 | | { |
| 0 | 2217 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 2218 | | { |
| 0 | 2219 | | _httpRequest.Headers.Remove(_header.Key); |
| | 2220 | | } |
| 0 | 2221 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 2222 | | } |
| | 2223 | | } |
| | 2224 | |
|
| | 2225 | | // Serialize Request |
| 4 | 2226 | | string _requestContent = null; |
| 4 | 2227 | | if(image == null) |
| | 2228 | | { |
| 0 | 2229 | | throw new System.ArgumentNullException("image"); |
| | 2230 | | } |
| 4 | 2231 | | if (image != null && image != Stream.Null) |
| | 2232 | | { |
| 4 | 2233 | | _httpRequest.Content = new StreamContent(image); |
| 4 | 2234 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 2235 | | } |
| | 2236 | | // Set Credentials |
| 4 | 2237 | | if (Credentials != null) |
| | 2238 | | { |
| 4 | 2239 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 2240 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2241 | | } |
| | 2242 | | // Send Request |
| 4 | 2243 | | if (_shouldTrace) |
| | 2244 | | { |
| 0 | 2245 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 2246 | | } |
| 4 | 2247 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 2248 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 4 | 2249 | | if (_shouldTrace) |
| | 2250 | | { |
| 0 | 2251 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 2252 | | } |
| 4 | 2253 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 4 | 2254 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 2255 | | string _responseContent = null; |
| 4 | 2256 | | if ((int)_statusCode != 200) |
| | 2257 | | { |
| 0 | 2258 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 2259 | | try |
| | 2260 | | { |
| 0 | 2261 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 2262 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 2263 | | if (_errorBody != null) |
| | 2264 | | { |
| 0 | 2265 | | ex.Body = _errorBody; |
| | 2266 | | } |
| 0 | 2267 | | } |
| 0 | 2268 | | catch (JsonException) |
| | 2269 | | { |
| | 2270 | | // Ignore the exception |
| 0 | 2271 | | } |
| 0 | 2272 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 2273 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 2274 | | if (_shouldTrace) |
| | 2275 | | { |
| 0 | 2276 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 2277 | | } |
| 0 | 2278 | | _httpRequest.Dispose(); |
| 0 | 2279 | | if (_httpResponse != null) |
| | 2280 | | { |
| 0 | 2281 | | _httpResponse.Dispose(); |
| | 2282 | | } |
| 0 | 2283 | | throw ex; |
| | 2284 | | } |
| | 2285 | | // Create Result |
| 4 | 2286 | | var _result = new HttpOperationResponse<AreaOfInterestResult>(); |
| 4 | 2287 | | _result.Request = _httpRequest; |
| 4 | 2288 | | _result.Response = _httpResponse; |
| | 2289 | | // Deserialize Response |
| 4 | 2290 | | if ((int)_statusCode == 200) |
| | 2291 | | { |
| 4 | 2292 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 2293 | | try |
| | 2294 | | { |
| 4 | 2295 | | _result.Body = SafeJsonConvert.DeserializeObject<AreaOfInterestResult>(_responseContent, Deserializa |
| 4 | 2296 | | } |
| 0 | 2297 | | catch (JsonException ex) |
| | 2298 | | { |
| 0 | 2299 | | _httpRequest.Dispose(); |
| 0 | 2300 | | if (_httpResponse != null) |
| | 2301 | | { |
| 0 | 2302 | | _httpResponse.Dispose(); |
| | 2303 | | } |
| 0 | 2304 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 2305 | | } |
| | 2306 | | } |
| 4 | 2307 | | if (_shouldTrace) |
| | 2308 | | { |
| 0 | 2309 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 2310 | | } |
| 4 | 2311 | | return _result; |
| 4 | 2312 | | } |
| | 2313 | |
|
| | 2314 | | /// <summary> |
| | 2315 | | /// This operation generates a description of an image in human readable |
| | 2316 | | /// language with complete sentences. The description is based on a collection |
| | 2317 | | /// of content tags, which are also returned by the operation. More than one |
| | 2318 | | /// description can be generated for each image. Descriptions are ordered by |
| | 2319 | | /// their confidence score. Descriptions may include results from celebrity and |
| | 2320 | | /// landmark domain models, if applicable. |
| | 2321 | | /// Two input methods are supported -- (1) Uploading an image or (2) specifying |
| | 2322 | | /// an image URL. |
| | 2323 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 2324 | | /// response will contain an error code and a message to help understand what |
| | 2325 | | /// went wrong. |
| | 2326 | | /// </summary> |
| | 2327 | | /// <param name='image'> |
| | 2328 | | /// An image stream. |
| | 2329 | | /// </param> |
| | 2330 | | /// <param name='maxCandidates'> |
| | 2331 | | /// Maximum number of candidate descriptions to be returned. The default is 1. |
| | 2332 | | /// </param> |
| | 2333 | | /// <param name='language'> |
| | 2334 | | /// The desired language for output generation. If this parameter is not |
| | 2335 | | /// specified, the default value is &quot;en&quot;.Supported |
| | 2336 | | /// languages:en - English, Default. es - Spanish, ja - Japanese, pt - |
| | 2337 | | /// Portuguese, zh - Simplified Chinese. Possible values include: 'en', 'es', |
| | 2338 | | /// 'ja', 'pt', 'zh' |
| | 2339 | | /// </param> |
| | 2340 | | /// <param name='descriptionExclude'> |
| | 2341 | | /// Turn off specified domain models when generating the description. |
| | 2342 | | /// </param> |
| | 2343 | | /// <param name='customHeaders'> |
| | 2344 | | /// Headers that will be added to request. |
| | 2345 | | /// </param> |
| | 2346 | | /// <param name='cancellationToken'> |
| | 2347 | | /// The cancellation token. |
| | 2348 | | /// </param> |
| | 2349 | | /// <exception cref="ComputerVisionErrorException"> |
| | 2350 | | /// Thrown when the operation returned an invalid status code |
| | 2351 | | /// </exception> |
| | 2352 | | /// <exception cref="SerializationException"> |
| | 2353 | | /// Thrown when unable to deserialize the response |
| | 2354 | | /// </exception> |
| | 2355 | | /// <exception cref="ValidationException"> |
| | 2356 | | /// Thrown when a required parameter is null |
| | 2357 | | /// </exception> |
| | 2358 | | /// <exception cref="System.ArgumentNullException"> |
| | 2359 | | /// Thrown when a required parameter is null |
| | 2360 | | /// </exception> |
| | 2361 | | /// <return> |
| | 2362 | | /// A response object containing the response body and response headers. |
| | 2363 | | /// </return> |
| | 2364 | | public async Task<HttpOperationResponse<ImageDescription>> DescribeImageInStreamWithHttpMessagesAsync(Stream ima |
| | 2365 | | { |
| 2 | 2366 | | if (Endpoint == null) |
| | 2367 | | { |
| 0 | 2368 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 2369 | | } |
| 2 | 2370 | | if (image == null) |
| | 2371 | | { |
| 0 | 2372 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 2373 | | } |
| | 2374 | | // Tracing |
| 2 | 2375 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 2376 | | string _invocationId = null; |
| 2 | 2377 | | if (_shouldTrace) |
| | 2378 | | { |
| 0 | 2379 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 2380 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 2381 | | tracingParameters.Add("maxCandidates", maxCandidates); |
| 0 | 2382 | | tracingParameters.Add("language", language); |
| 0 | 2383 | | tracingParameters.Add("descriptionExclude", descriptionExclude); |
| 0 | 2384 | | tracingParameters.Add("image", image); |
| 0 | 2385 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 2386 | | ServiceClientTracing.Enter(_invocationId, this, "DescribeImageInStream", tracingParameters); |
| | 2387 | | } |
| | 2388 | | // Construct URL |
| 2 | 2389 | | var _baseUrl = BaseUri; |
| 2 | 2390 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "describe"; |
| 2 | 2391 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 2392 | | List<string> _queryParameters = new List<string>(); |
| 2 | 2393 | | if (maxCandidates != null) |
| | 2394 | | { |
| 2 | 2395 | | _queryParameters.Add(string.Format("maxCandidates={0}", System.Uri.EscapeDataString(SafeJsonConvert.Seri |
| | 2396 | | } |
| 2 | 2397 | | if (language != null) |
| | 2398 | | { |
| 0 | 2399 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(SafeJsonConvert.Serialize |
| | 2400 | | } |
| 2 | 2401 | | if (descriptionExclude != null) |
| | 2402 | | { |
| 0 | 2403 | | _queryParameters.Add(string.Format("descriptionExclude={0}", System.Uri.EscapeDataString(string.Join("," |
| | 2404 | | } |
| 2 | 2405 | | if (_queryParameters.Count > 0) |
| | 2406 | | { |
| 2 | 2407 | | _url += "?" + string.Join("&", _queryParameters); |
| | 2408 | | } |
| | 2409 | | // Create HTTP transport objects |
| 2 | 2410 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 2411 | | HttpResponseMessage _httpResponse = null; |
| 2 | 2412 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 2413 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 2414 | | // Set Headers |
| | 2415 | |
|
| | 2416 | |
|
| 2 | 2417 | | if (customHeaders != null) |
| | 2418 | | { |
| 0 | 2419 | | foreach(var _header in customHeaders) |
| | 2420 | | { |
| 0 | 2421 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 2422 | | { |
| 0 | 2423 | | _httpRequest.Headers.Remove(_header.Key); |
| | 2424 | | } |
| 0 | 2425 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 2426 | | } |
| | 2427 | | } |
| | 2428 | |
|
| | 2429 | | // Serialize Request |
| 2 | 2430 | | string _requestContent = null; |
| 2 | 2431 | | if(image == null) |
| | 2432 | | { |
| 0 | 2433 | | throw new System.ArgumentNullException("image"); |
| | 2434 | | } |
| 2 | 2435 | | if (image != null && image != Stream.Null) |
| | 2436 | | { |
| 2 | 2437 | | _httpRequest.Content = new StreamContent(image); |
| 2 | 2438 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 2439 | | } |
| | 2440 | | // Set Credentials |
| 2 | 2441 | | if (Credentials != null) |
| | 2442 | | { |
| 2 | 2443 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2444 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2445 | | } |
| | 2446 | | // Send Request |
| 2 | 2447 | | if (_shouldTrace) |
| | 2448 | | { |
| 0 | 2449 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 2450 | | } |
| 2 | 2451 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2452 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 2453 | | if (_shouldTrace) |
| | 2454 | | { |
| 0 | 2455 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 2456 | | } |
| 2 | 2457 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 2458 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2459 | | string _responseContent = null; |
| 2 | 2460 | | if ((int)_statusCode != 200) |
| | 2461 | | { |
| 0 | 2462 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 2463 | | try |
| | 2464 | | { |
| 0 | 2465 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 2466 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 2467 | | if (_errorBody != null) |
| | 2468 | | { |
| 0 | 2469 | | ex.Body = _errorBody; |
| | 2470 | | } |
| 0 | 2471 | | } |
| 0 | 2472 | | catch (JsonException) |
| | 2473 | | { |
| | 2474 | | // Ignore the exception |
| 0 | 2475 | | } |
| 0 | 2476 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 2477 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 2478 | | if (_shouldTrace) |
| | 2479 | | { |
| 0 | 2480 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 2481 | | } |
| 0 | 2482 | | _httpRequest.Dispose(); |
| 0 | 2483 | | if (_httpResponse != null) |
| | 2484 | | { |
| 0 | 2485 | | _httpResponse.Dispose(); |
| | 2486 | | } |
| 0 | 2487 | | throw ex; |
| | 2488 | | } |
| | 2489 | | // Create Result |
| 2 | 2490 | | var _result = new HttpOperationResponse<ImageDescription>(); |
| 2 | 2491 | | _result.Request = _httpRequest; |
| 2 | 2492 | | _result.Response = _httpResponse; |
| | 2493 | | // Deserialize Response |
| 2 | 2494 | | if ((int)_statusCode == 200) |
| | 2495 | | { |
| 2 | 2496 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 2497 | | try |
| | 2498 | | { |
| 2 | 2499 | | _result.Body = SafeJsonConvert.DeserializeObject<ImageDescription>(_responseContent, Deserialization |
| 2 | 2500 | | } |
| 0 | 2501 | | catch (JsonException ex) |
| | 2502 | | { |
| 0 | 2503 | | _httpRequest.Dispose(); |
| 0 | 2504 | | if (_httpResponse != null) |
| | 2505 | | { |
| 0 | 2506 | | _httpResponse.Dispose(); |
| | 2507 | | } |
| 0 | 2508 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 2509 | | } |
| | 2510 | | } |
| 2 | 2511 | | if (_shouldTrace) |
| | 2512 | | { |
| 0 | 2513 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 2514 | | } |
| 2 | 2515 | | return _result; |
| 2 | 2516 | | } |
| | 2517 | |
|
| | 2518 | | /// <summary> |
| | 2519 | | /// Performs object detection on the specified image. |
| | 2520 | | /// Two input methods are supported -- (1) Uploading an image or (2) specifying |
| | 2521 | | /// an image URL. |
| | 2522 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 2523 | | /// response will contain an error code and a message to help understand what |
| | 2524 | | /// went wrong. |
| | 2525 | | /// </summary> |
| | 2526 | | /// <param name='image'> |
| | 2527 | | /// An image stream. |
| | 2528 | | /// </param> |
| | 2529 | | /// <param name='customHeaders'> |
| | 2530 | | /// Headers that will be added to request. |
| | 2531 | | /// </param> |
| | 2532 | | /// <param name='cancellationToken'> |
| | 2533 | | /// The cancellation token. |
| | 2534 | | /// </param> |
| | 2535 | | /// <exception cref="ComputerVisionErrorException"> |
| | 2536 | | /// Thrown when the operation returned an invalid status code |
| | 2537 | | /// </exception> |
| | 2538 | | /// <exception cref="SerializationException"> |
| | 2539 | | /// Thrown when unable to deserialize the response |
| | 2540 | | /// </exception> |
| | 2541 | | /// <exception cref="ValidationException"> |
| | 2542 | | /// Thrown when a required parameter is null |
| | 2543 | | /// </exception> |
| | 2544 | | /// <exception cref="System.ArgumentNullException"> |
| | 2545 | | /// Thrown when a required parameter is null |
| | 2546 | | /// </exception> |
| | 2547 | | /// <return> |
| | 2548 | | /// A response object containing the response body and response headers. |
| | 2549 | | /// </return> |
| | 2550 | | public async Task<HttpOperationResponse<DetectResult>> DetectObjectsInStreamWithHttpMessagesAsync(Stream image, |
| | 2551 | | { |
| 2 | 2552 | | if (Endpoint == null) |
| | 2553 | | { |
| 0 | 2554 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 2555 | | } |
| 2 | 2556 | | if (image == null) |
| | 2557 | | { |
| 0 | 2558 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 2559 | | } |
| | 2560 | | // Tracing |
| 2 | 2561 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 2562 | | string _invocationId = null; |
| 2 | 2563 | | if (_shouldTrace) |
| | 2564 | | { |
| 0 | 2565 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 2566 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 2567 | | tracingParameters.Add("image", image); |
| 0 | 2568 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 2569 | | ServiceClientTracing.Enter(_invocationId, this, "DetectObjectsInStream", tracingParameters); |
| | 2570 | | } |
| | 2571 | | // Construct URL |
| 2 | 2572 | | var _baseUrl = BaseUri; |
| 2 | 2573 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "detect"; |
| 2 | 2574 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| | 2575 | | // Create HTTP transport objects |
| 2 | 2576 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 2577 | | HttpResponseMessage _httpResponse = null; |
| 2 | 2578 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 2579 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 2580 | | // Set Headers |
| | 2581 | |
|
| | 2582 | |
|
| 2 | 2583 | | if (customHeaders != null) |
| | 2584 | | { |
| 0 | 2585 | | foreach(var _header in customHeaders) |
| | 2586 | | { |
| 0 | 2587 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 2588 | | { |
| 0 | 2589 | | _httpRequest.Headers.Remove(_header.Key); |
| | 2590 | | } |
| 0 | 2591 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 2592 | | } |
| | 2593 | | } |
| | 2594 | |
|
| | 2595 | | // Serialize Request |
| 2 | 2596 | | string _requestContent = null; |
| 2 | 2597 | | if(image == null) |
| | 2598 | | { |
| 0 | 2599 | | throw new System.ArgumentNullException("image"); |
| | 2600 | | } |
| 2 | 2601 | | if (image != null && image != Stream.Null) |
| | 2602 | | { |
| 2 | 2603 | | _httpRequest.Content = new StreamContent(image); |
| 2 | 2604 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 2605 | | } |
| | 2606 | | // Set Credentials |
| 2 | 2607 | | if (Credentials != null) |
| | 2608 | | { |
| 2 | 2609 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2610 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2611 | | } |
| | 2612 | | // Send Request |
| 2 | 2613 | | if (_shouldTrace) |
| | 2614 | | { |
| 0 | 2615 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 2616 | | } |
| 2 | 2617 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2618 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 2619 | | if (_shouldTrace) |
| | 2620 | | { |
| 0 | 2621 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 2622 | | } |
| 2 | 2623 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 2624 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2625 | | string _responseContent = null; |
| 2 | 2626 | | if ((int)_statusCode != 200) |
| | 2627 | | { |
| 0 | 2628 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 2629 | | try |
| | 2630 | | { |
| 0 | 2631 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 2632 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 2633 | | if (_errorBody != null) |
| | 2634 | | { |
| 0 | 2635 | | ex.Body = _errorBody; |
| | 2636 | | } |
| 0 | 2637 | | } |
| 0 | 2638 | | catch (JsonException) |
| | 2639 | | { |
| | 2640 | | // Ignore the exception |
| 0 | 2641 | | } |
| 0 | 2642 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 2643 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 2644 | | if (_shouldTrace) |
| | 2645 | | { |
| 0 | 2646 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 2647 | | } |
| 0 | 2648 | | _httpRequest.Dispose(); |
| 0 | 2649 | | if (_httpResponse != null) |
| | 2650 | | { |
| 0 | 2651 | | _httpResponse.Dispose(); |
| | 2652 | | } |
| 0 | 2653 | | throw ex; |
| | 2654 | | } |
| | 2655 | | // Create Result |
| 2 | 2656 | | var _result = new HttpOperationResponse<DetectResult>(); |
| 2 | 2657 | | _result.Request = _httpRequest; |
| 2 | 2658 | | _result.Response = _httpResponse; |
| | 2659 | | // Deserialize Response |
| 2 | 2660 | | if ((int)_statusCode == 200) |
| | 2661 | | { |
| 2 | 2662 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 2663 | | try |
| | 2664 | | { |
| 2 | 2665 | | _result.Body = SafeJsonConvert.DeserializeObject<DetectResult>(_responseContent, DeserializationSett |
| 2 | 2666 | | } |
| 0 | 2667 | | catch (JsonException ex) |
| | 2668 | | { |
| 0 | 2669 | | _httpRequest.Dispose(); |
| 0 | 2670 | | if (_httpResponse != null) |
| | 2671 | | { |
| 0 | 2672 | | _httpResponse.Dispose(); |
| | 2673 | | } |
| 0 | 2674 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 2675 | | } |
| | 2676 | | } |
| 2 | 2677 | | if (_shouldTrace) |
| | 2678 | | { |
| 0 | 2679 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 2680 | | } |
| 2 | 2681 | | return _result; |
| 2 | 2682 | | } |
| | 2683 | |
|
| | 2684 | | /// <summary> |
| | 2685 | | /// This operation generates a thumbnail image with the user-specified width |
| | 2686 | | /// and height. By default, the service analyzes the image, identifies the |
| | 2687 | | /// region of interest (ROI), and generates smart cropping coordinates based on |
| | 2688 | | /// the ROI. Smart cropping helps when you specify an aspect ratio that differs |
| | 2689 | | /// from that of the input image. |
| | 2690 | | /// A successful response contains the thumbnail image binary. If the request |
| | 2691 | | /// failed, the response contains an error code and a message to help determine |
| | 2692 | | /// what went wrong. |
| | 2693 | | /// Upon failure, the error code and an error message are returned. The error |
| | 2694 | | /// code could be one of InvalidImageUrl, InvalidImageFormat, InvalidImageSize, |
| | 2695 | | /// InvalidThumbnailSize, NotSupportedImage, FailedToProcess, Timeout, or |
| | 2696 | | /// InternalServerError. |
| | 2697 | | /// </summary> |
| | 2698 | | /// <param name='width'> |
| | 2699 | | /// Width of the thumbnail, in pixels. It must be between 1 and 1024. |
| | 2700 | | /// Recommended minimum of 50. |
| | 2701 | | /// </param> |
| | 2702 | | /// <param name='height'> |
| | 2703 | | /// Height of the thumbnail, in pixels. It must be between 1 and 1024. |
| | 2704 | | /// Recommended minimum of 50. |
| | 2705 | | /// </param> |
| | 2706 | | /// <param name='image'> |
| | 2707 | | /// An image stream. |
| | 2708 | | /// </param> |
| | 2709 | | /// <param name='smartCropping'> |
| | 2710 | | /// Boolean flag for enabling smart cropping. |
| | 2711 | | /// </param> |
| | 2712 | | /// <param name='customHeaders'> |
| | 2713 | | /// Headers that will be added to request. |
| | 2714 | | /// </param> |
| | 2715 | | /// <param name='cancellationToken'> |
| | 2716 | | /// The cancellation token. |
| | 2717 | | /// </param> |
| | 2718 | | /// <exception cref="HttpOperationException"> |
| | 2719 | | /// Thrown when the operation returned an invalid status code |
| | 2720 | | /// </exception> |
| | 2721 | | /// <exception cref="SerializationException"> |
| | 2722 | | /// Thrown when unable to deserialize the response |
| | 2723 | | /// </exception> |
| | 2724 | | /// <exception cref="ValidationException"> |
| | 2725 | | /// Thrown when a required parameter is null |
| | 2726 | | /// </exception> |
| | 2727 | | /// <exception cref="System.ArgumentNullException"> |
| | 2728 | | /// Thrown when a required parameter is null |
| | 2729 | | /// </exception> |
| | 2730 | | /// <return> |
| | 2731 | | /// A response object containing the response body and response headers. |
| | 2732 | | /// </return> |
| | 2733 | | public async Task<HttpOperationResponse<Stream>> GenerateThumbnailInStreamWithHttpMessagesAsync(int width, int h |
| | 2734 | | { |
| 2 | 2735 | | if (Endpoint == null) |
| | 2736 | | { |
| 0 | 2737 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 2738 | | } |
| 2 | 2739 | | if (width > 1024) |
| | 2740 | | { |
| 0 | 2741 | | throw new ValidationException(ValidationRules.InclusiveMaximum, "width", 1024); |
| | 2742 | | } |
| 2 | 2743 | | if (width < 1) |
| | 2744 | | { |
| 0 | 2745 | | throw new ValidationException(ValidationRules.InclusiveMinimum, "width", 1); |
| | 2746 | | } |
| 2 | 2747 | | if (height > 1024) |
| | 2748 | | { |
| 0 | 2749 | | throw new ValidationException(ValidationRules.InclusiveMaximum, "height", 1024); |
| | 2750 | | } |
| 2 | 2751 | | if (height < 1) |
| | 2752 | | { |
| 0 | 2753 | | throw new ValidationException(ValidationRules.InclusiveMinimum, "height", 1); |
| | 2754 | | } |
| 2 | 2755 | | if (image == null) |
| | 2756 | | { |
| 0 | 2757 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 2758 | | } |
| | 2759 | | // Tracing |
| 2 | 2760 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 2761 | | string _invocationId = null; |
| 2 | 2762 | | if (_shouldTrace) |
| | 2763 | | { |
| 0 | 2764 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 2765 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 2766 | | tracingParameters.Add("width", width); |
| 0 | 2767 | | tracingParameters.Add("height", height); |
| 0 | 2768 | | tracingParameters.Add("smartCropping", smartCropping); |
| 0 | 2769 | | tracingParameters.Add("image", image); |
| 0 | 2770 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 2771 | | ServiceClientTracing.Enter(_invocationId, this, "GenerateThumbnailInStream", tracingParameters); |
| | 2772 | | } |
| | 2773 | | // Construct URL |
| 2 | 2774 | | var _baseUrl = BaseUri; |
| 2 | 2775 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "generateThumbnail"; |
| 2 | 2776 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 2777 | | List<string> _queryParameters = new List<string>(); |
| 2 | 2778 | | _queryParameters.Add(string.Format("width={0}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject( |
| 2 | 2779 | | _queryParameters.Add(string.Format("height={0}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject |
| 2 | 2780 | | if (smartCropping != null) |
| | 2781 | | { |
| 2 | 2782 | | _queryParameters.Add(string.Format("smartCropping={0}", System.Uri.EscapeDataString(SafeJsonConvert.Seri |
| | 2783 | | } |
| 2 | 2784 | | if (_queryParameters.Count > 0) |
| | 2785 | | { |
| 2 | 2786 | | _url += "?" + string.Join("&", _queryParameters); |
| | 2787 | | } |
| | 2788 | | // Create HTTP transport objects |
| 2 | 2789 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 2790 | | HttpResponseMessage _httpResponse = null; |
| 2 | 2791 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 2792 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 2793 | | // Set Headers |
| | 2794 | |
|
| | 2795 | |
|
| 2 | 2796 | | if (customHeaders != null) |
| | 2797 | | { |
| 0 | 2798 | | foreach(var _header in customHeaders) |
| | 2799 | | { |
| 0 | 2800 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 2801 | | { |
| 0 | 2802 | | _httpRequest.Headers.Remove(_header.Key); |
| | 2803 | | } |
| 0 | 2804 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 2805 | | } |
| | 2806 | | } |
| | 2807 | |
|
| | 2808 | | // Serialize Request |
| 2 | 2809 | | string _requestContent = null; |
| 2 | 2810 | | if(image == null) |
| | 2811 | | { |
| 0 | 2812 | | throw new System.ArgumentNullException("image"); |
| | 2813 | | } |
| 2 | 2814 | | if (image != null && image != Stream.Null) |
| | 2815 | | { |
| 2 | 2816 | | _httpRequest.Content = new StreamContent(image); |
| 2 | 2817 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 2818 | | } |
| | 2819 | | // Set Credentials |
| 2 | 2820 | | if (Credentials != null) |
| | 2821 | | { |
| 2 | 2822 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2823 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 2824 | | } |
| | 2825 | | // Send Request |
| 2 | 2826 | | if (_shouldTrace) |
| | 2827 | | { |
| 0 | 2828 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 2829 | | } |
| 2 | 2830 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2831 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, System.Net.Http.HttpCompletionOption.ResponseHeader |
| 2 | 2832 | | if (_shouldTrace) |
| | 2833 | | { |
| 0 | 2834 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 2835 | | } |
| 2 | 2836 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 2837 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2838 | | string _responseContent = null; |
| 2 | 2839 | | if ((int)_statusCode != 200) |
| | 2840 | | { |
| 0 | 2841 | | var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _st |
| 0 | 2842 | | if (_httpResponse.Content != null) { |
| 0 | 2843 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 2844 | | } |
| | 2845 | | else { |
| 0 | 2846 | | _responseContent = string.Empty; |
| | 2847 | | } |
| 0 | 2848 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 2849 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 2850 | | if (_shouldTrace) |
| | 2851 | | { |
| 0 | 2852 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 2853 | | } |
| 0 | 2854 | | _httpRequest.Dispose(); |
| 0 | 2855 | | if (_httpResponse != null) |
| | 2856 | | { |
| 0 | 2857 | | _httpResponse.Dispose(); |
| | 2858 | | } |
| 0 | 2859 | | throw ex; |
| | 2860 | | } |
| | 2861 | | // Create Result |
| 2 | 2862 | | var _result = new HttpOperationResponse<Stream>(); |
| 2 | 2863 | | _result.Request = _httpRequest; |
| 2 | 2864 | | _result.Response = _httpResponse; |
| | 2865 | | // Deserialize Response |
| 2 | 2866 | | if ((int)_statusCode == 200) |
| | 2867 | | { |
| 2 | 2868 | | _result.Body = await _httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false); |
| | 2869 | | } |
| 2 | 2870 | | if (_shouldTrace) |
| | 2871 | | { |
| 0 | 2872 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 2873 | | } |
| 2 | 2874 | | return _result; |
| 2 | 2875 | | } |
| | 2876 | |
|
| | 2877 | | /// <summary> |
| | 2878 | | /// This operation recognizes content within an image by applying a |
| | 2879 | | /// domain-specific model. The list of domain-specific models that are |
| | 2880 | | /// supported by the Computer Vision API can be retrieved using the /models GET |
| | 2881 | | /// request. Currently, the API provides following domain-specific models: |
| | 2882 | | /// celebrities, landmarks. |
| | 2883 | | /// Two input methods are supported -- (1) Uploading an image or (2) specifying |
| | 2884 | | /// an image URL. |
| | 2885 | | /// A successful response will be returned in JSON. |
| | 2886 | | /// If the request failed, the response will contain an error code and a |
| | 2887 | | /// message to help understand what went wrong. |
| | 2888 | | /// </summary> |
| | 2889 | | /// <param name='model'> |
| | 2890 | | /// The domain-specific content to recognize. |
| | 2891 | | /// </param> |
| | 2892 | | /// <param name='image'> |
| | 2893 | | /// An image stream. |
| | 2894 | | /// </param> |
| | 2895 | | /// <param name='language'> |
| | 2896 | | /// The desired language for output generation. If this parameter is not |
| | 2897 | | /// specified, the default value is &quot;en&quot;.Supported |
| | 2898 | | /// languages:en - English, Default. es - Spanish, ja - Japanese, pt - |
| | 2899 | | /// Portuguese, zh - Simplified Chinese. Possible values include: 'en', 'es', |
| | 2900 | | /// 'ja', 'pt', 'zh' |
| | 2901 | | /// </param> |
| | 2902 | | /// <param name='customHeaders'> |
| | 2903 | | /// Headers that will be added to request. |
| | 2904 | | /// </param> |
| | 2905 | | /// <param name='cancellationToken'> |
| | 2906 | | /// The cancellation token. |
| | 2907 | | /// </param> |
| | 2908 | | /// <exception cref="ComputerVisionErrorException"> |
| | 2909 | | /// Thrown when the operation returned an invalid status code |
| | 2910 | | /// </exception> |
| | 2911 | | /// <exception cref="SerializationException"> |
| | 2912 | | /// Thrown when unable to deserialize the response |
| | 2913 | | /// </exception> |
| | 2914 | | /// <exception cref="ValidationException"> |
| | 2915 | | /// Thrown when a required parameter is null |
| | 2916 | | /// </exception> |
| | 2917 | | /// <exception cref="System.ArgumentNullException"> |
| | 2918 | | /// Thrown when a required parameter is null |
| | 2919 | | /// </exception> |
| | 2920 | | /// <return> |
| | 2921 | | /// A response object containing the response body and response headers. |
| | 2922 | | /// </return> |
| | 2923 | | public async Task<HttpOperationResponse<DomainModelResults>> AnalyzeImageByDomainInStreamWithHttpMessagesAsync(s |
| | 2924 | | { |
| 2 | 2925 | | if (Endpoint == null) |
| | 2926 | | { |
| 0 | 2927 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 2928 | | } |
| 2 | 2929 | | if (model == null) |
| | 2930 | | { |
| 0 | 2931 | | throw new ValidationException(ValidationRules.CannotBeNull, "model"); |
| | 2932 | | } |
| 2 | 2933 | | if (image == null) |
| | 2934 | | { |
| 0 | 2935 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 2936 | | } |
| | 2937 | | // Tracing |
| 2 | 2938 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 2939 | | string _invocationId = null; |
| 2 | 2940 | | if (_shouldTrace) |
| | 2941 | | { |
| 0 | 2942 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 2943 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 2944 | | tracingParameters.Add("model", model); |
| 0 | 2945 | | tracingParameters.Add("language", language); |
| 0 | 2946 | | tracingParameters.Add("image", image); |
| 0 | 2947 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 2948 | | ServiceClientTracing.Enter(_invocationId, this, "AnalyzeImageByDomainInStream", tracingParameters); |
| | 2949 | | } |
| | 2950 | | // Construct URL |
| 2 | 2951 | | var _baseUrl = BaseUri; |
| 2 | 2952 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "models/{model}/analyze"; |
| 2 | 2953 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 2954 | | _url = _url.Replace("{model}", System.Uri.EscapeDataString(model)); |
| 2 | 2955 | | List<string> _queryParameters = new List<string>(); |
| 2 | 2956 | | if (language != null) |
| | 2957 | | { |
| 0 | 2958 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(SafeJsonConvert.Serialize |
| | 2959 | | } |
| 2 | 2960 | | if (_queryParameters.Count > 0) |
| | 2961 | | { |
| 0 | 2962 | | _url += "?" + string.Join("&", _queryParameters); |
| | 2963 | | } |
| | 2964 | | // Create HTTP transport objects |
| 2 | 2965 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 2966 | | HttpResponseMessage _httpResponse = null; |
| 2 | 2967 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 2968 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 2969 | | // Set Headers |
| | 2970 | |
|
| | 2971 | |
|
| 2 | 2972 | | if (customHeaders != null) |
| | 2973 | | { |
| 0 | 2974 | | foreach(var _header in customHeaders) |
| | 2975 | | { |
| 0 | 2976 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 2977 | | { |
| 0 | 2978 | | _httpRequest.Headers.Remove(_header.Key); |
| | 2979 | | } |
| 0 | 2980 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 2981 | | } |
| | 2982 | | } |
| | 2983 | |
|
| | 2984 | | // Serialize Request |
| 2 | 2985 | | string _requestContent = null; |
| 2 | 2986 | | if(image == null) |
| | 2987 | | { |
| 0 | 2988 | | throw new System.ArgumentNullException("image"); |
| | 2989 | | } |
| 2 | 2990 | | if (image != null && image != Stream.Null) |
| | 2991 | | { |
| 2 | 2992 | | _httpRequest.Content = new StreamContent(image); |
| 2 | 2993 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 2994 | | } |
| | 2995 | | // Set Credentials |
| 2 | 2996 | | if (Credentials != null) |
| | 2997 | | { |
| 2 | 2998 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 2999 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 3000 | | } |
| | 3001 | | // Send Request |
| 2 | 3002 | | if (_shouldTrace) |
| | 3003 | | { |
| 0 | 3004 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 3005 | | } |
| 2 | 3006 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 3007 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 3008 | | if (_shouldTrace) |
| | 3009 | | { |
| 0 | 3010 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 3011 | | } |
| 2 | 3012 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 3013 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 3014 | | string _responseContent = null; |
| 2 | 3015 | | if ((int)_statusCode != 200) |
| | 3016 | | { |
| 0 | 3017 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 3018 | | try |
| | 3019 | | { |
| 0 | 3020 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 3021 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 3022 | | if (_errorBody != null) |
| | 3023 | | { |
| 0 | 3024 | | ex.Body = _errorBody; |
| | 3025 | | } |
| 0 | 3026 | | } |
| 0 | 3027 | | catch (JsonException) |
| | 3028 | | { |
| | 3029 | | // Ignore the exception |
| 0 | 3030 | | } |
| 0 | 3031 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 3032 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 3033 | | if (_shouldTrace) |
| | 3034 | | { |
| 0 | 3035 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 3036 | | } |
| 0 | 3037 | | _httpRequest.Dispose(); |
| 0 | 3038 | | if (_httpResponse != null) |
| | 3039 | | { |
| 0 | 3040 | | _httpResponse.Dispose(); |
| | 3041 | | } |
| 0 | 3042 | | throw ex; |
| | 3043 | | } |
| | 3044 | | // Create Result |
| 2 | 3045 | | var _result = new HttpOperationResponse<DomainModelResults>(); |
| 2 | 3046 | | _result.Request = _httpRequest; |
| 2 | 3047 | | _result.Response = _httpResponse; |
| | 3048 | | // Deserialize Response |
| 2 | 3049 | | if ((int)_statusCode == 200) |
| | 3050 | | { |
| 2 | 3051 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 3052 | | try |
| | 3053 | | { |
| 2 | 3054 | | _result.Body = SafeJsonConvert.DeserializeObject<DomainModelResults>(_responseContent, Deserializati |
| 2 | 3055 | | } |
| 0 | 3056 | | catch (JsonException ex) |
| | 3057 | | { |
| 0 | 3058 | | _httpRequest.Dispose(); |
| 0 | 3059 | | if (_httpResponse != null) |
| | 3060 | | { |
| 0 | 3061 | | _httpResponse.Dispose(); |
| | 3062 | | } |
| 0 | 3063 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 3064 | | } |
| | 3065 | | } |
| 2 | 3066 | | if (_shouldTrace) |
| | 3067 | | { |
| 0 | 3068 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 3069 | | } |
| 2 | 3070 | | return _result; |
| 2 | 3071 | | } |
| | 3072 | |
|
| | 3073 | | /// <summary> |
| | 3074 | | /// Optical Character Recognition (OCR) detects text in an image and extracts |
| | 3075 | | /// the recognized characters into a machine-usable character stream. |
| | 3076 | | /// Upon success, the OCR results will be returned. |
| | 3077 | | /// Upon failure, the error code together with an error message will be |
| | 3078 | | /// returned. The error code can be one of InvalidImageUrl, InvalidImageFormat, |
| | 3079 | | /// InvalidImageSize, NotSupportedImage, NotSupportedLanguage, or |
| | 3080 | | /// InternalServerError. |
| | 3081 | | /// </summary> |
| | 3082 | | /// <param name='detectOrientation'> |
| | 3083 | | /// Whether detect the text orientation in the image. With |
| | 3084 | | /// detectOrientation=true the OCR service tries to detect the image |
| | 3085 | | /// orientation and correct it before further processing (e.g. if it's |
| | 3086 | | /// upside-down). |
| | 3087 | | /// </param> |
| | 3088 | | /// <param name='image'> |
| | 3089 | | /// An image stream. |
| | 3090 | | /// </param> |
| | 3091 | | /// <param name='language'> |
| | 3092 | | /// The BCP-47 language code of the text to be detected in the image. The |
| | 3093 | | /// default value is 'unk'. Possible values include: 'unk', 'zh-Hans', |
| | 3094 | | /// 'zh-Hant', 'cs', 'da', 'nl', 'en', 'fi', 'fr', 'de', 'el', 'hu', 'it', |
| | 3095 | | /// 'ja', 'ko', 'nb', 'pl', 'pt', 'ru', 'es', 'sv', 'tr', 'ar', 'ro', |
| | 3096 | | /// 'sr-Cyrl', 'sr-Latn', 'sk' |
| | 3097 | | /// </param> |
| | 3098 | | /// <param name='customHeaders'> |
| | 3099 | | /// Headers that will be added to request. |
| | 3100 | | /// </param> |
| | 3101 | | /// <param name='cancellationToken'> |
| | 3102 | | /// The cancellation token. |
| | 3103 | | /// </param> |
| | 3104 | | /// <exception cref="ComputerVisionErrorException"> |
| | 3105 | | /// Thrown when the operation returned an invalid status code |
| | 3106 | | /// </exception> |
| | 3107 | | /// <exception cref="SerializationException"> |
| | 3108 | | /// Thrown when unable to deserialize the response |
| | 3109 | | /// </exception> |
| | 3110 | | /// <exception cref="ValidationException"> |
| | 3111 | | /// Thrown when a required parameter is null |
| | 3112 | | /// </exception> |
| | 3113 | | /// <exception cref="System.ArgumentNullException"> |
| | 3114 | | /// Thrown when a required parameter is null |
| | 3115 | | /// </exception> |
| | 3116 | | /// <return> |
| | 3117 | | /// A response object containing the response body and response headers. |
| | 3118 | | /// </return> |
| | 3119 | | public async Task<HttpOperationResponse<OcrResult>> RecognizePrintedTextInStreamWithHttpMessagesAsync(bool detec |
| | 3120 | | { |
| 2 | 3121 | | if (Endpoint == null) |
| | 3122 | | { |
| 0 | 3123 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 3124 | | } |
| 2 | 3125 | | if (image == null) |
| | 3126 | | { |
| 0 | 3127 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 3128 | | } |
| | 3129 | | // Tracing |
| 2 | 3130 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 3131 | | string _invocationId = null; |
| 2 | 3132 | | if (_shouldTrace) |
| | 3133 | | { |
| 0 | 3134 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 3135 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 3136 | | tracingParameters.Add("detectOrientation", detectOrientation); |
| 0 | 3137 | | tracingParameters.Add("language", language); |
| 0 | 3138 | | tracingParameters.Add("image", image); |
| 0 | 3139 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 3140 | | ServiceClientTracing.Enter(_invocationId, this, "RecognizePrintedTextInStream", tracingParameters); |
| | 3141 | | } |
| | 3142 | | // Construct URL |
| 2 | 3143 | | var _baseUrl = BaseUri; |
| 2 | 3144 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "ocr"; |
| 2 | 3145 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 3146 | | List<string> _queryParameters = new List<string>(); |
| 2 | 3147 | | _queryParameters.Add(string.Format("detectOrientation={0}", System.Uri.EscapeDataString(SafeJsonConvert.Seri |
| 2 | 3148 | | if (language != null) |
| | 3149 | | { |
| 0 | 3150 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(SafeJsonConvert.Serialize |
| | 3151 | | } |
| 2 | 3152 | | if (_queryParameters.Count > 0) |
| | 3153 | | { |
| 2 | 3154 | | _url += "?" + string.Join("&", _queryParameters); |
| | 3155 | | } |
| | 3156 | | // Create HTTP transport objects |
| 2 | 3157 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 3158 | | HttpResponseMessage _httpResponse = null; |
| 2 | 3159 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 3160 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 3161 | | // Set Headers |
| | 3162 | |
|
| | 3163 | |
|
| 2 | 3164 | | if (customHeaders != null) |
| | 3165 | | { |
| 0 | 3166 | | foreach(var _header in customHeaders) |
| | 3167 | | { |
| 0 | 3168 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 3169 | | { |
| 0 | 3170 | | _httpRequest.Headers.Remove(_header.Key); |
| | 3171 | | } |
| 0 | 3172 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 3173 | | } |
| | 3174 | | } |
| | 3175 | |
|
| | 3176 | | // Serialize Request |
| 2 | 3177 | | string _requestContent = null; |
| 2 | 3178 | | if(image == null) |
| | 3179 | | { |
| 0 | 3180 | | throw new System.ArgumentNullException("image"); |
| | 3181 | | } |
| 2 | 3182 | | if (image != null && image != Stream.Null) |
| | 3183 | | { |
| 2 | 3184 | | _httpRequest.Content = new StreamContent(image); |
| 2 | 3185 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 3186 | | } |
| | 3187 | | // Set Credentials |
| 2 | 3188 | | if (Credentials != null) |
| | 3189 | | { |
| 2 | 3190 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 3191 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 3192 | | } |
| | 3193 | | // Send Request |
| 2 | 3194 | | if (_shouldTrace) |
| | 3195 | | { |
| 0 | 3196 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 3197 | | } |
| 2 | 3198 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 3199 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 3200 | | if (_shouldTrace) |
| | 3201 | | { |
| 0 | 3202 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 3203 | | } |
| 2 | 3204 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 3205 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 3206 | | string _responseContent = null; |
| 2 | 3207 | | if ((int)_statusCode != 200) |
| | 3208 | | { |
| 0 | 3209 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 3210 | | try |
| | 3211 | | { |
| 0 | 3212 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 3213 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 3214 | | if (_errorBody != null) |
| | 3215 | | { |
| 0 | 3216 | | ex.Body = _errorBody; |
| | 3217 | | } |
| 0 | 3218 | | } |
| 0 | 3219 | | catch (JsonException) |
| | 3220 | | { |
| | 3221 | | // Ignore the exception |
| 0 | 3222 | | } |
| 0 | 3223 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 3224 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 3225 | | if (_shouldTrace) |
| | 3226 | | { |
| 0 | 3227 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 3228 | | } |
| 0 | 3229 | | _httpRequest.Dispose(); |
| 0 | 3230 | | if (_httpResponse != null) |
| | 3231 | | { |
| 0 | 3232 | | _httpResponse.Dispose(); |
| | 3233 | | } |
| 0 | 3234 | | throw ex; |
| | 3235 | | } |
| | 3236 | | // Create Result |
| 2 | 3237 | | var _result = new HttpOperationResponse<OcrResult>(); |
| 2 | 3238 | | _result.Request = _httpRequest; |
| 2 | 3239 | | _result.Response = _httpResponse; |
| | 3240 | | // Deserialize Response |
| 2 | 3241 | | if ((int)_statusCode == 200) |
| | 3242 | | { |
| 2 | 3243 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 3244 | | try |
| | 3245 | | { |
| 2 | 3246 | | _result.Body = SafeJsonConvert.DeserializeObject<OcrResult>(_responseContent, DeserializationSetting |
| 2 | 3247 | | } |
| 0 | 3248 | | catch (JsonException ex) |
| | 3249 | | { |
| 0 | 3250 | | _httpRequest.Dispose(); |
| 0 | 3251 | | if (_httpResponse != null) |
| | 3252 | | { |
| 0 | 3253 | | _httpResponse.Dispose(); |
| | 3254 | | } |
| 0 | 3255 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 3256 | | } |
| | 3257 | | } |
| 2 | 3258 | | if (_shouldTrace) |
| | 3259 | | { |
| 0 | 3260 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 3261 | | } |
| 2 | 3262 | | return _result; |
| 2 | 3263 | | } |
| | 3264 | |
|
| | 3265 | | /// <summary> |
| | 3266 | | /// This operation generates a list of words, or tags, that are relevant to the |
| | 3267 | | /// content of the supplied image. The Computer Vision API can return tags |
| | 3268 | | /// based on objects, living beings, scenery or actions found in images. Unlike |
| | 3269 | | /// categories, tags are not organized according to a hierarchical |
| | 3270 | | /// classification system, but correspond to image content. Tags may contain |
| | 3271 | | /// hints to avoid ambiguity or provide context, for example the tag |
| | 3272 | | /// "ascomycete" may be accompanied by the hint "fungus". |
| | 3273 | | /// Two input methods are supported -- (1) Uploading an image or (2) specifying |
| | 3274 | | /// an image URL. |
| | 3275 | | /// A successful response will be returned in JSON. If the request failed, the |
| | 3276 | | /// response will contain an error code and a message to help understand what |
| | 3277 | | /// went wrong. |
| | 3278 | | /// </summary> |
| | 3279 | | /// <param name='image'> |
| | 3280 | | /// An image stream. |
| | 3281 | | /// </param> |
| | 3282 | | /// <param name='language'> |
| | 3283 | | /// The desired language for output generation. If this parameter is not |
| | 3284 | | /// specified, the default value is &quot;en&quot;.Supported |
| | 3285 | | /// languages:en - English, Default. es - Spanish, ja - Japanese, pt - |
| | 3286 | | /// Portuguese, zh - Simplified Chinese. Possible values include: 'en', 'es', |
| | 3287 | | /// 'ja', 'pt', 'zh' |
| | 3288 | | /// </param> |
| | 3289 | | /// <param name='customHeaders'> |
| | 3290 | | /// Headers that will be added to request. |
| | 3291 | | /// </param> |
| | 3292 | | /// <param name='cancellationToken'> |
| | 3293 | | /// The cancellation token. |
| | 3294 | | /// </param> |
| | 3295 | | /// <exception cref="ComputerVisionErrorException"> |
| | 3296 | | /// Thrown when the operation returned an invalid status code |
| | 3297 | | /// </exception> |
| | 3298 | | /// <exception cref="SerializationException"> |
| | 3299 | | /// Thrown when unable to deserialize the response |
| | 3300 | | /// </exception> |
| | 3301 | | /// <exception cref="ValidationException"> |
| | 3302 | | /// Thrown when a required parameter is null |
| | 3303 | | /// </exception> |
| | 3304 | | /// <exception cref="System.ArgumentNullException"> |
| | 3305 | | /// Thrown when a required parameter is null |
| | 3306 | | /// </exception> |
| | 3307 | | /// <return> |
| | 3308 | | /// A response object containing the response body and response headers. |
| | 3309 | | /// </return> |
| | 3310 | | public async Task<HttpOperationResponse<TagResult>> TagImageInStreamWithHttpMessagesAsync(Stream image, string l |
| | 3311 | | { |
| 2 | 3312 | | if (Endpoint == null) |
| | 3313 | | { |
| 0 | 3314 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 3315 | | } |
| 2 | 3316 | | if (image == null) |
| | 3317 | | { |
| 0 | 3318 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 3319 | | } |
| | 3320 | | // Tracing |
| 2 | 3321 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 3322 | | string _invocationId = null; |
| 2 | 3323 | | if (_shouldTrace) |
| | 3324 | | { |
| 0 | 3325 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 3326 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 3327 | | tracingParameters.Add("language", language); |
| 0 | 3328 | | tracingParameters.Add("image", image); |
| 0 | 3329 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 3330 | | ServiceClientTracing.Enter(_invocationId, this, "TagImageInStream", tracingParameters); |
| | 3331 | | } |
| | 3332 | | // Construct URL |
| 2 | 3333 | | var _baseUrl = BaseUri; |
| 2 | 3334 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "tag"; |
| 2 | 3335 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 3336 | | List<string> _queryParameters = new List<string>(); |
| 2 | 3337 | | if (language != null) |
| | 3338 | | { |
| 2 | 3339 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(SafeJsonConvert.Serialize |
| | 3340 | | } |
| 2 | 3341 | | if (_queryParameters.Count > 0) |
| | 3342 | | { |
| 2 | 3343 | | _url += "?" + string.Join("&", _queryParameters); |
| | 3344 | | } |
| | 3345 | | // Create HTTP transport objects |
| 2 | 3346 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 3347 | | HttpResponseMessage _httpResponse = null; |
| 2 | 3348 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 3349 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 3350 | | // Set Headers |
| | 3351 | |
|
| | 3352 | |
|
| 2 | 3353 | | if (customHeaders != null) |
| | 3354 | | { |
| 0 | 3355 | | foreach(var _header in customHeaders) |
| | 3356 | | { |
| 0 | 3357 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 3358 | | { |
| 0 | 3359 | | _httpRequest.Headers.Remove(_header.Key); |
| | 3360 | | } |
| 0 | 3361 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 3362 | | } |
| | 3363 | | } |
| | 3364 | |
|
| | 3365 | | // Serialize Request |
| 2 | 3366 | | string _requestContent = null; |
| 2 | 3367 | | if(image == null) |
| | 3368 | | { |
| 0 | 3369 | | throw new System.ArgumentNullException("image"); |
| | 3370 | | } |
| 2 | 3371 | | if (image != null && image != Stream.Null) |
| | 3372 | | { |
| 2 | 3373 | | _httpRequest.Content = new StreamContent(image); |
| 2 | 3374 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 3375 | | } |
| | 3376 | | // Set Credentials |
| 2 | 3377 | | if (Credentials != null) |
| | 3378 | | { |
| 2 | 3379 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 3380 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 3381 | | } |
| | 3382 | | // Send Request |
| 2 | 3383 | | if (_shouldTrace) |
| | 3384 | | { |
| 0 | 3385 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 3386 | | } |
| 2 | 3387 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 3388 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 3389 | | if (_shouldTrace) |
| | 3390 | | { |
| 0 | 3391 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 3392 | | } |
| 2 | 3393 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 3394 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 3395 | | string _responseContent = null; |
| 2 | 3396 | | if ((int)_statusCode != 200) |
| | 3397 | | { |
| 0 | 3398 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 3399 | | try |
| | 3400 | | { |
| 0 | 3401 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 3402 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 3403 | | if (_errorBody != null) |
| | 3404 | | { |
| 0 | 3405 | | ex.Body = _errorBody; |
| | 3406 | | } |
| 0 | 3407 | | } |
| 0 | 3408 | | catch (JsonException) |
| | 3409 | | { |
| | 3410 | | // Ignore the exception |
| 0 | 3411 | | } |
| 0 | 3412 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 3413 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 3414 | | if (_shouldTrace) |
| | 3415 | | { |
| 0 | 3416 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 3417 | | } |
| 0 | 3418 | | _httpRequest.Dispose(); |
| 0 | 3419 | | if (_httpResponse != null) |
| | 3420 | | { |
| 0 | 3421 | | _httpResponse.Dispose(); |
| | 3422 | | } |
| 0 | 3423 | | throw ex; |
| | 3424 | | } |
| | 3425 | | // Create Result |
| 2 | 3426 | | var _result = new HttpOperationResponse<TagResult>(); |
| 2 | 3427 | | _result.Request = _httpRequest; |
| 2 | 3428 | | _result.Response = _httpResponse; |
| | 3429 | | // Deserialize Response |
| 2 | 3430 | | if ((int)_statusCode == 200) |
| | 3431 | | { |
| 2 | 3432 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 3433 | | try |
| | 3434 | | { |
| 2 | 3435 | | _result.Body = SafeJsonConvert.DeserializeObject<TagResult>(_responseContent, DeserializationSetting |
| 2 | 3436 | | } |
| 0 | 3437 | | catch (JsonException ex) |
| | 3438 | | { |
| 0 | 3439 | | _httpRequest.Dispose(); |
| 0 | 3440 | | if (_httpResponse != null) |
| | 3441 | | { |
| 0 | 3442 | | _httpResponse.Dispose(); |
| | 3443 | | } |
| 0 | 3444 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 3445 | | } |
| | 3446 | | } |
| 2 | 3447 | | if (_shouldTrace) |
| | 3448 | | { |
| 0 | 3449 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 3450 | | } |
| 2 | 3451 | | return _result; |
| 2 | 3452 | | } |
| | 3453 | |
|
| | 3454 | | /// <summary> |
| | 3455 | | /// Use this interface to get the result of a Read operation, employing the |
| | 3456 | | /// state-of-the-art Optical Character Recognition (OCR) algorithms optimized |
| | 3457 | | /// for text-heavy documents. When you use the Read interface, the response |
| | 3458 | | /// contains a field called 'Operation-Location'. The 'Operation-Location' |
| | 3459 | | /// field contains the URL that you must use for your 'GetReadResult' operation |
| | 3460 | | /// to access OCR results.​ |
| | 3461 | | /// </summary> |
| | 3462 | | /// <param name='url'> |
| | 3463 | | /// Publicly reachable URL of an image. |
| | 3464 | | /// </param> |
| | 3465 | | /// <param name='language'> |
| | 3466 | | /// The BCP-47 language code of the text in the document. Currently, only |
| | 3467 | | /// English ('en'), Dutch (‘nl’), French (‘fr’), German (‘de’), Italian (‘it’), |
| | 3468 | | /// Portuguese (‘pt), and Spanish ('es') are supported. Read supports auto |
| | 3469 | | /// language identification and multilanguage documents, so only provide a |
| | 3470 | | /// language code if you would like to force the documented to be processed as |
| | 3471 | | /// that specific language. Possible values include: 'en', 'es', 'fr', 'de', |
| | 3472 | | /// 'it', 'nl', 'pt' |
| | 3473 | | /// </param> |
| | 3474 | | /// <param name='customHeaders'> |
| | 3475 | | /// Headers that will be added to request. |
| | 3476 | | /// </param> |
| | 3477 | | /// <param name='cancellationToken'> |
| | 3478 | | /// The cancellation token. |
| | 3479 | | /// </param> |
| | 3480 | | /// <exception cref="ComputerVisionErrorException"> |
| | 3481 | | /// Thrown when the operation returned an invalid status code |
| | 3482 | | /// </exception> |
| | 3483 | | /// <exception cref="ValidationException"> |
| | 3484 | | /// Thrown when a required parameter is null |
| | 3485 | | /// </exception> |
| | 3486 | | /// <exception cref="System.ArgumentNullException"> |
| | 3487 | | /// Thrown when a required parameter is null |
| | 3488 | | /// </exception> |
| | 3489 | | /// <return> |
| | 3490 | | /// A response object containing the response body and response headers. |
| | 3491 | | /// </return> |
| | 3492 | | public async Task<HttpOperationHeaderResponse<ReadHeaders>> ReadWithHttpMessagesAsync(string url, string languag |
| | 3493 | | { |
| 0 | 3494 | | if (Endpoint == null) |
| | 3495 | | { |
| 0 | 3496 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 3497 | | } |
| 0 | 3498 | | if (url == null) |
| | 3499 | | { |
| 0 | 3500 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 3501 | | } |
| 0 | 3502 | | ImageUrl imageUrl = new ImageUrl(); |
| 0 | 3503 | | if (url != null) |
| | 3504 | | { |
| 0 | 3505 | | imageUrl.Url = url; |
| | 3506 | | } |
| | 3507 | | // Tracing |
| 0 | 3508 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 3509 | | string _invocationId = null; |
| 0 | 3510 | | if (_shouldTrace) |
| | 3511 | | { |
| 0 | 3512 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 3513 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 3514 | | tracingParameters.Add("language", language); |
| 0 | 3515 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 3516 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 3517 | | ServiceClientTracing.Enter(_invocationId, this, "Read", tracingParameters); |
| | 3518 | | } |
| | 3519 | | // Construct URL |
| 0 | 3520 | | var _baseUrl = BaseUri; |
| 0 | 3521 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "read/analyze"; |
| 0 | 3522 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 0 | 3523 | | List<string> _queryParameters = new List<string>(); |
| 0 | 3524 | | if (language != null) |
| | 3525 | | { |
| 0 | 3526 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(language))); |
| | 3527 | | } |
| 0 | 3528 | | if (_queryParameters.Count > 0) |
| | 3529 | | { |
| 0 | 3530 | | _url += "?" + string.Join("&", _queryParameters); |
| | 3531 | | } |
| | 3532 | | // Create HTTP transport objects |
| 0 | 3533 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 3534 | | HttpResponseMessage _httpResponse = null; |
| 0 | 3535 | | _httpRequest.Method = new HttpMethod("POST"); |
| 0 | 3536 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 3537 | | // Set Headers |
| | 3538 | |
|
| | 3539 | |
|
| 0 | 3540 | | if (customHeaders != null) |
| | 3541 | | { |
| 0 | 3542 | | foreach(var _header in customHeaders) |
| | 3543 | | { |
| 0 | 3544 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 3545 | | { |
| 0 | 3546 | | _httpRequest.Headers.Remove(_header.Key); |
| | 3547 | | } |
| 0 | 3548 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 3549 | | } |
| | 3550 | | } |
| | 3551 | |
|
| | 3552 | | // Serialize Request |
| 0 | 3553 | | string _requestContent = null; |
| 0 | 3554 | | if(imageUrl != null) |
| | 3555 | | { |
| 0 | 3556 | | _requestContent = SafeJsonConvert.SerializeObject(imageUrl, SerializationSettings); |
| 0 | 3557 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 0 | 3558 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 3559 | | } |
| | 3560 | | // Set Credentials |
| 0 | 3561 | | if (Credentials != null) |
| | 3562 | | { |
| 0 | 3563 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 3564 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 3565 | | } |
| | 3566 | | // Send Request |
| 0 | 3567 | | if (_shouldTrace) |
| | 3568 | | { |
| 0 | 3569 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 3570 | | } |
| 0 | 3571 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 3572 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 3573 | | if (_shouldTrace) |
| | 3574 | | { |
| 0 | 3575 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 3576 | | } |
| 0 | 3577 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 3578 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 3579 | | string _responseContent = null; |
| 0 | 3580 | | if ((int)_statusCode != 202) |
| | 3581 | | { |
| 0 | 3582 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 3583 | | try |
| | 3584 | | { |
| 0 | 3585 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 3586 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 3587 | | if (_errorBody != null) |
| | 3588 | | { |
| 0 | 3589 | | ex.Body = _errorBody; |
| | 3590 | | } |
| 0 | 3591 | | } |
| 0 | 3592 | | catch (JsonException) |
| | 3593 | | { |
| | 3594 | | // Ignore the exception |
| 0 | 3595 | | } |
| 0 | 3596 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 3597 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 3598 | | if (_shouldTrace) |
| | 3599 | | { |
| 0 | 3600 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 3601 | | } |
| 0 | 3602 | | _httpRequest.Dispose(); |
| 0 | 3603 | | if (_httpResponse != null) |
| | 3604 | | { |
| 0 | 3605 | | _httpResponse.Dispose(); |
| | 3606 | | } |
| 0 | 3607 | | throw ex; |
| | 3608 | | } |
| | 3609 | | // Create Result |
| 0 | 3610 | | var _result = new HttpOperationHeaderResponse<ReadHeaders>(); |
| 0 | 3611 | | _result.Request = _httpRequest; |
| 0 | 3612 | | _result.Response = _httpResponse; |
| | 3613 | | try |
| | 3614 | | { |
| 0 | 3615 | | _result.Headers = _httpResponse.GetHeadersAsJson().ToObject<ReadHeaders>(JsonSerializer.Create(Deseriali |
| 0 | 3616 | | } |
| 0 | 3617 | | catch (JsonException ex) |
| | 3618 | | { |
| 0 | 3619 | | _httpRequest.Dispose(); |
| 0 | 3620 | | if (_httpResponse != null) |
| | 3621 | | { |
| 0 | 3622 | | _httpResponse.Dispose(); |
| | 3623 | | } |
| 0 | 3624 | | throw new SerializationException("Unable to deserialize the headers.", _httpResponse.GetHeadersAsJson(). |
| | 3625 | | } |
| 0 | 3626 | | if (_shouldTrace) |
| | 3627 | | { |
| 0 | 3628 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 3629 | | } |
| 0 | 3630 | | return _result; |
| 0 | 3631 | | } |
| | 3632 | |
|
| | 3633 | | /// <summary> |
| | 3634 | | /// This interface is used for getting OCR results of Read operation. The URL |
| | 3635 | | /// to this interface should be retrieved from 'Operation-Location' field |
| | 3636 | | /// returned from Read interface. |
| | 3637 | | /// </summary> |
| | 3638 | | /// <param name='operationId'> |
| | 3639 | | /// Id of read operation returned in the response of the 'Read' interface. |
| | 3640 | | /// </param> |
| | 3641 | | /// <param name='customHeaders'> |
| | 3642 | | /// Headers that will be added to request. |
| | 3643 | | /// </param> |
| | 3644 | | /// <param name='cancellationToken'> |
| | 3645 | | /// The cancellation token. |
| | 3646 | | /// </param> |
| | 3647 | | /// <exception cref="ComputerVisionErrorException"> |
| | 3648 | | /// Thrown when the operation returned an invalid status code |
| | 3649 | | /// </exception> |
| | 3650 | | /// <exception cref="SerializationException"> |
| | 3651 | | /// Thrown when unable to deserialize the response |
| | 3652 | | /// </exception> |
| | 3653 | | /// <exception cref="ValidationException"> |
| | 3654 | | /// Thrown when a required parameter is null |
| | 3655 | | /// </exception> |
| | 3656 | | /// <exception cref="System.ArgumentNullException"> |
| | 3657 | | /// Thrown when a required parameter is null |
| | 3658 | | /// </exception> |
| | 3659 | | /// <return> |
| | 3660 | | /// A response object containing the response body and response headers. |
| | 3661 | | /// </return> |
| | 3662 | | public async Task<HttpOperationResponse<ReadOperationResult>> GetReadResultWithHttpMessagesAsync(System.Guid ope |
| | 3663 | | { |
| 0 | 3664 | | if (Endpoint == null) |
| | 3665 | | { |
| 0 | 3666 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 3667 | | } |
| | 3668 | | // Tracing |
| 0 | 3669 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 3670 | | string _invocationId = null; |
| 0 | 3671 | | if (_shouldTrace) |
| | 3672 | | { |
| 0 | 3673 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 3674 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 3675 | | tracingParameters.Add("operationId", operationId); |
| 0 | 3676 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 3677 | | ServiceClientTracing.Enter(_invocationId, this, "GetReadResult", tracingParameters); |
| | 3678 | | } |
| | 3679 | | // Construct URL |
| 0 | 3680 | | var _baseUrl = BaseUri; |
| 0 | 3681 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "read/analyzeResults/{operationId}"; |
| 0 | 3682 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 0 | 3683 | | _url = _url.Replace("{operationId}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject(operationId |
| | 3684 | | // Create HTTP transport objects |
| 0 | 3685 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 3686 | | HttpResponseMessage _httpResponse = null; |
| 0 | 3687 | | _httpRequest.Method = new HttpMethod("GET"); |
| 0 | 3688 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 3689 | | // Set Headers |
| | 3690 | |
|
| | 3691 | |
|
| 0 | 3692 | | if (customHeaders != null) |
| | 3693 | | { |
| 0 | 3694 | | foreach(var _header in customHeaders) |
| | 3695 | | { |
| 0 | 3696 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 3697 | | { |
| 0 | 3698 | | _httpRequest.Headers.Remove(_header.Key); |
| | 3699 | | } |
| 0 | 3700 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 3701 | | } |
| | 3702 | | } |
| | 3703 | |
|
| | 3704 | | // Serialize Request |
| 0 | 3705 | | string _requestContent = null; |
| | 3706 | | // Set Credentials |
| 0 | 3707 | | if (Credentials != null) |
| | 3708 | | { |
| 0 | 3709 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 3710 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 3711 | | } |
| | 3712 | | // Send Request |
| 0 | 3713 | | if (_shouldTrace) |
| | 3714 | | { |
| 0 | 3715 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 3716 | | } |
| 0 | 3717 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 3718 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 3719 | | if (_shouldTrace) |
| | 3720 | | { |
| 0 | 3721 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 3722 | | } |
| 0 | 3723 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 3724 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 3725 | | string _responseContent = null; |
| 0 | 3726 | | if ((int)_statusCode != 200) |
| | 3727 | | { |
| 0 | 3728 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 3729 | | try |
| | 3730 | | { |
| 0 | 3731 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 3732 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 3733 | | if (_errorBody != null) |
| | 3734 | | { |
| 0 | 3735 | | ex.Body = _errorBody; |
| | 3736 | | } |
| 0 | 3737 | | } |
| 0 | 3738 | | catch (JsonException) |
| | 3739 | | { |
| | 3740 | | // Ignore the exception |
| 0 | 3741 | | } |
| 0 | 3742 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 3743 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 3744 | | if (_shouldTrace) |
| | 3745 | | { |
| 0 | 3746 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 3747 | | } |
| 0 | 3748 | | _httpRequest.Dispose(); |
| 0 | 3749 | | if (_httpResponse != null) |
| | 3750 | | { |
| 0 | 3751 | | _httpResponse.Dispose(); |
| | 3752 | | } |
| 0 | 3753 | | throw ex; |
| | 3754 | | } |
| | 3755 | | // Create Result |
| 0 | 3756 | | var _result = new HttpOperationResponse<ReadOperationResult>(); |
| 0 | 3757 | | _result.Request = _httpRequest; |
| 0 | 3758 | | _result.Response = _httpResponse; |
| | 3759 | | // Deserialize Response |
| 0 | 3760 | | if ((int)_statusCode == 200) |
| | 3761 | | { |
| 0 | 3762 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 3763 | | try |
| | 3764 | | { |
| 0 | 3765 | | _result.Body = SafeJsonConvert.DeserializeObject<ReadOperationResult>(_responseContent, Deserializat |
| 0 | 3766 | | } |
| 0 | 3767 | | catch (JsonException ex) |
| | 3768 | | { |
| 0 | 3769 | | _httpRequest.Dispose(); |
| 0 | 3770 | | if (_httpResponse != null) |
| | 3771 | | { |
| 0 | 3772 | | _httpResponse.Dispose(); |
| | 3773 | | } |
| 0 | 3774 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 3775 | | } |
| | 3776 | | } |
| 0 | 3777 | | if (_shouldTrace) |
| | 3778 | | { |
| 0 | 3779 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 3780 | | } |
| 0 | 3781 | | return _result; |
| 0 | 3782 | | } |
| | 3783 | |
|
| | 3784 | | /// <summary> |
| | 3785 | | /// Use this interface to get the result of a Read operation, employing the |
| | 3786 | | /// state-of-the-art Optical Character Recognition (OCR) algorithms optimized |
| | 3787 | | /// for text-heavy documents. When you use the Read interface, the response |
| | 3788 | | /// contains a field called 'Operation-Location'. The 'Operation-Location' |
| | 3789 | | /// field contains the URL that you must use for your 'GetReadResult' operation |
| | 3790 | | /// to access OCR results.​ |
| | 3791 | | /// </summary> |
| | 3792 | | /// <param name='image'> |
| | 3793 | | /// An image stream. |
| | 3794 | | /// </param> |
| | 3795 | | /// <param name='language'> |
| | 3796 | | /// The BCP-47 language code of the text in the document. Currently, only |
| | 3797 | | /// English ('en'), Dutch (‘nl’), French (‘fr’), German (‘de’), Italian (‘it’), |
| | 3798 | | /// Portuguese (‘pt), and Spanish ('es') are supported. Read supports auto |
| | 3799 | | /// language identification and multilanguage documents, so only provide a |
| | 3800 | | /// language code if you would like to force the documented to be processed as |
| | 3801 | | /// that specific language. Possible values include: 'en', 'es', 'fr', 'de', |
| | 3802 | | /// 'it', 'nl', 'pt' |
| | 3803 | | /// </param> |
| | 3804 | | /// <param name='customHeaders'> |
| | 3805 | | /// Headers that will be added to request. |
| | 3806 | | /// </param> |
| | 3807 | | /// <param name='cancellationToken'> |
| | 3808 | | /// The cancellation token. |
| | 3809 | | /// </param> |
| | 3810 | | /// <exception cref="ComputerVisionErrorException"> |
| | 3811 | | /// Thrown when the operation returned an invalid status code |
| | 3812 | | /// </exception> |
| | 3813 | | /// <exception cref="ValidationException"> |
| | 3814 | | /// Thrown when a required parameter is null |
| | 3815 | | /// </exception> |
| | 3816 | | /// <exception cref="System.ArgumentNullException"> |
| | 3817 | | /// Thrown when a required parameter is null |
| | 3818 | | /// </exception> |
| | 3819 | | /// <return> |
| | 3820 | | /// A response object containing the response body and response headers. |
| | 3821 | | /// </return> |
| | 3822 | | public async Task<HttpOperationHeaderResponse<ReadInStreamHeaders>> ReadInStreamWithHttpMessagesAsync(Stream ima |
| | 3823 | | { |
| 0 | 3824 | | if (Endpoint == null) |
| | 3825 | | { |
| 0 | 3826 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 3827 | | } |
| 0 | 3828 | | if (image == null) |
| | 3829 | | { |
| 0 | 3830 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 3831 | | } |
| | 3832 | | // Tracing |
| 0 | 3833 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 3834 | | string _invocationId = null; |
| 0 | 3835 | | if (_shouldTrace) |
| | 3836 | | { |
| 0 | 3837 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 3838 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 3839 | | tracingParameters.Add("language", language); |
| 0 | 3840 | | tracingParameters.Add("image", image); |
| 0 | 3841 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 3842 | | ServiceClientTracing.Enter(_invocationId, this, "ReadInStream", tracingParameters); |
| | 3843 | | } |
| | 3844 | | // Construct URL |
| 0 | 3845 | | var _baseUrl = BaseUri; |
| 0 | 3846 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "read/analyze"; |
| 0 | 3847 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 0 | 3848 | | List<string> _queryParameters = new List<string>(); |
| 0 | 3849 | | if (language != null) |
| | 3850 | | { |
| 0 | 3851 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(language))); |
| | 3852 | | } |
| 0 | 3853 | | if (_queryParameters.Count > 0) |
| | 3854 | | { |
| 0 | 3855 | | _url += "?" + string.Join("&", _queryParameters); |
| | 3856 | | } |
| | 3857 | | // Create HTTP transport objects |
| 0 | 3858 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 3859 | | HttpResponseMessage _httpResponse = null; |
| 0 | 3860 | | _httpRequest.Method = new HttpMethod("POST"); |
| 0 | 3861 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 3862 | | // Set Headers |
| | 3863 | |
|
| | 3864 | |
|
| 0 | 3865 | | if (customHeaders != null) |
| | 3866 | | { |
| 0 | 3867 | | foreach(var _header in customHeaders) |
| | 3868 | | { |
| 0 | 3869 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 3870 | | { |
| 0 | 3871 | | _httpRequest.Headers.Remove(_header.Key); |
| | 3872 | | } |
| 0 | 3873 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 3874 | | } |
| | 3875 | | } |
| | 3876 | |
|
| | 3877 | | // Serialize Request |
| 0 | 3878 | | string _requestContent = null; |
| 0 | 3879 | | if(image == null) |
| | 3880 | | { |
| 0 | 3881 | | throw new System.ArgumentNullException("image"); |
| | 3882 | | } |
| 0 | 3883 | | if (image != null && image != Stream.Null) |
| | 3884 | | { |
| 0 | 3885 | | _httpRequest.Content = new StreamContent(image); |
| 0 | 3886 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 3887 | | } |
| | 3888 | | // Set Credentials |
| 0 | 3889 | | if (Credentials != null) |
| | 3890 | | { |
| 0 | 3891 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 3892 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 3893 | | } |
| | 3894 | | // Send Request |
| 0 | 3895 | | if (_shouldTrace) |
| | 3896 | | { |
| 0 | 3897 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 3898 | | } |
| 0 | 3899 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 3900 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 3901 | | if (_shouldTrace) |
| | 3902 | | { |
| 0 | 3903 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 3904 | | } |
| 0 | 3905 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 3906 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 3907 | | string _responseContent = null; |
| 0 | 3908 | | if ((int)_statusCode != 202) |
| | 3909 | | { |
| 0 | 3910 | | var ex = new ComputerVisionErrorException(string.Format("Operation returned an invalid status code '{0}' |
| | 3911 | | try |
| | 3912 | | { |
| 0 | 3913 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 3914 | | ComputerVisionError _errorBody = SafeJsonConvert.DeserializeObject<ComputerVisionError>(_responseCo |
| 0 | 3915 | | if (_errorBody != null) |
| | 3916 | | { |
| 0 | 3917 | | ex.Body = _errorBody; |
| | 3918 | | } |
| 0 | 3919 | | } |
| 0 | 3920 | | catch (JsonException) |
| | 3921 | | { |
| | 3922 | | // Ignore the exception |
| 0 | 3923 | | } |
| 0 | 3924 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 3925 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 3926 | | if (_shouldTrace) |
| | 3927 | | { |
| 0 | 3928 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 3929 | | } |
| 0 | 3930 | | _httpRequest.Dispose(); |
| 0 | 3931 | | if (_httpResponse != null) |
| | 3932 | | { |
| 0 | 3933 | | _httpResponse.Dispose(); |
| | 3934 | | } |
| 0 | 3935 | | throw ex; |
| | 3936 | | } |
| | 3937 | | // Create Result |
| 0 | 3938 | | var _result = new HttpOperationHeaderResponse<ReadInStreamHeaders>(); |
| 0 | 3939 | | _result.Request = _httpRequest; |
| 0 | 3940 | | _result.Response = _httpResponse; |
| | 3941 | | try |
| | 3942 | | { |
| 0 | 3943 | | _result.Headers = _httpResponse.GetHeadersAsJson().ToObject<ReadInStreamHeaders>(JsonSerializer.Create(D |
| 0 | 3944 | | } |
| 0 | 3945 | | catch (JsonException ex) |
| | 3946 | | { |
| 0 | 3947 | | _httpRequest.Dispose(); |
| 0 | 3948 | | if (_httpResponse != null) |
| | 3949 | | { |
| 0 | 3950 | | _httpResponse.Dispose(); |
| | 3951 | | } |
| 0 | 3952 | | throw new SerializationException("Unable to deserialize the headers.", _httpResponse.GetHeadersAsJson(). |
| | 3953 | | } |
| 0 | 3954 | | if (_shouldTrace) |
| | 3955 | | { |
| 0 | 3956 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 3957 | | } |
| 0 | 3958 | | return _result; |
| 0 | 3959 | | } |
| | 3960 | |
|
| | 3961 | | } |
| | 3962 | | } |