Search code examples
c#restwindows-phone-7.1restsharp

RestSharp synchronous request in windows phone


Is there a way (any, no matter how) to simulate synchronous requests using restsharp?

I'm developing an app which has to wait for a successful login response to navigate forward, it is a pain to be passing callbacks all over my code just to check stuff.


Solution

  • Use the Microsoft.Bcl.Async package.

    Then with an extension method like:

    public static class RestClientExtensions
        {
            public static Task<IRestResponse> ExecuteTask (this IRestClient restClient, RestRequest restRequest)
            {
                var tcs = new TaskCompletionSource<IRestResponse> ();
                restClient.ExecuteAsync (restRequest, (restResponse, asyncHandle) =>
                {
                    if (restResponse.ResponseStatus == ResponseStatus.Error)
                        tcs.SetException (restResponse.ErrorException);
                    else
                        tcs.SetResult (restResponse);
                });
                return tcs.Task;
            }
        }
    

    You can make calls like:

    var restResponse = await restClient.ExecuteTask(restRequest);