5

it's easy to request an API with restsharp.org, but when i need to call two different API, first request holds code, and after response second one starts, i think it is not correct, below is my code:

var client = new RestClient("http://xxx.yyy.com/");

var requestHotels = new RestRequest("api/hotelUi/home/hotelList", Method.POST);
requestHotels.AddParameter("take", "16");                                               
IRestResponse hotels = client.Execute(requestHotels); 
List<Hotel> topHotels = JsonConvert.DeserializeObject<List<Hotel>>(hotels.Content); 

var requestCities = new RestRequest("api/hotelUi/home/cityList", Method.POST);
requestCities.AddParameter("take", "16");                                                 
IRestResponse cities = client.Execute(requestCities);
List<City> topCities = JsonConvert.DeserializeObject<List<City>>(cities.Content);    

as you see city request wait until hotel request response, but i think both of them must be send, and wait until both response come back.

how can i do this like?

  • You can use ExecuteAsync or ExecuteTaskAsync methods instead of Execute. – Wokuo Mar 25 '19 at 10:28
  • @Wokuo how? in example there is no multi request, how i understand both of request are done? – mohammad adibi Mar 25 '19 at 10:32
  • 1
    [This is not specifically for RestSharp](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/how-to-make-multiple-web-requests-in-parallel-by-using-async-and-await) but you can apply it. You have to understand async/await in order to use the suggested `ExecuteAsync` method of RestSharp. – Crowcoder Mar 25 '19 at 11:44

1 Answers1

6

The comments are correct, using ExecuteAsync (which can also deserialise the data - see http://restsharp.org/) with Tasks could look something like the following:

// Set up requests as before
var client = new RestClient("http://xxx.yyy.com/");

var requestHotels = new RestRequest("api/hotelUi/home/hotelList", Method.POST);
requestHotels.AddParameter("take", "16");  

var requestCities = new RestRequest("api/hotelUi/home/cityList", Method.POST);
requestCities.AddParameter("take", "16");     

var cancellationTokenSource = new CancellationTokenSource();

var hotelsTask = client.ExecuteTaskAsync<List<Hotel>>(requestHotels, cancellationTokenSource.Token);
var citiesTask = client.ExecuteTaskAsync<List<City>>(requestCities, cancellationTokenSource.Token);

var tasks = new List<Task> { hotelsTask, citiesTask };

// Pause execution here until both tasks are complete
await Task.WhenAll(tasks);

// Check status then use hotelsTask.Result and citiesTask.Result
Greg Stanley
  • 328
  • 1
  • 3
  • 10
  • The best overloaded Add method 'List.Add(Task)' for the collection initializer has some invalid arguments – mohammad adibi Mar 26 '19 at 05:18
  • client.ExecuteTaskAsync(requestHotels , cancellationTokenSource.Token), please complete your answer for others, thank you, https://stackoverflow.com/a/21779724/11254207 – mohammad adibi Mar 26 '19 at 08:38
  • I've changed the example to use the ExecuteAsyncTask approach. I don't have data to test the response but you should be able to inspect the two task objects and find what you need. – Greg Stanley Mar 26 '19 at 10:41