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