Async and RESTSharp

tl;tr; Examples of using Async method of RESTSharp, and comments

As I mentioned last time I was forced to use async architecture because Windows Phone require it. I was newbie in async programming but when I met RESTSharp, it clear everything, I will show you some code block with my comment for it. Basics, and how to connect to Web API check this blogpost

If we want execute GET.

We have to remember that async method is runs in brand new thread! First we have to set REST request, type here your api path, and what method, for this case it will be GET. r.Data is deserialised List of Locations.

If we want execute POST

Here we fill body of our REST request, we add new object which will provide information that we want to POST.

If we want execute DELETE

Now we have to add parameter to URL so our request for example will be “http://example.com/api/location/1”, and method DELETE

If we want to update (PUT)

In this case we have to provide object which will replace old one, by using of AddObject method. And finally we can execute our request.

Most of you will ask me, how works this part. If we want to execute method ExecuteAsync<T>(). As a first argument we have to provide request. Second argument is CallBack – what we want to do when data will be received, so we can put here some MessageBox, event for logger or simply bind our given data.

In next episode we will be checking, is there any possibility to mix REST a XNA,

Introduction to RESTSharp

I would like to show you, good library to working with REST architecture. Nowadays REST is getting more popular because it’s very simple API, even ASP.NET MVC support this as default API service.

RESTSharp, is powerful library for any kind of .NET technology. Especially for Windows Phone where you have to use REST or SOAP to communicate with your external data. I came across RESTSharp when I was working on Project for Imagine Cup 2013. It saves me, a lot of time. RestSharp support asynchronously actions as well as windows phone because it is the main presumption of programming on that platform. I would like to show you few code block of using RESTSharp. At the begging basics and then in the next blog post some advance methods of using library with async.

First, we have to make a connection with server where API is located. Then we add physical path to specific API controller, and choose method of connection (GET, POST, DELETE, PUT etc.). Next step is create a query to our REST API services, our result will be list of Item, it’s highly recommended to use that type deserialization, now we can easily get access just adding .Data at the end of statement.

Now let’s look at Method.POST.

client and request stay same like in first example, but there is one change in method of connection, please set Method.POST. Next step is add body to our request create model that is expected in API controller, and finally last line, execute our request.

If we want run delete method, just use Method.DELETE

Declaring client stay same like in other cases, request has small change, first you have to add path with parameter and define method of connection Method.GET. Moreover, we have to add to request one more property, define parameter {id} and what values its get. Finally execute request.