Skip to content

Instantly share code, notes, and snippets.

@Hoang-Minh
Forked from lkaczanowski/RestSharpExtensions.cs
Created October 7, 2022 05:05
Show Gist options
  • Select an option

  • Save Hoang-Minh/0b584746aaf94c191f522ad7f3c16a45 to your computer and use it in GitHub Desktop.

Select an option

Save Hoang-Minh/0b584746aaf94c191f522ad7f3c16a45 to your computer and use it in GitHub Desktop.

Revisions

  1. @lkaczanowski lkaczanowski revised this gist Apr 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RestSharpExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ namespace RestSharpExtensions
    {
    internal static class RestSharpExtensions
    {
    public static bool IsScuccessStatusCode(this HttpStatusCode responseCode)
    public static bool IsSuccessStatusCode(this HttpStatusCode responseCode)
    {
    var numericResponse = (int)responseCode;

  2. @lkaczanowski lkaczanowski revised this gist Dec 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RestSharpExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -55,7 +55,7 @@ public RestException(HttpStatusCode httpStatusCode, Uri requestUri, string conte

    public string Content { get; private set; }

    public static Model.RestException CreateException(Uri requestUri, IRestResponse response)
    public static RestException CreateException(Uri requestUri, IRestResponse response)
    {
    Exception innerException = null;

  3. @lkaczanowski lkaczanowski revised this gist Dec 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RestSharpExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -75,7 +75,7 @@ public static Model.RestException CreateException(Uri requestUri, IRestResponse
    innerException = response.ErrorException;
    }

    return new Model.RestException(response.StatusCode, requestUri, response.Content, messageBuilder.ToString(), innerException);
    return new RestException(response.StatusCode, requestUri, response.Content, messageBuilder.ToString(), innerException);
    }
    }
    }
  4. @lkaczanowski lkaczanowski created this gist Dec 22, 2015.
    81 changes: 81 additions & 0 deletions RestSharpExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    using System;
    using System.Net;
    using System.Text;

    using RestSharp;

    namespace RestSharpExtensions
    {
    internal static class RestSharpExtensions
    {
    public static bool IsScuccessStatusCode(this HttpStatusCode responseCode)
    {
    var numericResponse = (int)responseCode;

    const int statusCodeOk = (int)HttpStatusCode.OK;

    const int statusCodeBadRequest = (int)HttpStatusCode.BadRequest;

    return numericResponse >= statusCodeOk &&
    numericResponse < statusCodeBadRequest;
    }

    public static bool IsSuccessful(this IRestResponse response)
    {
    return response.StatusCode.IsScuccessStatusCode() &&
    response.ResponseStatus == ResponseStatus.Completed;
    }

    public static void EnsureResponseWasSuccessful(this IRestClient client, IRestRequest request, IRestResponse response)
    {
    if (response.IsSuccessful())
    {
    return;
    }

    var requestUri = client.BuildUri(request);

    throw RestException.CreateException(requestUri, response);
    }
    }

    public class RestException : Exception
    {
    public RestException(HttpStatusCode httpStatusCode, Uri requestUri, string content, string message, Exception innerException)
    : base(message, innerException)
    {
    HttpStatusCode = httpStatusCode;
    RequestUri = requestUri;
    Content = content;
    }

    public HttpStatusCode HttpStatusCode { get; private set; }

    public Uri RequestUri { get; private set; }

    public string Content { get; private set; }

    public static Model.RestException CreateException(Uri requestUri, IRestResponse response)
    {
    Exception innerException = null;

    var messageBuilder = new StringBuilder();

    messageBuilder.AppendLine(string.Format("Processing request [{0}] resulted with following errors:", requestUri));

    if (response.StatusCode.IsScuccessStatusCode() == false)
    {
    messageBuilder.AppendLine("- Server responded with unsuccessfult status code: " + response.StatusDescription);
    }

    if (response.ErrorException != null)
    {
    messageBuilder.AppendLine("- An exception occurred while processing request: " + response.ErrorMessage);

    innerException = response.ErrorException;
    }

    return new Model.RestException(response.StatusCode, requestUri, response.Content, messageBuilder.ToString(), innerException);
    }
    }
    }