예제 #1
0
        internal static XmlResponse Post(RestClient rest,
                                         string endpoint,
                                         string deviceId,
                                         DateTime timestamp,
                                         Dictionary <string, object> parameters,
                                         string username = null,
                                         byte[] token    = null)
        {
            var headers = new Dictionary <string, string>
            {
                ["Accept"]     = "application/xml",
                ["Date"]       = timestamp.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'", EnUs),
                ["User-Agent"] = GetUserAgent(deviceId),
            };

            if (!username.IsNullOrEmpty())
            {
                headers["Authorization"] = GetAuthorizationHeader(username, token);
            }

            var response = rest.PostForm(endpoint, parameters, headers);

            if (response.IsSuccessful)
            {
                return(XmlResponse.Parse(response.Content));
            }

            if (response.IsNetworkError)
            {
                throw new NetworkErrorException("Network error has occurred", response.Error);
            }

            // Special handling for 401. There's no other way to tell if the password is correct.
            // TODO: Write a test for this path. Now it should be easy once we transitioned away
            //       from HttpWebResponse.
            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new BadCredentialsException("The password is incorrect");
            }

            throw new InternalErrorException(
                      $"HTTP request to '{response.RequestUri}' failed with status {response.StatusCode}");
        }
예제 #2
0
        private static XmlResponse HandlePostResponse(Func <string> post)
        {
            try
            {
                return(XmlResponse.Parse(post()));
            }
            catch (WebException e)
            {
                // Special handling for 401. There's no other way to tell if the password is correct.
                // TODO: Write a test for this path. It's not trivial to mock HttpWebResponse
                //       if at all possible.
                var r = e.Response as HttpWebResponse;
                if (r != null && r.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new FetchException(FetchException.FailureReason.IncorrectPassword,
                                             "Incorrect password",
                                             e);
                }

                throw new FetchException(FetchException.FailureReason.NetworkError,
                                         "Network request failed",
                                         e);
            }
        }