Monday, October 8, 2012

Introducing Resty.Net

Well it's been very long since I last blogged, I was waiting to stumble some new experience worth sharing. Today I'd like to introduce my first ever open source project Resty.Net hosted in github.

So what is Resty.Net?

Resty.Net is a simple bare to metal REST/HTTP client for .net framework written in c#. It serves as a very thin layer above the HttpWebRequest object and exposes most of the properties available in HttpWebRequest.

Why another Rest client?

It's true that there are many rest clients for dotnet framework. I have used the HttpClient from microsoft, I have also used another mature and inspirational and superb Restsharp library. I have liked both of them quite well. But I needed something quite lightweight, and something that gave me more control over the underlying HttpWebRequest. In fact I wanted to use HttpWebRequest directly but with all the strongly typed supports and the API goodness that HttpClient and RestSharp provided. So, the result is Resty.Net.

Well I had been planning for an open-source project for a long time, but the dream hadn't materialized till now, so when I created this rest client, I thought about open sourcing it.

Some examples.

RestRequest request = new RestRequest(HttpMethod.GET, new RestUri("http://example.com/api", "/Person/{id}").SetParameter("id", "1"));

request.AddHeader(new { header = "value" });
request.AddHeader("header2", "value");

using (RestResponse response = request.GetResponse())
{
  var contentStream = response.Body.ReadAsStream();
  var contentByteArray = response.Body.ReadAsByteArray();
  string contentString = response.Body.ReadAsString();
}

//strongly typed response
RestRequest request2 = new RestRequest(HttpMethod.GET, new RestUri("http://example.com/api", "/Person/{id}").SetParameter("id", "2"));

request.AddHeader(new { header = "value" });
request.AddHeader("header2", "value");

using (RestResponse<person> response = request.GetResponse<person>())
{
  var person = response.Data;
  var email = person.Email;
}

I think the API itself is quite clear, create a RestRequest instance and call it's GetResponse method to send the request to server and get the response from server. RestRequest constructor takes two arguments:
  • The HttpMethod
  • The RestUri.
These two are very important and most basic concepts in Rest apis. To send the content to server (e.g. inPOST method) the Body property must be set. Next thing is to call GetResponse method, which will return an instance of RestResponse. GetResponse also has a strongly typed overload, which will return strongly typed RestResponse and that's it.

Give me Restsharp like RestClient.

If you prefer a RestSharpis api more, then RestInvoker class under Rest.Net.Extras namespace is just the thing for you.
using Rest.Net.Extras;
.
.
.
RestInvoker invoker = new RestInvoker();
RestResponse response invoker.Get(new RestUri("http://example.com/api", "/Person/{id}").SetParameter("id", "2"));

Where is my Fluent API?

The RestRequestBuilder class under Rest.Net.Extras gives you a way to build RestRequest with fluent API.
using Rest.Net.Extras;
.
.
.
RestRequest request = RestRequestBuilder.CreateRequest(HttpMethod.Get, new RestUri("http://example.com/api", "/Person/{id}").SetParameter("id", "2"))
        .WithHeader("accept","application/json")
        .WithHeader("header2","value")
        .WithCookie("cookie1","value");

Where to get it?

Of course in GitHub https://github.com/nripendra/Resty.Net, you can fork it or just clone it locally, and if feeling generous you can also contribute to the project.

I have also uploaded a prelease nuget package at http://nuget.org/packages/Resty.Net/1.0.1-beta.
Since, I have designeted it as a prerelease, -pre flag must be included in nuget install-package command.
Install-Package Resty.Net -Pre

"Include Prelease" dropdown options must be selected when using Nuget package manage GUI or even searching package in nuget.org.

Finally

I have tried to follow principles like DRY, and open-close as far as I understood them. It is by no means complete product and likely to change a lot. I'll try to find time for posting more Resty.Net features in future, and also keep on improving it.

3 comments:

  1. When I try your example, I only get 256 bytes back in the response. How do I get the entire response?

    ReplyDelete
    Replies
    1. Hi Barry,

      Could you please be a bit more specific, what code you're writing and what problem you are going through?

      Regards.

      Delete
  2. Thanks for your reply. I've moved on to a different way of doing things, but the problem I was having with yours is that my responses were always only 256 bytes long. No errors either. It doesn't matter now, though, since I've adopted another implementation. Thanks again.

    ReplyDelete