A simple library for .net core that can help you to consume REST services.
We have two object, RESTClient and RESTResponse that implements IRESTClient and IRESTResponse interfaces. We have also extended the HttpClient object with Patch\Head\Options wrappers over the classic GetAsync\PutAsync\PostAsync, so we have a full REST Client methods support.
Please check inline docs for more information.
RESTClient http status macrotypes:
- 2xx : Succesful
- 3xx : Redirected
- 4xx : Error
- 5xx : Fault
PUT, GET, POST, PATCH, HEAD, OPTIONS
The library is based on three interfaces:
- IRESTClient : is the RESTClient core for connnections
- IRESTResponse : is the RESTResponse object
- IRESTRequest : is the RESTRequest object
With this approach you can use the dependency injection and use also your implementation of code.
You can use the library creating and filling a RESTRequest object this way.
- Instantiate a RESTRequest object
- Set URL, Method, etc
- Execute RESTClient.PerformRequest(myRESTRequestObject)
Es:
var apiHost = "https://myapi.com/";
using(RESTRequest request = new RESTRequest())
{
request.SetMethod(Method.GET);
request.SetUrl(apiHost + "2/categories");
request.AddQueryArgs("order", "asc");
request.AddHeader("Accept", "application/json");
using(RESTClient client = new RESTClient())
{
using(RESTResponse response = (RESTResponse)await client.PerformRequest(request))
{
string myJson = null;
if(response.IsSuccesStatusCode())
myJson = response.ReadResponseAsStringAsync();
...
}
}
}
Or you can use directly RESTClient methods versions (PUT, GET, PATCH, ETC)
Es:
var apiHost = "https://myapi.com/";
Dictionary<string, string> queryArgs = new Dictionary<string, string>();
queryArgs.Add("order", "asc");
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Accept", "application/json");
using(RESTClient client = new RESTClient())
using(RESTResponse response = (RESTResponse)await client.PerformGetRequestAsync(apiHost + "2/categories", queryAargs, headers))
{
string myJson = null;
if(response.IsSuccesStatusCode())
myJson = response.ReadResponseAsStringAsync();
...
}
}
- RESTResponse : The response object
- RESTClient : The client object
- RESTRequest : Request object
- GetStatusCode() : HttpStatusCode - Return the status code.
- IsSuccessStatusCode() : bool - True if the type is Success
- IsRedirectedStatusCode() : bool - True if type is Redirect
- IsErrorStatusCode() : bool - True if type is Error
- IsFaultStatusCode() : bool - True if type is Fault
- GetHttpStatusCodeType : HttpStatusType - Return the status code type. If you want the status code you can use the enum in System.Net
- ReadResponseAsStringAsync : awaitable Task<string> - Return the response as a string
- GetResponse : HttpResponseMessage - Get the direct response object
- RESTResponse(HttpResponseMessage) : constructor - Construct a new RESTResponse object
- SetValidation(HeadersValidation) : void - Set if the header must be validated before sending
- GetRESTResponse(HttpResponseMessage) : IRESTResponse - Return a new RESTResèpmse object from the HttpResponseMessage
- IsValidUrl(string) : bool - Check if an url is valid
- GetHandler() : HttpClientHandler - Get current http handler
- SetHandler(HttpClientHandler) : Set a new handler for client. This will create also a new instance of httpclient in it
- RESTClient(CancellationToken) : constructor
- __RESTClient(HeadersValidation, CancellationToken) : constructor
- RESTClient(HeaderValidatoin, HttpClientHandler, CancellationToken) : constructor
- __RESTClient(HttpClientHandler, CancellationToken) : constructor
- PerformRequest(IRESTRequest) : awaitable Task<IRESTResponse> - Perform a request by an IRESTRequest object
- PerformDeleteRequestAsync(string, Dictionary<string,string>,Dictionary<string,string>) : awaitable Task<IRESTResponse> - Perform a delete request by an IRESTRequest object
- PerformPutRequestAsync(string, Dictionary<string,string>,Dictionary<string,string>) : awaitable Task<IRESTResponse> - Perform a put request by an IRESTRequest object
- PerformPatchRequestAsync(string, Dictionary<string,string>,Dictionary<string,string>) : awaitable Task<IRESTResponse> - Perform a patch request by an IRESTRequest object
- PerformPostRequestAsync(string, Dictionary<string,string>,Dictionary<string,string>) : awaitable Task<IRESTResponse> - Perform a post request by an IRESTRequest object
- PerformGetRequestAsync(string, Dictionary<string,string>,Dictionary<string,string>) : awaitable Task<IRESTResponse> - Perform a get request by an IRESTRequest object
- PerformHeadRequestAsync(string, Dictionary<string,string>,Dictionary<string,string>) : awaitable Task<IRESTResponse> - Perform a head request by an IRESTRequest object
- PerformOptionsRequestAsync(string, Dictionary<string,string>,Dictionary<string,string>) : awaitable Task<IRESTResponse> - Perform a options request by an IRESTRequest object
- SetContent(HttpContent) : void - Add a content to the request
- SetHeader(string,string) : void - Add new header to the request
- SetQueryArgs(string,string) : void - Add a new argoument to the query string
- SetUrl(string) : void - Set the url to the request
- IsValidUrl(string) : bool - Check if an url is valid
- GetMethod() : Method - return the current method
- SetMethod(Method) : void - return the current method
- GetHeaders() : Dictionary<string,string> - Return the setted headers
- GetContent() : HttpContent - Return the setted content
- GetQueryArgs() : Dictionary<string,string> - Return the setted headers
- GetUrl() : string - Return the request URL
- AddMultipartFormDataContent(string,byte[],string) : void - Add a multipart form data
- AddMultipartFormDataContent(string,string) : void - Add a multipart form data
- AddMultipartFormDataContent(string,Stream,string) : void - Add a multipart form data
- AddFormUrlEncodedContent(string,string) : void - Add a form url encoded data
- Dispose() : void
- RESTRequest(string) : constructor - Construct a RESTRequest with url
- RESTRequest() : constructor
You can find it on nuget with the name DewRESTClient