async public Task <List <Planet> > GetPlanetsAsync() { var planets = new List <Planet>(); var swapiUrl = $"{SwapiUrl}/planets/"; if (GetOneSwapiPageAtATime) { while (swapiUrl != null) { string jsonResult = await HttpService.GetResponseAsJson(swapiUrl); var swapiPlanetsResponse = JsonConvert.DeserializeObject <SwapiPlanetsResponse>(jsonResult); planets.AddRange(swapiPlanetsResponse.Results); swapiUrl = swapiPlanetsResponse.Next; } } else { // Helper conversion function - takes an HttpResponse string and, assuming it is an SwapiPlanetsResponse, deserializes it Func <string, SwapiPlanetsResponse> extractPlanetsFromResponse = response => { return(JsonConvert.DeserializeObject <SwapiPlanetsResponse>(response)); }; // Get pages concurrently (as much as possible) List <string> allResponses = await HttpService.GetAllResponsePagesAsJson(swapiUrl, response => { return(extractPlanetsFromResponse(response).Count); }); // Extract the planets from the page responses allResponses.ForEach(r => { planets.AddRange(extractPlanetsFromResponse(r).Results); }); //// Here we assume the Star Wars API is fixed (with page size 10) and therefore we can use concurrent data retrieval. //// The first page tells us how many pages there are. //const int pageSize = 10; //string firstPageResponse = await HttpService.GetResponseAsJson(swapiUrl); //var swapiPlanetsResponse = JsonConvert.DeserializeObject<SwapiPlanetsResponse>(firstPageResponse); //var pageCount = swapiPlanetsResponse.Count / pageSize; //if (swapiPlanetsResponse.Count % pageSize > 0) //{ // pageCount += 1; //} //var urls = new List<string>(); //for (var pageNumber = 2; pageNumber <= pageCount; pageNumber++) //{ // urls.Add($"{swapiUrl}?page={pageNumber}"); //} //var tasks = urls.Select(url => HttpService.GetResponseAsJson(url)); //var responses = await Task.WhenAll(tasks); //// Add very first response (page 1) //var completeList = responses.ToList(); //completeList.Add(firstPageResponse); //completeList // .ForEach(r => planets.AddRange(JsonConvert.DeserializeObject<SwapiPlanetsResponse>(r).Results)); } return(planets); }