Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of RestSharp ASYNC client.ExecuteAsync<T> () works

Tags:

c#

restsharp

Could someone please help me modify the code below:

client.ExecuteAsync(request, response => {
    Console.WriteLine(response.Content);
});

Basically I want to use ExecuteAsync method above but don't want to print but return response.Content to the caller.

Is there any easy way to achieve this?

I tried this but doesnt' work:

    public T Execute<T>(RestRequest request) where T : new()
        {
            var client = new RestClient();
            client.BaseUrl = BaseUrl;
            client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
            request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment); // used on every request
            var response = client.ExecuteAsync(request, response => {
    return response.data);
});

}

The above code is from https://github.com/restsharp/RestSharp

like image 607
Nil Pun Avatar asked Sep 02 '12 01:09

Nil Pun


People also ask

How do I use ExecuteAsync?

To do this, add one or more ExecuteAsync message requests to an ExecuteMultiple message request. Due to throttling restrictions that improve overall system performance, only one message running asynchronously is allowed to execute at a time for each organization.

Is RestSharp asynchronous?

RestSharp is an open-source HTTP Client library that we can use to consume APIs. Based on that, we can install it using NuGet Package Manager. Although RestSharp can call any API using the HTTP protocol, the purpose of RestSharp is to consume the REST APIs. RestSharp supports both synchronous and asynchronous requests.

How can I put my body in my RestSharp?

RestSharp supports sending XML or JSON body as part of the request. To add a body to the request, simply call AddJsonBody or AddXmlBody method of the IRestRequest instance. There is no need to set the Content-Type or add the DataFormat parameter to the request when using those methods, RestSharp will do it for you.

What is RestSharp client?

Definition of RestSharp RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.


1 Answers

There's the thing... you can't return an asynchronously delivered value, because your calling method will already have returned. Blocking the caller until you have a result defeats the point of using ExecuteAsync. In this case, I'd return a Task<string> (assuming response.Content is a string):

Task<string> GetResponseContentAsync(...)
{
  var tcs=new TaskCompletionSource<string>();
  client.ExecuteAsync(request, response => {
    tcs.SetResult(response.Content);
  });
  return tcs.Task;
}

Now, when the task completes, you have a value. As we move to c#5 async/await, you should get used to stating asynchrony in terms of Task<T> as it's pretty core.

http://msdn.microsoft.com/en-us/library/dd537609.aspx

http://msdn.microsoft.com/en-us/library/hh191443.aspx

like image 148
spender Avatar answered Oct 21 '22 22:10

spender