| | 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.Face |
| | 12 | | { |
| | 13 | | using Microsoft.Rest; |
| | 14 | | using Models; |
| | 15 | | using Newtonsoft.Json; |
| | 16 | | using System.Collections; |
| | 17 | | using System.Collections.Generic; |
| | 18 | | using System.IO; |
| | 19 | | using System.Linq; |
| | 20 | | using System.Net; |
| | 21 | | using System.Net.Http; |
| | 22 | | using System.Threading; |
| | 23 | | using System.Threading.Tasks; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// FaceListOperations operations. |
| | 27 | | /// </summary> |
| | 28 | | public partial class FaceListOperations : IServiceOperations<FaceClient>, IFaceListOperations |
| | 29 | | { |
| | 30 | | /// <summary> |
| | 31 | | /// Initializes a new instance of the FaceListOperations class. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name='client'> |
| | 34 | | /// Reference to the service client. |
| | 35 | | /// </param> |
| | 36 | | /// <exception cref="System.ArgumentNullException"> |
| | 37 | | /// Thrown when a required parameter is null |
| | 38 | | /// </exception> |
| 34 | 39 | | public FaceListOperations(FaceClient client) |
| | 40 | | { |
| 34 | 41 | | if (client == null) |
| | 42 | | { |
| 0 | 43 | | throw new System.ArgumentNullException("client"); |
| | 44 | | } |
| 34 | 45 | | Client = client; |
| 34 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets a reference to the FaceClient |
| | 50 | | /// </summary> |
| 212 | 51 | | public FaceClient Client { get; private set; } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Create an empty face list with user-specified faceListId, name, an optional |
| | 55 | | /// userData and recognitionModel. Up to 64 face lists are allowed in one |
| | 56 | | /// subscription. |
| | 57 | | /// <br /> Face list is a list of faces, up to 1,000 faces, and used by |
| | 58 | | /// [Face - Find |
| | 59 | | /// Similar](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395237). |
| | 60 | | /// <br /> After creation, user should use [FaceList - Add |
| | 61 | | /// Face](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395250) |
| | 62 | | /// to import the faces. No image will be stored. Only the extracted face |
| | 63 | | /// features are stored on server until [FaceList - |
| | 64 | | /// Delete](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524f) |
| | 65 | | /// is called. |
| | 66 | | /// <br /> Find Similar is used for scenario like finding celebrity-like |
| | 67 | | /// faces, similar face filtering, or as a light way face identification. But |
| | 68 | | /// if the actual use is to identify person, please use |
| | 69 | | /// [PersonGroup](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395244) |
| | 70 | | /// / |
| | 71 | | /// [LargePersonGroup](/docs/services/563879b61984550e40cbbe8d/operations/599acdee6ac60f11b48b5a9d) |
| | 72 | | /// and [Face - |
| | 73 | | /// Identify](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395239). |
| | 74 | | /// <br /> Please consider |
| | 75 | | /// [LargeFaceList](/docs/services/563879b61984550e40cbbe8d/operations/5a157b68d2de3616c086f2cc) |
| | 76 | | /// when the face number is large. It can support up to 1,000,000 faces. |
| | 77 | | /// <br />'recognitionModel' should be specified to associate with this |
| | 78 | | /// face list. The default value for 'recognitionModel' is 'recognition_01', if |
| | 79 | | /// the latest model needed, please explicitly specify the model you need in |
| | 80 | | /// this parameter. New faces that are added to an existing face list will use |
| | 81 | | /// the recognition model that's already associated with the collection. |
| | 82 | | /// Existing face features in a face list can't be updated to features |
| | 83 | | /// extracted by another version of recognition model. |
| | 84 | | /// * 'recognition_01': The default recognition model for [FaceList- |
| | 85 | | /// Create](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524b). |
| | 86 | | /// All those face lists created before 2019 March are bonded with this |
| | 87 | | /// recognition model. |
| | 88 | | /// * 'recognition_02': Recognition model released in 2019 March. |
| | 89 | | /// 'recognition_02' is recommended since its overall accuracy is improved |
| | 90 | | /// compared with 'recognition_01'. |
| | 91 | | /// </summary> |
| | 92 | | /// <param name='faceListId'> |
| | 93 | | /// Id referencing a particular face list. |
| | 94 | | /// </param> |
| | 95 | | /// <param name='name'> |
| | 96 | | /// User defined name, maximum length is 128. |
| | 97 | | /// </param> |
| | 98 | | /// <param name='userData'> |
| | 99 | | /// User specified data. Length should not exceed 16KB. |
| | 100 | | /// </param> |
| | 101 | | /// <param name='recognitionModel'> |
| | 102 | | /// Possible values include: 'recognition_01', 'recognition_02' |
| | 103 | | /// </param> |
| | 104 | | /// <param name='customHeaders'> |
| | 105 | | /// Headers that will be added to request. |
| | 106 | | /// </param> |
| | 107 | | /// <param name='cancellationToken'> |
| | 108 | | /// The cancellation token. |
| | 109 | | /// </param> |
| | 110 | | /// <exception cref="APIErrorException"> |
| | 111 | | /// Thrown when the operation returned an invalid status code |
| | 112 | | /// </exception> |
| | 113 | | /// <exception cref="ValidationException"> |
| | 114 | | /// Thrown when a required parameter is null |
| | 115 | | /// </exception> |
| | 116 | | /// <exception cref="System.ArgumentNullException"> |
| | 117 | | /// Thrown when a required parameter is null |
| | 118 | | /// </exception> |
| | 119 | | /// <return> |
| | 120 | | /// A response object containing the response body and response headers. |
| | 121 | | /// </return> |
| | 122 | | public async Task<HttpOperationResponse> CreateWithHttpMessagesAsync(string faceListId, string name = default(st |
| | 123 | | { |
| 4 | 124 | | if (Client.Endpoint == null) |
| | 125 | | { |
| 0 | 126 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 127 | | } |
| 4 | 128 | | if (faceListId == null) |
| | 129 | | { |
| 0 | 130 | | throw new ValidationException(ValidationRules.CannotBeNull, "faceListId"); |
| | 131 | | } |
| 4 | 132 | | if (faceListId != null) |
| | 133 | | { |
| 4 | 134 | | if (faceListId.Length > 64) |
| | 135 | | { |
| 0 | 136 | | throw new ValidationException(ValidationRules.MaxLength, "faceListId", 64); |
| | 137 | | } |
| 4 | 138 | | if (!System.Text.RegularExpressions.Regex.IsMatch(faceListId, "^[a-z0-9-_]+$")) |
| | 139 | | { |
| 0 | 140 | | throw new ValidationException(ValidationRules.Pattern, "faceListId", "^[a-z0-9-_]+$"); |
| | 141 | | } |
| | 142 | | } |
| 4 | 143 | | if (name != null) |
| | 144 | | { |
| 4 | 145 | | if (name.Length > 128) |
| | 146 | | { |
| 0 | 147 | | throw new ValidationException(ValidationRules.MaxLength, "name", 128); |
| | 148 | | } |
| | 149 | | } |
| 4 | 150 | | if (userData != null) |
| | 151 | | { |
| 2 | 152 | | if (userData.Length > 16384) |
| | 153 | | { |
| 0 | 154 | | throw new ValidationException(ValidationRules.MaxLength, "userData", 16384); |
| | 155 | | } |
| | 156 | | } |
| 4 | 157 | | MetaDataContract body = new MetaDataContract(); |
| 4 | 158 | | if (name != null || userData != null || recognitionModel != null) |
| | 159 | | { |
| 4 | 160 | | body.Name = name; |
| 4 | 161 | | body.UserData = userData; |
| 4 | 162 | | body.RecognitionModel = recognitionModel; |
| | 163 | | } |
| | 164 | | // Tracing |
| 4 | 165 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 4 | 166 | | string _invocationId = null; |
| 4 | 167 | | if (_shouldTrace) |
| | 168 | | { |
| 0 | 169 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 170 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 171 | | tracingParameters.Add("faceListId", faceListId); |
| 0 | 172 | | tracingParameters.Add("body", body); |
| 0 | 173 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 174 | | ServiceClientTracing.Enter(_invocationId, this, "Create", tracingParameters); |
| | 175 | | } |
| | 176 | | // Construct URL |
| 4 | 177 | | var _baseUrl = Client.BaseUri; |
| 4 | 178 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "facelists/{faceListId}"; |
| 4 | 179 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 4 | 180 | | _url = _url.Replace("{faceListId}", System.Uri.EscapeDataString(faceListId)); |
| | 181 | | // Create HTTP transport objects |
| 4 | 182 | | var _httpRequest = new HttpRequestMessage(); |
| 4 | 183 | | HttpResponseMessage _httpResponse = null; |
| 4 | 184 | | _httpRequest.Method = new HttpMethod("PUT"); |
| 4 | 185 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 186 | | // Set Headers |
| | 187 | |
|
| | 188 | |
|
| 4 | 189 | | if (customHeaders != null) |
| | 190 | | { |
| 0 | 191 | | foreach(var _header in customHeaders) |
| | 192 | | { |
| 0 | 193 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 194 | | { |
| 0 | 195 | | _httpRequest.Headers.Remove(_header.Key); |
| | 196 | | } |
| 0 | 197 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 198 | | } |
| | 199 | | } |
| | 200 | |
|
| | 201 | | // Serialize Request |
| 4 | 202 | | string _requestContent = null; |
| 4 | 203 | | if(body != null) |
| | 204 | | { |
| 4 | 205 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(body, Client.SerializationSettings) |
| 4 | 206 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 4 | 207 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 208 | | } |
| | 209 | | // Set Credentials |
| 4 | 210 | | if (Client.Credentials != null) |
| | 211 | | { |
| 4 | 212 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 213 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 214 | | } |
| | 215 | | // Send Request |
| 4 | 216 | | if (_shouldTrace) |
| | 217 | | { |
| 0 | 218 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 219 | | } |
| 4 | 220 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 221 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 4 | 222 | | if (_shouldTrace) |
| | 223 | | { |
| 0 | 224 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 225 | | } |
| 4 | 226 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 4 | 227 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 228 | | string _responseContent = null; |
| 4 | 229 | | if ((int)_statusCode != 200) |
| | 230 | | { |
| 0 | 231 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | 232 | | try |
| | 233 | | { |
| 0 | 234 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 235 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| 0 | 236 | | if (_errorBody != null) |
| | 237 | | { |
| 0 | 238 | | ex.Body = _errorBody; |
| | 239 | | } |
| 0 | 240 | | } |
| 0 | 241 | | catch (JsonException) |
| | 242 | | { |
| | 243 | | // Ignore the exception |
| 0 | 244 | | } |
| 0 | 245 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 246 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 247 | | if (_shouldTrace) |
| | 248 | | { |
| 0 | 249 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 250 | | } |
| 0 | 251 | | _httpRequest.Dispose(); |
| 0 | 252 | | if (_httpResponse != null) |
| | 253 | | { |
| 0 | 254 | | _httpResponse.Dispose(); |
| | 255 | | } |
| 0 | 256 | | throw ex; |
| | 257 | | } |
| | 258 | | // Create Result |
| 4 | 259 | | var _result = new HttpOperationResponse(); |
| 4 | 260 | | _result.Request = _httpRequest; |
| 4 | 261 | | _result.Response = _httpResponse; |
| 4 | 262 | | if (_shouldTrace) |
| | 263 | | { |
| 0 | 264 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 265 | | } |
| 4 | 266 | | return _result; |
| 4 | 267 | | } |
| | 268 | |
|
| | 269 | | /// <summary> |
| | 270 | | /// Retrieve a face list’s faceListId, name, userData, recognitionModel and |
| | 271 | | /// faces in the face list. |
| | 272 | | /// |
| | 273 | | /// </summary> |
| | 274 | | /// <param name='faceListId'> |
| | 275 | | /// Id referencing a particular face list. |
| | 276 | | /// </param> |
| | 277 | | /// <param name='returnRecognitionModel'> |
| | 278 | | /// A value indicating whether the operation should return 'recognitionModel' |
| | 279 | | /// in response. |
| | 280 | | /// </param> |
| | 281 | | /// <param name='customHeaders'> |
| | 282 | | /// Headers that will be added to request. |
| | 283 | | /// </param> |
| | 284 | | /// <param name='cancellationToken'> |
| | 285 | | /// The cancellation token. |
| | 286 | | /// </param> |
| | 287 | | /// <exception cref="APIErrorException"> |
| | 288 | | /// Thrown when the operation returned an invalid status code |
| | 289 | | /// </exception> |
| | 290 | | /// <exception cref="SerializationException"> |
| | 291 | | /// Thrown when unable to deserialize the response |
| | 292 | | /// </exception> |
| | 293 | | /// <exception cref="ValidationException"> |
| | 294 | | /// Thrown when a required parameter is null |
| | 295 | | /// </exception> |
| | 296 | | /// <exception cref="System.ArgumentNullException"> |
| | 297 | | /// Thrown when a required parameter is null |
| | 298 | | /// </exception> |
| | 299 | | /// <return> |
| | 300 | | /// A response object containing the response body and response headers. |
| | 301 | | /// </return> |
| | 302 | | public async Task<HttpOperationResponse<FaceList>> GetWithHttpMessagesAsync(string faceListId, bool? returnRecog |
| | 303 | | { |
| 2 | 304 | | if (Client.Endpoint == null) |
| | 305 | | { |
| 0 | 306 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 307 | | } |
| 2 | 308 | | if (faceListId == null) |
| | 309 | | { |
| 0 | 310 | | throw new ValidationException(ValidationRules.CannotBeNull, "faceListId"); |
| | 311 | | } |
| 2 | 312 | | if (faceListId != null) |
| | 313 | | { |
| 2 | 314 | | if (faceListId.Length > 64) |
| | 315 | | { |
| 0 | 316 | | throw new ValidationException(ValidationRules.MaxLength, "faceListId", 64); |
| | 317 | | } |
| 2 | 318 | | if (!System.Text.RegularExpressions.Regex.IsMatch(faceListId, "^[a-z0-9-_]+$")) |
| | 319 | | { |
| 0 | 320 | | throw new ValidationException(ValidationRules.Pattern, "faceListId", "^[a-z0-9-_]+$"); |
| | 321 | | } |
| | 322 | | } |
| | 323 | | // Tracing |
| 2 | 324 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 325 | | string _invocationId = null; |
| 2 | 326 | | if (_shouldTrace) |
| | 327 | | { |
| 0 | 328 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 329 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 330 | | tracingParameters.Add("faceListId", faceListId); |
| 0 | 331 | | tracingParameters.Add("returnRecognitionModel", returnRecognitionModel); |
| 0 | 332 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 333 | | ServiceClientTracing.Enter(_invocationId, this, "Get", tracingParameters); |
| | 334 | | } |
| | 335 | | // Construct URL |
| 2 | 336 | | var _baseUrl = Client.BaseUri; |
| 2 | 337 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "facelists/{faceListId}"; |
| 2 | 338 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 2 | 339 | | _url = _url.Replace("{faceListId}", System.Uri.EscapeDataString(faceListId)); |
| 2 | 340 | | List<string> _queryParameters = new List<string>(); |
| 2 | 341 | | if (returnRecognitionModel != null) |
| | 342 | | { |
| 2 | 343 | | _queryParameters.Add(string.Format("returnRecognitionModel={0}", System.Uri.EscapeDataString(Rest.Serial |
| | 344 | | } |
| 2 | 345 | | if (_queryParameters.Count > 0) |
| | 346 | | { |
| 2 | 347 | | _url += "?" + string.Join("&", _queryParameters); |
| | 348 | | } |
| | 349 | | // Create HTTP transport objects |
| 2 | 350 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 351 | | HttpResponseMessage _httpResponse = null; |
| 2 | 352 | | _httpRequest.Method = new HttpMethod("GET"); |
| 2 | 353 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 354 | | // Set Headers |
| | 355 | |
|
| | 356 | |
|
| 2 | 357 | | if (customHeaders != null) |
| | 358 | | { |
| 0 | 359 | | foreach(var _header in customHeaders) |
| | 360 | | { |
| 0 | 361 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 362 | | { |
| 0 | 363 | | _httpRequest.Headers.Remove(_header.Key); |
| | 364 | | } |
| 0 | 365 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 366 | | } |
| | 367 | | } |
| | 368 | |
|
| | 369 | | // Serialize Request |
| 2 | 370 | | string _requestContent = null; |
| | 371 | | // Set Credentials |
| 2 | 372 | | if (Client.Credentials != null) |
| | 373 | | { |
| 2 | 374 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 375 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 376 | | } |
| | 377 | | // Send Request |
| 2 | 378 | | if (_shouldTrace) |
| | 379 | | { |
| 0 | 380 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 381 | | } |
| 2 | 382 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 383 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 384 | | if (_shouldTrace) |
| | 385 | | { |
| 0 | 386 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 387 | | } |
| 2 | 388 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 389 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 390 | | string _responseContent = null; |
| 2 | 391 | | if ((int)_statusCode != 200) |
| | 392 | | { |
| 0 | 393 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | 394 | | try |
| | 395 | | { |
| 0 | 396 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 397 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| 0 | 398 | | if (_errorBody != null) |
| | 399 | | { |
| 0 | 400 | | ex.Body = _errorBody; |
| | 401 | | } |
| 0 | 402 | | } |
| 0 | 403 | | catch (JsonException) |
| | 404 | | { |
| | 405 | | // Ignore the exception |
| 0 | 406 | | } |
| 0 | 407 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 408 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 409 | | if (_shouldTrace) |
| | 410 | | { |
| 0 | 411 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 412 | | } |
| 0 | 413 | | _httpRequest.Dispose(); |
| 0 | 414 | | if (_httpResponse != null) |
| | 415 | | { |
| 0 | 416 | | _httpResponse.Dispose(); |
| | 417 | | } |
| 0 | 418 | | throw ex; |
| | 419 | | } |
| | 420 | | // Create Result |
| 2 | 421 | | var _result = new HttpOperationResponse<FaceList>(); |
| 2 | 422 | | _result.Request = _httpRequest; |
| 2 | 423 | | _result.Response = _httpResponse; |
| | 424 | | // Deserialize Response |
| 2 | 425 | | if ((int)_statusCode == 200) |
| | 426 | | { |
| 2 | 427 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 428 | | try |
| | 429 | | { |
| 2 | 430 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<FaceList>(_responseContent, Clie |
| 2 | 431 | | } |
| 0 | 432 | | catch (JsonException ex) |
| | 433 | | { |
| 0 | 434 | | _httpRequest.Dispose(); |
| 0 | 435 | | if (_httpResponse != null) |
| | 436 | | { |
| 0 | 437 | | _httpResponse.Dispose(); |
| | 438 | | } |
| 0 | 439 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 440 | | } |
| | 441 | | } |
| 2 | 442 | | if (_shouldTrace) |
| | 443 | | { |
| 0 | 444 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 445 | | } |
| 2 | 446 | | return _result; |
| 2 | 447 | | } |
| | 448 | |
|
| | 449 | | /// <summary> |
| | 450 | | /// Update information of a face list. |
| | 451 | | /// </summary> |
| | 452 | | /// <param name='faceListId'> |
| | 453 | | /// Id referencing a particular face list. |
| | 454 | | /// </param> |
| | 455 | | /// <param name='name'> |
| | 456 | | /// User defined name, maximum length is 128. |
| | 457 | | /// </param> |
| | 458 | | /// <param name='userData'> |
| | 459 | | /// User specified data. Length should not exceed 16KB. |
| | 460 | | /// </param> |
| | 461 | | /// <param name='customHeaders'> |
| | 462 | | /// Headers that will be added to request. |
| | 463 | | /// </param> |
| | 464 | | /// <param name='cancellationToken'> |
| | 465 | | /// The cancellation token. |
| | 466 | | /// </param> |
| | 467 | | /// <exception cref="APIErrorException"> |
| | 468 | | /// Thrown when the operation returned an invalid status code |
| | 469 | | /// </exception> |
| | 470 | | /// <exception cref="ValidationException"> |
| | 471 | | /// Thrown when a required parameter is null |
| | 472 | | /// </exception> |
| | 473 | | /// <exception cref="System.ArgumentNullException"> |
| | 474 | | /// Thrown when a required parameter is null |
| | 475 | | /// </exception> |
| | 476 | | /// <return> |
| | 477 | | /// A response object containing the response body and response headers. |
| | 478 | | /// </return> |
| | 479 | | public async Task<HttpOperationResponse> UpdateWithHttpMessagesAsync(string faceListId, string name = default(st |
| | 480 | | { |
| 0 | 481 | | if (Client.Endpoint == null) |
| | 482 | | { |
| 0 | 483 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 484 | | } |
| 0 | 485 | | if (faceListId == null) |
| | 486 | | { |
| 0 | 487 | | throw new ValidationException(ValidationRules.CannotBeNull, "faceListId"); |
| | 488 | | } |
| 0 | 489 | | if (faceListId != null) |
| | 490 | | { |
| 0 | 491 | | if (faceListId.Length > 64) |
| | 492 | | { |
| 0 | 493 | | throw new ValidationException(ValidationRules.MaxLength, "faceListId", 64); |
| | 494 | | } |
| 0 | 495 | | if (!System.Text.RegularExpressions.Regex.IsMatch(faceListId, "^[a-z0-9-_]+$")) |
| | 496 | | { |
| 0 | 497 | | throw new ValidationException(ValidationRules.Pattern, "faceListId", "^[a-z0-9-_]+$"); |
| | 498 | | } |
| | 499 | | } |
| 0 | 500 | | if (name != null) |
| | 501 | | { |
| 0 | 502 | | if (name.Length > 128) |
| | 503 | | { |
| 0 | 504 | | throw new ValidationException(ValidationRules.MaxLength, "name", 128); |
| | 505 | | } |
| | 506 | | } |
| 0 | 507 | | if (userData != null) |
| | 508 | | { |
| 0 | 509 | | if (userData.Length > 16384) |
| | 510 | | { |
| 0 | 511 | | throw new ValidationException(ValidationRules.MaxLength, "userData", 16384); |
| | 512 | | } |
| | 513 | | } |
| 0 | 514 | | NameAndUserDataContract body = new NameAndUserDataContract(); |
| 0 | 515 | | if (name != null || userData != null) |
| | 516 | | { |
| 0 | 517 | | body.Name = name; |
| 0 | 518 | | body.UserData = userData; |
| | 519 | | } |
| | 520 | | // Tracing |
| 0 | 521 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 522 | | string _invocationId = null; |
| 0 | 523 | | if (_shouldTrace) |
| | 524 | | { |
| 0 | 525 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 526 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 527 | | tracingParameters.Add("faceListId", faceListId); |
| 0 | 528 | | tracingParameters.Add("body", body); |
| 0 | 529 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 530 | | ServiceClientTracing.Enter(_invocationId, this, "Update", tracingParameters); |
| | 531 | | } |
| | 532 | | // Construct URL |
| 0 | 533 | | var _baseUrl = Client.BaseUri; |
| 0 | 534 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "facelists/{faceListId}"; |
| 0 | 535 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 0 | 536 | | _url = _url.Replace("{faceListId}", System.Uri.EscapeDataString(faceListId)); |
| | 537 | | // Create HTTP transport objects |
| 0 | 538 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 539 | | HttpResponseMessage _httpResponse = null; |
| 0 | 540 | | _httpRequest.Method = new HttpMethod("PATCH"); |
| 0 | 541 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 542 | | // Set Headers |
| | 543 | |
|
| | 544 | |
|
| 0 | 545 | | if (customHeaders != null) |
| | 546 | | { |
| 0 | 547 | | foreach(var _header in customHeaders) |
| | 548 | | { |
| 0 | 549 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 550 | | { |
| 0 | 551 | | _httpRequest.Headers.Remove(_header.Key); |
| | 552 | | } |
| 0 | 553 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 554 | | } |
| | 555 | | } |
| | 556 | |
|
| | 557 | | // Serialize Request |
| 0 | 558 | | string _requestContent = null; |
| 0 | 559 | | if(body != null) |
| | 560 | | { |
| 0 | 561 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(body, Client.SerializationSettings) |
| 0 | 562 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 0 | 563 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 564 | | } |
| | 565 | | // Set Credentials |
| 0 | 566 | | if (Client.Credentials != null) |
| | 567 | | { |
| 0 | 568 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 569 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 570 | | } |
| | 571 | | // Send Request |
| 0 | 572 | | if (_shouldTrace) |
| | 573 | | { |
| 0 | 574 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 575 | | } |
| 0 | 576 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 577 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 578 | | if (_shouldTrace) |
| | 579 | | { |
| 0 | 580 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 581 | | } |
| 0 | 582 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 583 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 584 | | string _responseContent = null; |
| 0 | 585 | | if ((int)_statusCode != 200) |
| | 586 | | { |
| 0 | 587 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | 588 | | try |
| | 589 | | { |
| 0 | 590 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 591 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| 0 | 592 | | if (_errorBody != null) |
| | 593 | | { |
| 0 | 594 | | ex.Body = _errorBody; |
| | 595 | | } |
| 0 | 596 | | } |
| 0 | 597 | | catch (JsonException) |
| | 598 | | { |
| | 599 | | // Ignore the exception |
| 0 | 600 | | } |
| 0 | 601 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 602 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 603 | | if (_shouldTrace) |
| | 604 | | { |
| 0 | 605 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 606 | | } |
| 0 | 607 | | _httpRequest.Dispose(); |
| 0 | 608 | | if (_httpResponse != null) |
| | 609 | | { |
| 0 | 610 | | _httpResponse.Dispose(); |
| | 611 | | } |
| 0 | 612 | | throw ex; |
| | 613 | | } |
| | 614 | | // Create Result |
| 0 | 615 | | var _result = new HttpOperationResponse(); |
| 0 | 616 | | _result.Request = _httpRequest; |
| 0 | 617 | | _result.Response = _httpResponse; |
| 0 | 618 | | if (_shouldTrace) |
| | 619 | | { |
| 0 | 620 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 621 | | } |
| 0 | 622 | | return _result; |
| 0 | 623 | | } |
| | 624 | |
|
| | 625 | | /// <summary> |
| | 626 | | /// Delete a specified face list. |
| | 627 | | /// </summary> |
| | 628 | | /// <param name='faceListId'> |
| | 629 | | /// Id referencing a particular face list. |
| | 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="APIErrorException"> |
| | 638 | | /// Thrown when the operation returned an invalid status code |
| | 639 | | /// </exception> |
| | 640 | | /// <exception cref="ValidationException"> |
| | 641 | | /// Thrown when a required parameter is null |
| | 642 | | /// </exception> |
| | 643 | | /// <exception cref="System.ArgumentNullException"> |
| | 644 | | /// Thrown when a required parameter is null |
| | 645 | | /// </exception> |
| | 646 | | /// <return> |
| | 647 | | /// A response object containing the response body and response headers. |
| | 648 | | /// </return> |
| | 649 | | public async Task<HttpOperationResponse> DeleteWithHttpMessagesAsync(string faceListId, Dictionary<string, List< |
| | 650 | | { |
| 6 | 651 | | if (Client.Endpoint == null) |
| | 652 | | { |
| 0 | 653 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 654 | | } |
| 6 | 655 | | if (faceListId == null) |
| | 656 | | { |
| 0 | 657 | | throw new ValidationException(ValidationRules.CannotBeNull, "faceListId"); |
| | 658 | | } |
| 6 | 659 | | if (faceListId != null) |
| | 660 | | { |
| 6 | 661 | | if (faceListId.Length > 64) |
| | 662 | | { |
| 0 | 663 | | throw new ValidationException(ValidationRules.MaxLength, "faceListId", 64); |
| | 664 | | } |
| 6 | 665 | | if (!System.Text.RegularExpressions.Regex.IsMatch(faceListId, "^[a-z0-9-_]+$")) |
| | 666 | | { |
| 0 | 667 | | throw new ValidationException(ValidationRules.Pattern, "faceListId", "^[a-z0-9-_]+$"); |
| | 668 | | } |
| | 669 | | } |
| | 670 | | // Tracing |
| 6 | 671 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 6 | 672 | | string _invocationId = null; |
| 6 | 673 | | if (_shouldTrace) |
| | 674 | | { |
| 0 | 675 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 676 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 677 | | tracingParameters.Add("faceListId", faceListId); |
| 0 | 678 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 679 | | ServiceClientTracing.Enter(_invocationId, this, "Delete", tracingParameters); |
| | 680 | | } |
| | 681 | | // Construct URL |
| 6 | 682 | | var _baseUrl = Client.BaseUri; |
| 6 | 683 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "facelists/{faceListId}"; |
| 6 | 684 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 6 | 685 | | _url = _url.Replace("{faceListId}", System.Uri.EscapeDataString(faceListId)); |
| | 686 | | // Create HTTP transport objects |
| 6 | 687 | | var _httpRequest = new HttpRequestMessage(); |
| 6 | 688 | | HttpResponseMessage _httpResponse = null; |
| 6 | 689 | | _httpRequest.Method = new HttpMethod("DELETE"); |
| 6 | 690 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 691 | | // Set Headers |
| | 692 | |
|
| | 693 | |
|
| 6 | 694 | | if (customHeaders != null) |
| | 695 | | { |
| 0 | 696 | | foreach(var _header in customHeaders) |
| | 697 | | { |
| 0 | 698 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 699 | | { |
| 0 | 700 | | _httpRequest.Headers.Remove(_header.Key); |
| | 701 | | } |
| 0 | 702 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 703 | | } |
| | 704 | | } |
| | 705 | |
|
| | 706 | | // Serialize Request |
| 6 | 707 | | string _requestContent = null; |
| | 708 | | // Set Credentials |
| 6 | 709 | | if (Client.Credentials != null) |
| | 710 | | { |
| 6 | 711 | | cancellationToken.ThrowIfCancellationRequested(); |
| 6 | 712 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 713 | | } |
| | 714 | | // Send Request |
| 6 | 715 | | if (_shouldTrace) |
| | 716 | | { |
| 0 | 717 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 718 | | } |
| 6 | 719 | | cancellationToken.ThrowIfCancellationRequested(); |
| 6 | 720 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 6 | 721 | | if (_shouldTrace) |
| | 722 | | { |
| 0 | 723 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 724 | | } |
| 6 | 725 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 6 | 726 | | cancellationToken.ThrowIfCancellationRequested(); |
| 6 | 727 | | string _responseContent = null; |
| 6 | 728 | | if ((int)_statusCode != 200) |
| | 729 | | { |
| 0 | 730 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | 731 | | try |
| | 732 | | { |
| 0 | 733 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 734 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| 0 | 735 | | if (_errorBody != null) |
| | 736 | | { |
| 0 | 737 | | ex.Body = _errorBody; |
| | 738 | | } |
| 0 | 739 | | } |
| 0 | 740 | | catch (JsonException) |
| | 741 | | { |
| | 742 | | // Ignore the exception |
| 0 | 743 | | } |
| 0 | 744 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 745 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 746 | | if (_shouldTrace) |
| | 747 | | { |
| 0 | 748 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 749 | | } |
| 0 | 750 | | _httpRequest.Dispose(); |
| 0 | 751 | | if (_httpResponse != null) |
| | 752 | | { |
| 0 | 753 | | _httpResponse.Dispose(); |
| | 754 | | } |
| 0 | 755 | | throw ex; |
| | 756 | | } |
| | 757 | | // Create Result |
| 6 | 758 | | var _result = new HttpOperationResponse(); |
| 6 | 759 | | _result.Request = _httpRequest; |
| 6 | 760 | | _result.Response = _httpResponse; |
| 6 | 761 | | if (_shouldTrace) |
| | 762 | | { |
| 0 | 763 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 764 | | } |
| 6 | 765 | | return _result; |
| 6 | 766 | | } |
| | 767 | |
|
| | 768 | | /// <summary> |
| | 769 | | /// List face lists’ faceListId, name, userData and recognitionModel. <br |
| | 770 | | /// /> |
| | 771 | | /// To get face information inside faceList use [FaceList - |
| | 772 | | /// Get](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524c) |
| | 773 | | /// |
| | 774 | | /// </summary> |
| | 775 | | /// <param name='returnRecognitionModel'> |
| | 776 | | /// A value indicating whether the operation should return 'recognitionModel' |
| | 777 | | /// in response. |
| | 778 | | /// </param> |
| | 779 | | /// <param name='customHeaders'> |
| | 780 | | /// Headers that will be added to request. |
| | 781 | | /// </param> |
| | 782 | | /// <param name='cancellationToken'> |
| | 783 | | /// The cancellation token. |
| | 784 | | /// </param> |
| | 785 | | /// <exception cref="APIErrorException"> |
| | 786 | | /// Thrown when the operation returned an invalid status code |
| | 787 | | /// </exception> |
| | 788 | | /// <exception cref="SerializationException"> |
| | 789 | | /// Thrown when unable to deserialize the response |
| | 790 | | /// </exception> |
| | 791 | | /// <exception cref="ValidationException"> |
| | 792 | | /// Thrown when a required parameter is null |
| | 793 | | /// </exception> |
| | 794 | | /// <exception cref="System.ArgumentNullException"> |
| | 795 | | /// Thrown when a required parameter is null |
| | 796 | | /// </exception> |
| | 797 | | /// <return> |
| | 798 | | /// A response object containing the response body and response headers. |
| | 799 | | /// </return> |
| | 800 | | public async Task<HttpOperationResponse<IList<FaceList>>> ListWithHttpMessagesAsync(bool? returnRecognitionModel |
| | 801 | | { |
| 0 | 802 | | if (Client.Endpoint == null) |
| | 803 | | { |
| 0 | 804 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 805 | | } |
| | 806 | | // Tracing |
| 0 | 807 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 808 | | string _invocationId = null; |
| 0 | 809 | | if (_shouldTrace) |
| | 810 | | { |
| 0 | 811 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 812 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 813 | | tracingParameters.Add("returnRecognitionModel", returnRecognitionModel); |
| 0 | 814 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 815 | | ServiceClientTracing.Enter(_invocationId, this, "List", tracingParameters); |
| | 816 | | } |
| | 817 | | // Construct URL |
| 0 | 818 | | var _baseUrl = Client.BaseUri; |
| 0 | 819 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "facelists"; |
| 0 | 820 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 0 | 821 | | List<string> _queryParameters = new List<string>(); |
| 0 | 822 | | if (returnRecognitionModel != null) |
| | 823 | | { |
| 0 | 824 | | _queryParameters.Add(string.Format("returnRecognitionModel={0}", System.Uri.EscapeDataString(Rest.Serial |
| | 825 | | } |
| 0 | 826 | | if (_queryParameters.Count > 0) |
| | 827 | | { |
| 0 | 828 | | _url += "?" + string.Join("&", _queryParameters); |
| | 829 | | } |
| | 830 | | // Create HTTP transport objects |
| 0 | 831 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 832 | | HttpResponseMessage _httpResponse = null; |
| 0 | 833 | | _httpRequest.Method = new HttpMethod("GET"); |
| 0 | 834 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 835 | | // Set Headers |
| | 836 | |
|
| | 837 | |
|
| 0 | 838 | | if (customHeaders != null) |
| | 839 | | { |
| 0 | 840 | | foreach(var _header in customHeaders) |
| | 841 | | { |
| 0 | 842 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 843 | | { |
| 0 | 844 | | _httpRequest.Headers.Remove(_header.Key); |
| | 845 | | } |
| 0 | 846 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 847 | | } |
| | 848 | | } |
| | 849 | |
|
| | 850 | | // Serialize Request |
| 0 | 851 | | string _requestContent = null; |
| | 852 | | // Set Credentials |
| 0 | 853 | | if (Client.Credentials != null) |
| | 854 | | { |
| 0 | 855 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 856 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 857 | | } |
| | 858 | | // Send Request |
| 0 | 859 | | if (_shouldTrace) |
| | 860 | | { |
| 0 | 861 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 862 | | } |
| 0 | 863 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 864 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 865 | | if (_shouldTrace) |
| | 866 | | { |
| 0 | 867 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 868 | | } |
| 0 | 869 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 870 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 871 | | string _responseContent = null; |
| 0 | 872 | | if ((int)_statusCode != 200) |
| | 873 | | { |
| 0 | 874 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | 875 | | try |
| | 876 | | { |
| 0 | 877 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 878 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| 0 | 879 | | if (_errorBody != null) |
| | 880 | | { |
| 0 | 881 | | ex.Body = _errorBody; |
| | 882 | | } |
| 0 | 883 | | } |
| 0 | 884 | | catch (JsonException) |
| | 885 | | { |
| | 886 | | // Ignore the exception |
| 0 | 887 | | } |
| 0 | 888 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 889 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 890 | | if (_shouldTrace) |
| | 891 | | { |
| 0 | 892 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 893 | | } |
| 0 | 894 | | _httpRequest.Dispose(); |
| 0 | 895 | | if (_httpResponse != null) |
| | 896 | | { |
| 0 | 897 | | _httpResponse.Dispose(); |
| | 898 | | } |
| 0 | 899 | | throw ex; |
| | 900 | | } |
| | 901 | | // Create Result |
| 0 | 902 | | var _result = new HttpOperationResponse<IList<FaceList>>(); |
| 0 | 903 | | _result.Request = _httpRequest; |
| 0 | 904 | | _result.Response = _httpResponse; |
| | 905 | | // Deserialize Response |
| 0 | 906 | | if ((int)_statusCode == 200) |
| | 907 | | { |
| 0 | 908 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 909 | | try |
| | 910 | | { |
| 0 | 911 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<IList<FaceList>>(_responseConten |
| 0 | 912 | | } |
| 0 | 913 | | catch (JsonException ex) |
| | 914 | | { |
| 0 | 915 | | _httpRequest.Dispose(); |
| 0 | 916 | | if (_httpResponse != null) |
| | 917 | | { |
| 0 | 918 | | _httpResponse.Dispose(); |
| | 919 | | } |
| 0 | 920 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 921 | | } |
| | 922 | | } |
| 0 | 923 | | if (_shouldTrace) |
| | 924 | | { |
| 0 | 925 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 926 | | } |
| 0 | 927 | | return _result; |
| 0 | 928 | | } |
| | 929 | |
|
| | 930 | | /// <summary> |
| | 931 | | /// Delete a face from a face list by specified faceListId and persistedFaceId. |
| | 932 | | /// <br /> Adding/deleting faces to/from a same face list are processed |
| | 933 | | /// sequentially and to/from different face lists are in parallel. |
| | 934 | | /// </summary> |
| | 935 | | /// <param name='faceListId'> |
| | 936 | | /// Id referencing a particular face list. |
| | 937 | | /// </param> |
| | 938 | | /// <param name='persistedFaceId'> |
| | 939 | | /// Id referencing a particular persistedFaceId of an existing face. |
| | 940 | | /// </param> |
| | 941 | | /// <param name='customHeaders'> |
| | 942 | | /// Headers that will be added to request. |
| | 943 | | /// </param> |
| | 944 | | /// <param name='cancellationToken'> |
| | 945 | | /// The cancellation token. |
| | 946 | | /// </param> |
| | 947 | | /// <exception cref="APIErrorException"> |
| | 948 | | /// Thrown when the operation returned an invalid status code |
| | 949 | | /// </exception> |
| | 950 | | /// <exception cref="ValidationException"> |
| | 951 | | /// Thrown when a required parameter is null |
| | 952 | | /// </exception> |
| | 953 | | /// <exception cref="System.ArgumentNullException"> |
| | 954 | | /// Thrown when a required parameter is null |
| | 955 | | /// </exception> |
| | 956 | | /// <return> |
| | 957 | | /// A response object containing the response body and response headers. |
| | 958 | | /// </return> |
| | 959 | | public async Task<HttpOperationResponse> DeleteFaceWithHttpMessagesAsync(string faceListId, System.Guid persiste |
| | 960 | | { |
| 0 | 961 | | if (Client.Endpoint == null) |
| | 962 | | { |
| 0 | 963 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 964 | | } |
| 0 | 965 | | if (faceListId == null) |
| | 966 | | { |
| 0 | 967 | | throw new ValidationException(ValidationRules.CannotBeNull, "faceListId"); |
| | 968 | | } |
| 0 | 969 | | if (faceListId != null) |
| | 970 | | { |
| 0 | 971 | | if (faceListId.Length > 64) |
| | 972 | | { |
| 0 | 973 | | throw new ValidationException(ValidationRules.MaxLength, "faceListId", 64); |
| | 974 | | } |
| 0 | 975 | | if (!System.Text.RegularExpressions.Regex.IsMatch(faceListId, "^[a-z0-9-_]+$")) |
| | 976 | | { |
| 0 | 977 | | throw new ValidationException(ValidationRules.Pattern, "faceListId", "^[a-z0-9-_]+$"); |
| | 978 | | } |
| | 979 | | } |
| | 980 | | // Tracing |
| 0 | 981 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 982 | | string _invocationId = null; |
| 0 | 983 | | if (_shouldTrace) |
| | 984 | | { |
| 0 | 985 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 986 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 987 | | tracingParameters.Add("faceListId", faceListId); |
| 0 | 988 | | tracingParameters.Add("persistedFaceId", persistedFaceId); |
| 0 | 989 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 990 | | ServiceClientTracing.Enter(_invocationId, this, "DeleteFace", tracingParameters); |
| | 991 | | } |
| | 992 | | // Construct URL |
| 0 | 993 | | var _baseUrl = Client.BaseUri; |
| 0 | 994 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "facelists/{faceListId}/persistedfaces/{persiste |
| 0 | 995 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 0 | 996 | | _url = _url.Replace("{faceListId}", System.Uri.EscapeDataString(faceListId)); |
| 0 | 997 | | _url = _url.Replace("{persistedFaceId}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonConvert.Seri |
| | 998 | | // Create HTTP transport objects |
| 0 | 999 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 1000 | | HttpResponseMessage _httpResponse = null; |
| 0 | 1001 | | _httpRequest.Method = new HttpMethod("DELETE"); |
| 0 | 1002 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1003 | | // Set Headers |
| | 1004 | |
|
| | 1005 | |
|
| 0 | 1006 | | if (customHeaders != null) |
| | 1007 | | { |
| 0 | 1008 | | foreach(var _header in customHeaders) |
| | 1009 | | { |
| 0 | 1010 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1011 | | { |
| 0 | 1012 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1013 | | } |
| 0 | 1014 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1015 | | } |
| | 1016 | | } |
| | 1017 | |
|
| | 1018 | | // Serialize Request |
| 0 | 1019 | | string _requestContent = null; |
| | 1020 | | // Set Credentials |
| 0 | 1021 | | if (Client.Credentials != null) |
| | 1022 | | { |
| 0 | 1023 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1024 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1025 | | } |
| | 1026 | | // Send Request |
| 0 | 1027 | | if (_shouldTrace) |
| | 1028 | | { |
| 0 | 1029 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1030 | | } |
| 0 | 1031 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1032 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 1033 | | if (_shouldTrace) |
| | 1034 | | { |
| 0 | 1035 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1036 | | } |
| 0 | 1037 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 1038 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1039 | | string _responseContent = null; |
| 0 | 1040 | | if ((int)_statusCode != 200) |
| | 1041 | | { |
| 0 | 1042 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | 1043 | | try |
| | 1044 | | { |
| 0 | 1045 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1046 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| 0 | 1047 | | if (_errorBody != null) |
| | 1048 | | { |
| 0 | 1049 | | ex.Body = _errorBody; |
| | 1050 | | } |
| 0 | 1051 | | } |
| 0 | 1052 | | catch (JsonException) |
| | 1053 | | { |
| | 1054 | | // Ignore the exception |
| 0 | 1055 | | } |
| 0 | 1056 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1057 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1058 | | if (_shouldTrace) |
| | 1059 | | { |
| 0 | 1060 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1061 | | } |
| 0 | 1062 | | _httpRequest.Dispose(); |
| 0 | 1063 | | if (_httpResponse != null) |
| | 1064 | | { |
| 0 | 1065 | | _httpResponse.Dispose(); |
| | 1066 | | } |
| 0 | 1067 | | throw ex; |
| | 1068 | | } |
| | 1069 | | // Create Result |
| 0 | 1070 | | var _result = new HttpOperationResponse(); |
| 0 | 1071 | | _result.Request = _httpRequest; |
| 0 | 1072 | | _result.Response = _httpResponse; |
| 0 | 1073 | | if (_shouldTrace) |
| | 1074 | | { |
| 0 | 1075 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1076 | | } |
| 0 | 1077 | | return _result; |
| 0 | 1078 | | } |
| | 1079 | |
|
| | 1080 | | /// <summary> |
| | 1081 | | /// Add a face to a specified face list, up to 1,000 faces. |
| | 1082 | | /// <br /> To deal with an image contains multiple faces, input face can |
| | 1083 | | /// be specified as an image with a targetFace rectangle. It returns a |
| | 1084 | | /// persistedFaceId representing the added face. No image will be stored. Only |
| | 1085 | | /// the extracted face feature will be stored on server until [FaceList - |
| | 1086 | | /// Delete |
| | 1087 | | /// Face](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395251) |
| | 1088 | | /// or [FaceList - |
| | 1089 | | /// Delete](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524f) |
| | 1090 | | /// is called. |
| | 1091 | | /// <br /> Note persistedFaceId is different from faceId generated by |
| | 1092 | | /// [Face - |
| | 1093 | | /// Detect](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236). |
| | 1094 | | /// * Higher face image quality means better detection and recognition |
| | 1095 | | /// precision. Please consider high-quality faces: frontal, clear, and face |
| | 1096 | | /// size is 200x200 pixels (100 pixels between eyes) or bigger. |
| | 1097 | | /// * JPEG, PNG, GIF (the first frame), and BMP format are supported. The |
| | 1098 | | /// allowed image file size is from 1KB to 6MB. |
| | 1099 | | /// * "targetFace" rectangle should contain one face. Zero or multiple faces |
| | 1100 | | /// will be regarded as an error. If the provided "targetFace" rectangle is not |
| | 1101 | | /// returned from [Face - |
| | 1102 | | /// Detect](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236), |
| | 1103 | | /// there’s no guarantee to detect and add the face successfully. |
| | 1104 | | /// * Out of detectable face size (36x36 - 4096x4096 pixels), large head-pose, |
| | 1105 | | /// or large occlusions will cause failures. |
| | 1106 | | /// * Adding/deleting faces to/from a same face list are processed sequentially |
| | 1107 | | /// and to/from different face lists are in parallel. |
| | 1108 | | /// * The minimum detectable face size is 36x36 pixels in an image no larger |
| | 1109 | | /// than 1920x1080 pixels. Images with dimensions higher than 1920x1080 pixels |
| | 1110 | | /// will need a proportionally larger minimum face size. |
| | 1111 | | /// * Different 'detectionModel' values can be provided. To use and compare |
| | 1112 | | /// different detection models, please refer to [How to specify a detection |
| | 1113 | | /// model](https://docs.microsoft.com/en-us/azure/cognitive-services/face/face-api-how-to-topics/specify-detecti |
| | 1114 | | /// | Model | Recommended use-case(s) | |
| | 1115 | | /// | ---------- | -------- | |
| | 1116 | | /// | 'detection_01': | The default detection model for [FaceList - Add |
| | 1117 | | /// Face](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395250). |
| | 1118 | | /// Recommend for near frontal face detection. For scenarios with exceptionally |
| | 1119 | | /// large angle (head-pose) faces, occluded faces or wrong image orientation, |
| | 1120 | | /// the faces in such cases may not be detected. | |
| | 1121 | | /// | 'detection_02': | Detection model released in 2019 May with improved |
| | 1122 | | /// accuracy especially on small, side and blurry faces. | |
| | 1123 | | /// </summary> |
| | 1124 | | /// <param name='faceListId'> |
| | 1125 | | /// Id referencing a particular face list. |
| | 1126 | | /// </param> |
| | 1127 | | /// <param name='url'> |
| | 1128 | | /// Publicly reachable URL of an image |
| | 1129 | | /// </param> |
| | 1130 | | /// <param name='userData'> |
| | 1131 | | /// User-specified data about the face for any purpose. The maximum length is |
| | 1132 | | /// 1KB. |
| | 1133 | | /// </param> |
| | 1134 | | /// <param name='targetFace'> |
| | 1135 | | /// A face rectangle to specify the target face to be added to a person in the |
| | 1136 | | /// format of "targetFace=left,top,width,height". E.g. |
| | 1137 | | /// "targetFace=10,10,100,100". If there is more than one face in the image, |
| | 1138 | | /// targetFace is required to specify which face to add. No targetFace means |
| | 1139 | | /// there is only one face detected in the entire image. |
| | 1140 | | /// </param> |
| | 1141 | | /// <param name='detectionModel'> |
| | 1142 | | /// Name of detection model. Detection model is used to detect faces in the |
| | 1143 | | /// submitted image. A detection model name can be provided when performing |
| | 1144 | | /// Face - Detect or (Large)FaceList - Add Face or (Large)PersonGroup - Add |
| | 1145 | | /// Face. The default value is 'detection_01', if another model is needed, |
| | 1146 | | /// please explicitly specify it. Possible values include: 'detection_01', |
| | 1147 | | /// 'detection_02' |
| | 1148 | | /// </param> |
| | 1149 | | /// <param name='customHeaders'> |
| | 1150 | | /// Headers that will be added to request. |
| | 1151 | | /// </param> |
| | 1152 | | /// <param name='cancellationToken'> |
| | 1153 | | /// The cancellation token. |
| | 1154 | | /// </param> |
| | 1155 | | /// <exception cref="APIErrorException"> |
| | 1156 | | /// Thrown when the operation returned an invalid status code |
| | 1157 | | /// </exception> |
| | 1158 | | /// <exception cref="SerializationException"> |
| | 1159 | | /// Thrown when unable to deserialize the response |
| | 1160 | | /// </exception> |
| | 1161 | | /// <exception cref="ValidationException"> |
| | 1162 | | /// Thrown when a required parameter is null |
| | 1163 | | /// </exception> |
| | 1164 | | /// <exception cref="System.ArgumentNullException"> |
| | 1165 | | /// Thrown when a required parameter is null |
| | 1166 | | /// </exception> |
| | 1167 | | /// <return> |
| | 1168 | | /// A response object containing the response body and response headers. |
| | 1169 | | /// </return> |
| | 1170 | | public async Task<HttpOperationResponse<PersistedFace>> AddFaceFromUrlWithHttpMessagesAsync(string faceListId, s |
| | 1171 | | { |
| 0 | 1172 | | if (Client.Endpoint == null) |
| | 1173 | | { |
| 0 | 1174 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 1175 | | } |
| 0 | 1176 | | if (faceListId == null) |
| | 1177 | | { |
| 0 | 1178 | | throw new ValidationException(ValidationRules.CannotBeNull, "faceListId"); |
| | 1179 | | } |
| 0 | 1180 | | if (faceListId != null) |
| | 1181 | | { |
| 0 | 1182 | | if (faceListId.Length > 64) |
| | 1183 | | { |
| 0 | 1184 | | throw new ValidationException(ValidationRules.MaxLength, "faceListId", 64); |
| | 1185 | | } |
| 0 | 1186 | | if (!System.Text.RegularExpressions.Regex.IsMatch(faceListId, "^[a-z0-9-_]+$")) |
| | 1187 | | { |
| 0 | 1188 | | throw new ValidationException(ValidationRules.Pattern, "faceListId", "^[a-z0-9-_]+$"); |
| | 1189 | | } |
| | 1190 | | } |
| 0 | 1191 | | if (userData != null) |
| | 1192 | | { |
| 0 | 1193 | | if (userData.Length > 1024) |
| | 1194 | | { |
| 0 | 1195 | | throw new ValidationException(ValidationRules.MaxLength, "userData", 1024); |
| | 1196 | | } |
| | 1197 | | } |
| 0 | 1198 | | if (url == null) |
| | 1199 | | { |
| 0 | 1200 | | throw new ValidationException(ValidationRules.CannotBeNull, "url"); |
| | 1201 | | } |
| 0 | 1202 | | ImageUrl imageUrl = new ImageUrl(); |
| 0 | 1203 | | if (url != null) |
| | 1204 | | { |
| 0 | 1205 | | imageUrl.Url = url; |
| | 1206 | | } |
| | 1207 | | // Tracing |
| 0 | 1208 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 1209 | | string _invocationId = null; |
| 0 | 1210 | | if (_shouldTrace) |
| | 1211 | | { |
| 0 | 1212 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1213 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1214 | | tracingParameters.Add("faceListId", faceListId); |
| 0 | 1215 | | tracingParameters.Add("userData", userData); |
| 0 | 1216 | | tracingParameters.Add("targetFace", targetFace); |
| 0 | 1217 | | tracingParameters.Add("detectionModel", detectionModel); |
| 0 | 1218 | | tracingParameters.Add("imageUrl", imageUrl); |
| 0 | 1219 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1220 | | ServiceClientTracing.Enter(_invocationId, this, "AddFaceFromUrl", tracingParameters); |
| | 1221 | | } |
| | 1222 | | // Construct URL |
| 0 | 1223 | | var _baseUrl = Client.BaseUri; |
| 0 | 1224 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "facelists/{faceListId}/persistedfaces"; |
| 0 | 1225 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 0 | 1226 | | _url = _url.Replace("{faceListId}", System.Uri.EscapeDataString(faceListId)); |
| 0 | 1227 | | List<string> _queryParameters = new List<string>(); |
| 0 | 1228 | | if (userData != null) |
| | 1229 | | { |
| 0 | 1230 | | _queryParameters.Add(string.Format("userData={0}", System.Uri.EscapeDataString(userData))); |
| | 1231 | | } |
| 0 | 1232 | | if (targetFace != null) |
| | 1233 | | { |
| 0 | 1234 | | _queryParameters.Add(string.Format("targetFace={0}", System.Uri.EscapeDataString(string.Join(",", target |
| | 1235 | | } |
| 0 | 1236 | | if (detectionModel != null) |
| | 1237 | | { |
| 0 | 1238 | | _queryParameters.Add(string.Format("detectionModel={0}", System.Uri.EscapeDataString(detectionModel))); |
| | 1239 | | } |
| 0 | 1240 | | if (_queryParameters.Count > 0) |
| | 1241 | | { |
| 0 | 1242 | | _url += "?" + string.Join("&", _queryParameters); |
| | 1243 | | } |
| | 1244 | | // Create HTTP transport objects |
| 0 | 1245 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 1246 | | HttpResponseMessage _httpResponse = null; |
| 0 | 1247 | | _httpRequest.Method = new HttpMethod("POST"); |
| 0 | 1248 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1249 | | // Set Headers |
| | 1250 | |
|
| | 1251 | |
|
| 0 | 1252 | | if (customHeaders != null) |
| | 1253 | | { |
| 0 | 1254 | | foreach(var _header in customHeaders) |
| | 1255 | | { |
| 0 | 1256 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1257 | | { |
| 0 | 1258 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1259 | | } |
| 0 | 1260 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1261 | | } |
| | 1262 | | } |
| | 1263 | |
|
| | 1264 | | // Serialize Request |
| 0 | 1265 | | string _requestContent = null; |
| 0 | 1266 | | if(imageUrl != null) |
| | 1267 | | { |
| 0 | 1268 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(imageUrl, Client.SerializationSetti |
| 0 | 1269 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 0 | 1270 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 1271 | | } |
| | 1272 | | // Set Credentials |
| 0 | 1273 | | if (Client.Credentials != null) |
| | 1274 | | { |
| 0 | 1275 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1276 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1277 | | } |
| | 1278 | | // Send Request |
| 0 | 1279 | | if (_shouldTrace) |
| | 1280 | | { |
| 0 | 1281 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1282 | | } |
| 0 | 1283 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1284 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 1285 | | if (_shouldTrace) |
| | 1286 | | { |
| 0 | 1287 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1288 | | } |
| 0 | 1289 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 1290 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 1291 | | string _responseContent = null; |
| 0 | 1292 | | if ((int)_statusCode != 200) |
| | 1293 | | { |
| 0 | 1294 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | 1295 | | try |
| | 1296 | | { |
| 0 | 1297 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1298 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| 0 | 1299 | | if (_errorBody != null) |
| | 1300 | | { |
| 0 | 1301 | | ex.Body = _errorBody; |
| | 1302 | | } |
| 0 | 1303 | | } |
| 0 | 1304 | | catch (JsonException) |
| | 1305 | | { |
| | 1306 | | // Ignore the exception |
| 0 | 1307 | | } |
| 0 | 1308 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1309 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1310 | | if (_shouldTrace) |
| | 1311 | | { |
| 0 | 1312 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1313 | | } |
| 0 | 1314 | | _httpRequest.Dispose(); |
| 0 | 1315 | | if (_httpResponse != null) |
| | 1316 | | { |
| 0 | 1317 | | _httpResponse.Dispose(); |
| | 1318 | | } |
| 0 | 1319 | | throw ex; |
| | 1320 | | } |
| | 1321 | | // Create Result |
| 0 | 1322 | | var _result = new HttpOperationResponse<PersistedFace>(); |
| 0 | 1323 | | _result.Request = _httpRequest; |
| 0 | 1324 | | _result.Response = _httpResponse; |
| | 1325 | | // Deserialize Response |
| 0 | 1326 | | if ((int)_statusCode == 200) |
| | 1327 | | { |
| 0 | 1328 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1329 | | try |
| | 1330 | | { |
| 0 | 1331 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<PersistedFace>(_responseContent, |
| 0 | 1332 | | } |
| 0 | 1333 | | catch (JsonException ex) |
| | 1334 | | { |
| 0 | 1335 | | _httpRequest.Dispose(); |
| 0 | 1336 | | if (_httpResponse != null) |
| | 1337 | | { |
| 0 | 1338 | | _httpResponse.Dispose(); |
| | 1339 | | } |
| 0 | 1340 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1341 | | } |
| | 1342 | | } |
| 0 | 1343 | | if (_shouldTrace) |
| | 1344 | | { |
| 0 | 1345 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1346 | | } |
| 0 | 1347 | | return _result; |
| 0 | 1348 | | } |
| | 1349 | |
|
| | 1350 | | /// <summary> |
| | 1351 | | /// Add a face to a specified face list, up to 1,000 faces. |
| | 1352 | | /// <br /> To deal with an image contains multiple faces, input face can |
| | 1353 | | /// be specified as an image with a targetFace rectangle. It returns a |
| | 1354 | | /// persistedFaceId representing the added face. No image will be stored. Only |
| | 1355 | | /// the extracted face feature will be stored on server until [FaceList - |
| | 1356 | | /// Delete |
| | 1357 | | /// Face](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395251) |
| | 1358 | | /// or [FaceList - |
| | 1359 | | /// Delete](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524f) |
| | 1360 | | /// is called. |
| | 1361 | | /// <br /> Note persistedFaceId is different from faceId generated by |
| | 1362 | | /// [Face - |
| | 1363 | | /// Detect](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236). |
| | 1364 | | /// * Higher face image quality means better detection and recognition |
| | 1365 | | /// precision. Please consider high-quality faces: frontal, clear, and face |
| | 1366 | | /// size is 200x200 pixels (100 pixels between eyes) or bigger. |
| | 1367 | | /// * JPEG, PNG, GIF (the first frame), and BMP format are supported. The |
| | 1368 | | /// allowed image file size is from 1KB to 6MB. |
| | 1369 | | /// * "targetFace" rectangle should contain one face. Zero or multiple faces |
| | 1370 | | /// will be regarded as an error. If the provided "targetFace" rectangle is not |
| | 1371 | | /// returned from [Face - |
| | 1372 | | /// Detect](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236), |
| | 1373 | | /// there’s no guarantee to detect and add the face successfully. |
| | 1374 | | /// * Out of detectable face size (36x36 - 4096x4096 pixels), large head-pose, |
| | 1375 | | /// or large occlusions will cause failures. |
| | 1376 | | /// * Adding/deleting faces to/from a same face list are processed sequentially |
| | 1377 | | /// and to/from different face lists are in parallel. |
| | 1378 | | /// * The minimum detectable face size is 36x36 pixels in an image no larger |
| | 1379 | | /// than 1920x1080 pixels. Images with dimensions higher than 1920x1080 pixels |
| | 1380 | | /// will need a proportionally larger minimum face size. |
| | 1381 | | /// * Different 'detectionModel' values can be provided. To use and compare |
| | 1382 | | /// different detection models, please refer to [How to specify a detection |
| | 1383 | | /// model](https://docs.microsoft.com/en-us/azure/cognitive-services/face/face-api-how-to-topics/specify-detecti |
| | 1384 | | /// | Model | Recommended use-case(s) | |
| | 1385 | | /// | ---------- | -------- | |
| | 1386 | | /// | 'detection_01': | The default detection model for [FaceList - Add |
| | 1387 | | /// Face](/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395250). |
| | 1388 | | /// Recommend for near frontal face detection. For scenarios with exceptionally |
| | 1389 | | /// large angle (head-pose) faces, occluded faces or wrong image orientation, |
| | 1390 | | /// the faces in such cases may not be detected. | |
| | 1391 | | /// | 'detection_02': | Detection model released in 2019 May with improved |
| | 1392 | | /// accuracy especially on small, side and blurry faces. | |
| | 1393 | | /// </summary> |
| | 1394 | | /// <param name='faceListId'> |
| | 1395 | | /// Id referencing a particular face list. |
| | 1396 | | /// </param> |
| | 1397 | | /// <param name='image'> |
| | 1398 | | /// An image stream. |
| | 1399 | | /// </param> |
| | 1400 | | /// <param name='userData'> |
| | 1401 | | /// User-specified data about the face for any purpose. The maximum length is |
| | 1402 | | /// 1KB. |
| | 1403 | | /// </param> |
| | 1404 | | /// <param name='targetFace'> |
| | 1405 | | /// A face rectangle to specify the target face to be added to a person in the |
| | 1406 | | /// format of "targetFace=left,top,width,height". E.g. |
| | 1407 | | /// "targetFace=10,10,100,100". If there is more than one face in the image, |
| | 1408 | | /// targetFace is required to specify which face to add. No targetFace means |
| | 1409 | | /// there is only one face detected in the entire image. |
| | 1410 | | /// </param> |
| | 1411 | | /// <param name='detectionModel'> |
| | 1412 | | /// Name of detection model. Detection model is used to detect faces in the |
| | 1413 | | /// submitted image. A detection model name can be provided when performing |
| | 1414 | | /// Face - Detect or (Large)FaceList - Add Face or (Large)PersonGroup - Add |
| | 1415 | | /// Face. The default value is 'detection_01', if another model is needed, |
| | 1416 | | /// please explicitly specify it. Possible values include: 'detection_01', |
| | 1417 | | /// 'detection_02' |
| | 1418 | | /// </param> |
| | 1419 | | /// <param name='customHeaders'> |
| | 1420 | | /// Headers that will be added to request. |
| | 1421 | | /// </param> |
| | 1422 | | /// <param name='cancellationToken'> |
| | 1423 | | /// The cancellation token. |
| | 1424 | | /// </param> |
| | 1425 | | /// <exception cref="APIErrorException"> |
| | 1426 | | /// Thrown when the operation returned an invalid status code |
| | 1427 | | /// </exception> |
| | 1428 | | /// <exception cref="SerializationException"> |
| | 1429 | | /// Thrown when unable to deserialize the response |
| | 1430 | | /// </exception> |
| | 1431 | | /// <exception cref="ValidationException"> |
| | 1432 | | /// Thrown when a required parameter is null |
| | 1433 | | /// </exception> |
| | 1434 | | /// <exception cref="System.ArgumentNullException"> |
| | 1435 | | /// Thrown when a required parameter is null |
| | 1436 | | /// </exception> |
| | 1437 | | /// <return> |
| | 1438 | | /// A response object containing the response body and response headers. |
| | 1439 | | /// </return> |
| | 1440 | | public async Task<HttpOperationResponse<PersistedFace>> AddFaceFromStreamWithHttpMessagesAsync(string faceListId |
| | 1441 | | { |
| 14 | 1442 | | if (Client.Endpoint == null) |
| | 1443 | | { |
| 0 | 1444 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 1445 | | } |
| 14 | 1446 | | if (faceListId == null) |
| | 1447 | | { |
| 0 | 1448 | | throw new ValidationException(ValidationRules.CannotBeNull, "faceListId"); |
| | 1449 | | } |
| 14 | 1450 | | if (faceListId != null) |
| | 1451 | | { |
| 14 | 1452 | | if (faceListId.Length > 64) |
| | 1453 | | { |
| 0 | 1454 | | throw new ValidationException(ValidationRules.MaxLength, "faceListId", 64); |
| | 1455 | | } |
| 14 | 1456 | | if (!System.Text.RegularExpressions.Regex.IsMatch(faceListId, "^[a-z0-9-_]+$")) |
| | 1457 | | { |
| 0 | 1458 | | throw new ValidationException(ValidationRules.Pattern, "faceListId", "^[a-z0-9-_]+$"); |
| | 1459 | | } |
| | 1460 | | } |
| 14 | 1461 | | if (userData != null) |
| | 1462 | | { |
| 0 | 1463 | | if (userData.Length > 1024) |
| | 1464 | | { |
| 0 | 1465 | | throw new ValidationException(ValidationRules.MaxLength, "userData", 1024); |
| | 1466 | | } |
| | 1467 | | } |
| 14 | 1468 | | if (image == null) |
| | 1469 | | { |
| 0 | 1470 | | throw new ValidationException(ValidationRules.CannotBeNull, "image"); |
| | 1471 | | } |
| | 1472 | | // Tracing |
| 14 | 1473 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 14 | 1474 | | string _invocationId = null; |
| 14 | 1475 | | if (_shouldTrace) |
| | 1476 | | { |
| 0 | 1477 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1478 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1479 | | tracingParameters.Add("faceListId", faceListId); |
| 0 | 1480 | | tracingParameters.Add("userData", userData); |
| 0 | 1481 | | tracingParameters.Add("targetFace", targetFace); |
| 0 | 1482 | | tracingParameters.Add("image", image); |
| 0 | 1483 | | tracingParameters.Add("detectionModel", detectionModel); |
| 0 | 1484 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1485 | | ServiceClientTracing.Enter(_invocationId, this, "AddFaceFromStream", tracingParameters); |
| | 1486 | | } |
| | 1487 | | // Construct URL |
| 14 | 1488 | | var _baseUrl = Client.BaseUri; |
| 14 | 1489 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "facelists/{faceListId}/persistedfaces"; |
| 14 | 1490 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 14 | 1491 | | _url = _url.Replace("{faceListId}", System.Uri.EscapeDataString(faceListId)); |
| 14 | 1492 | | List<string> _queryParameters = new List<string>(); |
| 14 | 1493 | | if (userData != null) |
| | 1494 | | { |
| 0 | 1495 | | _queryParameters.Add(string.Format("userData={0}", System.Uri.EscapeDataString(userData))); |
| | 1496 | | } |
| 14 | 1497 | | if (targetFace != null) |
| | 1498 | | { |
| 12 | 1499 | | _queryParameters.Add(string.Format("targetFace={0}", System.Uri.EscapeDataString(string.Join(",", target |
| | 1500 | | } |
| 14 | 1501 | | if (detectionModel != null) |
| | 1502 | | { |
| 14 | 1503 | | _queryParameters.Add(string.Format("detectionModel={0}", System.Uri.EscapeDataString(detectionModel))); |
| | 1504 | | } |
| 14 | 1505 | | if (_queryParameters.Count > 0) |
| | 1506 | | { |
| 14 | 1507 | | _url += "?" + string.Join("&", _queryParameters); |
| | 1508 | | } |
| | 1509 | | // Create HTTP transport objects |
| 14 | 1510 | | var _httpRequest = new HttpRequestMessage(); |
| 14 | 1511 | | HttpResponseMessage _httpResponse = null; |
| 14 | 1512 | | _httpRequest.Method = new HttpMethod("POST"); |
| 14 | 1513 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1514 | | // Set Headers |
| | 1515 | |
|
| | 1516 | |
|
| 14 | 1517 | | if (customHeaders != null) |
| | 1518 | | { |
| 0 | 1519 | | foreach(var _header in customHeaders) |
| | 1520 | | { |
| 0 | 1521 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1522 | | { |
| 0 | 1523 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1524 | | } |
| 0 | 1525 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1526 | | } |
| | 1527 | | } |
| | 1528 | |
|
| | 1529 | | // Serialize Request |
| 14 | 1530 | | string _requestContent = null; |
| 14 | 1531 | | if(image == null) |
| | 1532 | | { |
| 0 | 1533 | | throw new System.ArgumentNullException("image"); |
| | 1534 | | } |
| 14 | 1535 | | if (image != null && image != Stream.Null) |
| | 1536 | | { |
| 14 | 1537 | | _httpRequest.Content = new StreamContent(image); |
| 14 | 1538 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 1539 | | } |
| | 1540 | | // Set Credentials |
| 14 | 1541 | | if (Client.Credentials != null) |
| | 1542 | | { |
| 14 | 1543 | | cancellationToken.ThrowIfCancellationRequested(); |
| 14 | 1544 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1545 | | } |
| | 1546 | | // Send Request |
| 14 | 1547 | | if (_shouldTrace) |
| | 1548 | | { |
| 0 | 1549 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1550 | | } |
| 14 | 1551 | | cancellationToken.ThrowIfCancellationRequested(); |
| 14 | 1552 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 14 | 1553 | | if (_shouldTrace) |
| | 1554 | | { |
| 0 | 1555 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1556 | | } |
| 14 | 1557 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 14 | 1558 | | cancellationToken.ThrowIfCancellationRequested(); |
| 14 | 1559 | | string _responseContent = null; |
| 14 | 1560 | | if ((int)_statusCode != 200) |
| | 1561 | | { |
| 0 | 1562 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | 1563 | | try |
| | 1564 | | { |
| 0 | 1565 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1566 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| 0 | 1567 | | if (_errorBody != null) |
| | 1568 | | { |
| 0 | 1569 | | ex.Body = _errorBody; |
| | 1570 | | } |
| 0 | 1571 | | } |
| 0 | 1572 | | catch (JsonException) |
| | 1573 | | { |
| | 1574 | | // Ignore the exception |
| 0 | 1575 | | } |
| 0 | 1576 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1577 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1578 | | if (_shouldTrace) |
| | 1579 | | { |
| 0 | 1580 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1581 | | } |
| 0 | 1582 | | _httpRequest.Dispose(); |
| 0 | 1583 | | if (_httpResponse != null) |
| | 1584 | | { |
| 0 | 1585 | | _httpResponse.Dispose(); |
| | 1586 | | } |
| 0 | 1587 | | throw ex; |
| | 1588 | | } |
| | 1589 | | // Create Result |
| 14 | 1590 | | var _result = new HttpOperationResponse<PersistedFace>(); |
| 14 | 1591 | | _result.Request = _httpRequest; |
| 14 | 1592 | | _result.Response = _httpResponse; |
| | 1593 | | // Deserialize Response |
| 14 | 1594 | | if ((int)_statusCode == 200) |
| | 1595 | | { |
| 14 | 1596 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1597 | | try |
| | 1598 | | { |
| 14 | 1599 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<PersistedFace>(_responseContent, |
| 14 | 1600 | | } |
| 0 | 1601 | | catch (JsonException ex) |
| | 1602 | | { |
| 0 | 1603 | | _httpRequest.Dispose(); |
| 0 | 1604 | | if (_httpResponse != null) |
| | 1605 | | { |
| 0 | 1606 | | _httpResponse.Dispose(); |
| | 1607 | | } |
| 0 | 1608 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1609 | | } |
| | 1610 | | } |
| 14 | 1611 | | if (_shouldTrace) |
| | 1612 | | { |
| 0 | 1613 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1614 | | } |
| 14 | 1615 | | return _result; |
| 14 | 1616 | | } |
| | 1617 | |
|
| | 1618 | | } |
| | 1619 | | } |