This project is read-only.
Fluent Web Request (A simple wrapper of WebRequest class using Fluent style)

The need to consume services increases every day. It is common for a single project have to exchange information with multiple (2, 3, 5, 10) services, particularly REST type services.

Using .NET WebRequest class it is relatively simple, so it's normal to find code like this in our projects:

string serviceUrl = @"";

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
httpRequest.ContentType = "application/json; charset=utf-8";
httpRequest.Method = WebRequestMethods.Http.Post;

JObject jsonRequest = new JObject(new JProperty("dataForRequestBody", someValue));
string requestBody = JsonConvert.SerializeObject(jsonRequest);

using (StreamWriter streamWriter = new StreamWriter(httpRequest.GetRequestStream()))

using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
	using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
		string response = streamReader.ReadToEnd();
		// initialize an object to wrap the response
		// A lot of code to create an object response
But, what if instead of use the "traditional" style, we use the Fluent style that make it code easier to read and self-expressive? Then, the same above code would look like this:

JObject jsonRequest = new JObject(new JProperty("dataForRequestBody", someValue));

SomeResponseClass response = WebRequestBuilder<SomeResponseClass>

I implemented some classes and interfaces that allow it. They are fairly simple and easy to understand (I hope).

More samples see Documentation and My Blog

Last edited Jan 22, 2013 at 8:08 PM by tatis, version 5