/// <summary>
        /// Gets the <see cref="CloudFlareResponse{T}"/> of a CloudFlare API <paramref name="response"/>.
        /// </summary>
        /// <typeparam name="T">The type of the <see cref="CloudFlareResponse{T}.Result"/>.</typeparam>
        public static async Task <CloudFlareResponse <T> > ReadCloudFlareResponseAsync <T>(
            this HttpResponseMessage response,
            CancellationToken cancellationToken)
            where T : class
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            if (response.IsSuccessStatusCode)
            {
                return(await response
                       .Content
                       .ReadAsAsync <CloudFlareResponse <T> >(cancellationToken)
                       .ConfigureAwait(false));
            }

            CloudFlareResponseBase errorResponse =
                await response
                .Content
                .ReadAsAsync <CloudFlareResponseBase>(cancellationToken)
                .ConfigureAwait(false);

            throw new CloudFlareException(errorResponse, response);
        }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CloudFlareException"/> class.
 /// </summary>
 public CloudFlareException(
     CloudFlareResponseBase response,
     HttpResponseMessage httpResponse = null,
     Exception innerException         = null)
     : base(GetMessage(response, httpResponse), innerException)
 {
     Response = response;
     SetupData(response);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CloudFlareException"/> class.
 /// </summary>
 public CloudFlareException(
     CloudFlareResponseBase response,
     HttpResponseMessage httpResponse = null,
     Exception innerException = null)
     : base(GetMessage(response, httpResponse), innerException)
 {
     Response = response;
     SetupData(response);
 }
        private static string GetMessage(CloudFlareResponseBase response, HttpResponseMessage httpResponse = null)
        {
            if (response == null)
                throw new ArgumentNullException(nameof(response));

            string message = response.Errors.FirstOrDefault()?.Message ?? "Unknown error.";
            string requestMethod = httpResponse?.RequestMessage?.Method?.ToString() ?? "Unknown HTTP Method";
            string requestUri = httpResponse?.RequestMessage?.RequestUri?.AbsoluteUri ?? "Unknown Uri";

            return $"{message}: {requestMethod} {requestUri}";
        }
Beispiel #5
0
        private static string GetMessage(CloudFlareResponseBase response, HttpResponseMessage httpResponse = null)
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            string message       = response.Errors.FirstOrDefault()?.Message ?? "Unknown error.";
            string requestMethod = httpResponse?.RequestMessage?.Method?.ToString() ?? "Unknown HTTP Method";
            string requestUri    = httpResponse?.RequestMessage?.RequestUri?.AbsoluteUri ?? "Unknown Uri";

            return($"{message}: {requestMethod} {requestUri}");
        }
Beispiel #6
0
        public static Likeness <CloudFlareResponseBase, CloudFlareResponseBase> AsLikeness(
            this CloudFlareResponseBase actual)
        {
            if (actual == null)
            {
                throw new ArgumentNullException(nameof(actual));
            }

            return(actual
                   .AsSource()
                   .OfLikeness <CloudFlareResponseBase>()
                   .With(r => r.Errors)
                   .EqualsWhen((a, e) => a.Errors.SequenceEqual(e.Errors))
                   .With(r => r.Messages)
                   .EqualsWhen((a, e) => a.Messages.SequenceEqual(e.Messages))
                   .With(r => r.ResultInfo)
                   .EqualsWhen((a, e) => a.ResultInfo.AsLikeness().Equals(e.ResultInfo)));
        }
        private void SetupData(CloudFlareResponseBase response)
        {
            if (response == null)
                throw new ArgumentNullException(nameof(response));

            int index = 1;
            foreach (CloudFlareError error in response.Errors)
            {
                Data[$"ErrorCode{index}"] = error.Code;
                Data[$"ErrorMessage{index}"] = error.Message;
            }

            index = 1;
            foreach (string message in response.Messages)
            {
                Data[$"Message{index}"] = message;
            }
        }
Beispiel #8
0
        private void SetupData(CloudFlareResponseBase response)
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            int index = 1;

            foreach (CloudFlareError error in response.Errors)
            {
                Data[$"ErrorCode{index}"]    = error.Code;
                Data[$"ErrorMessage{index}"] = error.Message;
            }

            index = 1;
            foreach (string message in response.Messages)
            {
                Data[$"Message{index}"] = message;
            }
        }