Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Async and RestSharp for Windows Phone 7

DZone's Guide to

Async and RestSharp for Windows Phone 7

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

I have mentioned RestSharp quite a lot of times, it is simply an essential library for building applications that communicate to the outside world. Ever since Async CTP shipped for Windows Phone 7, I wanted to make everything asynchronous. RestSharp comes with asynchronous methods already built in, but they are not compatible with Async since they don’t return Task.

But we can fix that easily with our good friend TaskCompletionSource<T>

The idea is simple: create a TaskCompletionSource instance which will capture our asynchronous task and use regular ExecuteAsync method. Once it completes, it will transition the task to either Faulted state by setting the exception or to Completed state by setting the result.

Let’s convert ExecuteAsync method first:

public static Task<IRestResponse> ExecuteTaskAsync(this RestClient @this, RestRequest request)
{
    if (@this == null)
        throw new NullReferenceException();

    var tcs = new TaskCompletionSource<IRestResponse>();

    @this.ExecuteAsync(request, (response) =>
    {
        if (response.ErrorException != null)
            tcs.TrySetException(response.ErrorException);
        else
            tcs.TrySetResult(response);
    });

    return tcs.Task;
}

Looks good enough, what about generic method ExecuteAsync<T> that deserializes the content? Here we have a dilemma: what should the return type be? I have decided to return deserialized instance of type T i.e. theresponse.Data property directly instead of the full response, but you can change the code easily if you want to return the response instead. Here is the snippet:

public static Task<T> ExecuteTaskAsync<T>(this RestClient @this, RestRequest request)
    where T : new()
{
    if (@this == null)
        throw new NullReferenceException();

    var tcs = new TaskCompletionSource<T>();

    @this.ExecuteAsync<T>(request, (response) =>
    {
        if (response.ErrorException != null)
            tcs.TrySetException(response.ErrorException);
        else
            tcs.TrySetResult(response.Data);
    });

    return tcs.Task;
}

You can now use await method with RestSharp libraries. Happy coding.


Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}