Container of ClientError and Error Entity.
        /// <summary>
        /// Helper method executing the REST request.
        /// </summary>
        /// <typeparam name="TRequest">Type of request.</typeparam>
        /// <typeparam name="TResponse">Type of response.</typeparam>
        /// <param name="faceRectangles">Optional list of face rectangles.</param>
        /// <param name="requestBody">Content of the HTTP request.</param>
        /// <returns></returns>
        private async Task <TResponse> SendRequestAsync <TRequest, TResponse>(Rectangle[] faceRectangles, TRequest requestBody)
        {
            var httpMethod = HttpMethod.Post;
            var requestUri = new StringBuilder();

            requestUri.AppendFormat("{0}/{1}", _serviceUrl, RecognizeQuery);
            requestUri.Append('?');
            if (faceRectangles != null)
            {
                requestUri.Append(FaceRectangles);
                requestUri.Append('=');
                foreach (var rectangle in faceRectangles)
                {
                    requestUri.AppendFormat("{0},{1},{2},{3};",
                                            rectangle.Left,
                                            rectangle.Top,
                                            rectangle.Width,
                                            rectangle.Height);
                }
                requestUri.Remove(requestUri.Length - 1, 1); // drop last comma
                requestUri.Append('&');
            }
            requestUri.AppendFormat("{0}={1}",
                                    SubscriptionKeyName,
                                    _subscriptionKey);

            var request = new HttpRequestMessage(httpMethod, _serviceUrl);

            request.RequestUri = new Uri(requestUri.ToString());

            if (requestBody != null)
            {
                if (requestBody is Stream)
                {
                    request.Content = new StreamContent(requestBody as Stream);
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                }
                else
                {
                    request.Content = new StringContent(JsonConvert.SerializeObject(requestBody, s_settings), Encoding.UTF8, JsonHeader);
                }
            }

            HttpResponseMessage response = await _httpClient.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                string responseContent = null;
                if (response.Content != null)
                {
                    responseContent = await response.Content.ReadAsStringAsync();
                }

                if (!string.IsNullOrWhiteSpace(responseContent))
                {
                    return(JsonConvert.DeserializeObject <TResponse>(responseContent, s_settings));
                }

                return(default(TResponse));
            }
            else
            {
                if (response.Content != null && response.Content.Headers.ContentType.MediaType.Contains(JsonHeader))
                {
                    var errorObjectString = await response.Content.ReadAsStringAsync();

                    ClientError errorCollection = JsonConvert.DeserializeObject <ClientError>(errorObjectString);
                    if (errorCollection != null)
                    {
                        throw new ClientException(errorCollection, response.StatusCode);
                    }
                }

                response.EnsureSuccessStatusCode();
            }

            return(default(TResponse));
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientException"/> class.
 /// </summary>
 /// <param name="error">The error entity.</param>
 /// <param name="httpStatus">The http status.</param>
 public ClientException(ClientError error, HttpStatusCode httpStatus)
 {
     this.Error      = error;
     this.HttpStatus = httpStatus;
 }