예제 #1
0
        private ApiResponse <T> ToApiResponse <T>(IRestResponse <T> response)
        {
            T      result     = response.Data;
            string rawContent = response.Content;

            var transformed = new ApiResponse <T>(response.StatusCode, new Multimap <string, string>(), result, rawContent)
            {
                ErrorText = response.ErrorMessage,
                Cookies   = new List <Cookie>()
            };

            if (response.Headers != null)
            {
                foreach (var responseHeader in response.Headers)
                {
                    transformed.Headers.Add(responseHeader.Name, ClientUtils.ParameterToString(responseHeader.Value));
                }
            }

            if (response.Cookies != null)
            {
                foreach (var responseCookies in response.Cookies)
                {
                    transformed.Cookies.Add(
                        new Cookie(
                            responseCookies.Name,
                            responseCookies.Value,
                            responseCookies.Path,
                            responseCookies.Domain)
                        );
                }
            }

            return(transformed);
        }
예제 #2
0
        private ApiResponse <T> ToApiResponse <T>(HttpResponseMessage response, object responseData, HttpClientHandler handler, Uri uri)
        {
            T      result     = (T)responseData;
            string rawContent = response.Content.ToString();

            var transformed = new ApiResponse <T>(response.StatusCode, new Multimap <string, string>(), result, rawContent)
            {
                ErrorText = response.ReasonPhrase,
                Cookies   = new List <Cookie>()
            };

            // process response headers, e.g. Access-Control-Allow-Methods
            if (response.Headers != null)
            {
                foreach (var responseHeader in response.Headers)
                {
                    transformed.Headers.Add(responseHeader.Key, ClientUtils.ParameterToString(responseHeader.Value));
                }
            }

            // process response content headers, e.g. Content-Type
            if (response.Content.Headers != null)
            {
                foreach (var responseHeader in response.Content.Headers)
                {
                    transformed.Headers.Add(responseHeader.Key, ClientUtils.ParameterToString(responseHeader.Value));
                }
            }

            if (!_disableHandlerFeatures)
            {
                if (response != null)
                {
                    try {
                        foreach (Cookie cookie in handler.CookieContainer.GetCookies(uri))
                        {
                            transformed.Cookies.Add(cookie);
                        }
                    }
                    catch (PlatformNotSupportedException) {}
                }
            }

            return(transformed);
        }