Adventures of a wannabe geek!

Ranting within

Easy HTTP–not Just a Catchy Name

I was recently referred to EasyHTTP by it’s creator, Hadi Hariri. Hadi was looking at some code I had sent him regarding my work with the TeamCity REST API and thought that things could be a little easier and less verbose.

In summary my application creates a new HttpWebRequest passing in the request type as a magic string. It opens a stream to read the JSON response and that JSON is then de-serialised to an entity. It was very messy. In order to call this class from my application I would have had to do the follow:

There is absolutely no mention of whether the request was a GET never mind a PUT or a DELETE. This is where EasyHTTP came in for me.

I was able to make a generic method for a GET request. This method takes a url and a type and with the help of JsonFx, de-serialises the response back to the requested type. THis was a great way to take the hassle out of my old code where I was creating a Url, calling a request and then de-serialising it. The code in the calling class now looks as follows:

I was originally using the Newtonsoft JSON library for my de-serialising my JSON back to types and to do that I had to decorate the properties with code similar to the following:

[JsonProperty(PropertyName = “id”)]
public string Id {get; set;}

This would effectively map a piece of JSON to a property that was not named exactly the same. EasyHTTP took care of this for me as well. To quote Hadi, EasyHTTP handles mapping as follows:

“For mapping properties, it handles camelcase, lowercase and a bunch of other conventions. You can also specify explicitly”

Using EasyHTTP, I was able to cut at least 2 lines of code per method in the client code. I was also able to delete an attribute that was present on all properties in the entities in a little bit of work.

The added distinction of creating the notion of GETs, PUTs and DELETE calls from each other mean that my class can be extended much easier. Hadi has written a post on EasyHTTP on his blog. I would strongly suggest reading this and discovering if you can use this application in place of old HttpWebRequest calls. Its cleaner and helps to make code a little bit more DRY. My thanks to Hadi (and any contributors there may be) for making my work in this area a little easier!