Created
January 13, 2012 17:34
-
-
Save DVDPT/1607680 to your computer and use it in GitHub Desktop.
RestSharp Async for windows phone 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class RestSharpExtensions | |
{ | |
public static Task<IRestResponse<T>> ExecuteAsync<T>(this IRestClient client, IRestRequest request) where T : new() | |
{ | |
return client.ExecuteAsync<T>(request, CancellationToken.None); | |
} | |
public static Task<IRestResponse<T>> ExecuteAsync<T>(this IRestClient client, IRestRequest request, CancellationToken token) where T : new() | |
{ | |
var taskCompletionSource = new TaskCompletionSource<IRestResponse<T>>(); | |
RestRequestAsyncHandle asyncHandle = null; | |
token.Register(() => | |
{ | |
if (asyncHandle != null) | |
asyncHandle.Abort(); | |
taskCompletionSource.TrySetCanceled(); | |
}); | |
if (!token.IsCancellationRequested) | |
asyncHandle = client.ExecuteAsync(request, (IRestResponse<T> response, RestRequestAsyncHandle handle) => taskCompletionSource.TrySetResult(response)); | |
else | |
taskCompletionSource.TrySetCanceled(); | |
return taskCompletionSource.Task; | |
} | |
public static Task<IRestResponse> ExecuteAsync(this IRestClient client, IRestRequest request) | |
{ | |
return client.ExecuteAsync(request, CancellationToken.None); | |
} | |
public static Task<IRestResponse> ExecuteAsync(this IRestClient client, IRestRequest request, CancellationToken token) | |
{ | |
var taskCompletionSource = new TaskCompletionSource<IRestResponse>(); | |
RestRequestAsyncHandle asyncHandle = null; | |
token.Register(() => | |
{ | |
if (asyncHandle != null) | |
asyncHandle.Abort(); | |
taskCompletionSource.TrySetCanceled(); | |
}); | |
if (!token.IsCancellationRequested) | |
asyncHandle = client.ExecuteAsync(request, (response, handle) => taskCompletionSource.TrySetResult(response)); | |
else | |
taskCompletionSource.TrySetCanceled(); | |
return taskCompletionSource.Task; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment