예제 #1
0
        private static AutoaddressException GetAutoaddressException(HttpStatusCode httpStatusCode, Uri requestUri, string response)
        {
            JObject obj;

            try
            {
                obj = JObject.Parse(response);
            }
            catch
            {
                return(new AutoaddressException(ErrorType.Unknown, httpStatusCode, requestUri, $"response=[{response}]"));
            }

            try
            {
                AutoaddressException autoaddressException = null;
                if (obj["errors"] != null && obj["errors"].HasValues && obj["errors"][0]["type"]["code"] != null && obj["errors"][0]["message"] != null)
                {
                    ErrorType errorType = (ErrorType)((int)obj["errors"][0]["type"]["code"]);
                    string    message   = obj["errors"][0]["message"].ToString();
                    autoaddressException = new AutoaddressException(errorType, httpStatusCode, requestUri, message);
                }
                return(autoaddressException);
            }
            catch
            {
                return(new AutoaddressException(ErrorType.Unknown, httpStatusCode, requestUri, $"response=[{response}]"));
            }
        }
예제 #2
0
        private async Task <T> GetResponseAsync <T>(object request, Uri requestUri)
        {
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);

            httpRequestMessage.Headers.Accept.Clear();
            httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(JsonContentType));
            httpRequestMessage.Headers.Add("Client", $"Autoaddress2.0SDK {AssemblyVersion}");

            EnsureHttpClient();

            var completionOption  = HttpCompletionOption.ResponseContentRead;
            var cancellationToken = CancellationToken.None;

            PreRequest?.Invoke(this, new PreRequestEventArgs(request, httpRequestMessage));

            HttpResponseMessage response = await _httpClient.SendAsync(httpRequestMessage, completionOption, cancellationToken).ConfigureAwait(false);

            string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            PostRequest?.Invoke(this, new PostRequestEventArgs(response, content));

            if (!response.IsSuccessStatusCode)
            {
                if ((int)response.StatusCode == 429)
                {
                    throw new TooManyRequestsException(response.ReasonPhrase);
                }

                AutoaddressException autoaddressException = GetAutoaddressException(response.StatusCode, httpRequestMessage.RequestUri, content);

                if (autoaddressException != null)
                {
                    throw autoaddressException;
                }

                // guard
                response.EnsureSuccessStatusCode();

                throw new InvalidOperationException();
            }

            string result = ParseJson(content);

            return(JsonConvert.DeserializeObject <T>(result));
        }