Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Created December 18, 2015 11:44
Show Gist options
  • Save lkaczanowski/d93c6a0710a489982662 to your computer and use it in GitHub Desktop.
Save lkaczanowski/d93c6a0710a489982662 to your computer and use it in GitHub Desktop.
RestSharp extensions
using System;
using System.Net;
using System.Text;
using RestSharp;
namespace RestSharpExtensions
{
public 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 RestException CreateException(Uri requestUri, IRestResponse response)
{
Exception innerException = null;
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine("Processing request resulted with following errors:");
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 RestException(response.StatusCode, requestUri, response.Content, messageBuilder.ToString(), innerException);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment