/// <summary> /// Recursive method responsible for navigation between API pages and deserialization of data. /// </summary> /// <param name="url">API access url</param> /// <param name="starships">List of Ships that will be added to each page found in the API</param> private static void BuildResult(string url, List <StarshipModel> starships) { if (string.IsNullOrEmpty(url)) { return; } HttpResponseMessage message = null; try { message = HttpClient.GetAsync(url).GetAwaiter().GetResult(); } catch (Exception ex) { Console.WriteLine($"An error occurred. {ex}"); } string result = message.Content.ReadAsStringAsync().GetAwaiter().GetResult(); StarshipResultModel apiResult = null; try { apiResult = JsonConvert.DeserializeObject <StarshipResultModel>(result); } catch (JsonReaderException ejs) { Console.WriteLine($"An error occurred. {ejs}"); } starships.AddRange(apiResult.Results); if (apiResult.Next != null) { BuildResult(apiResult.Next, starships); } }
public static StarshipResultModel GetStarshipResults() { StarshipResultModel starships = _client.Execute <StarshipResultModel>(new RestRequest("https://swapi.co/api/starships", Method.GET))?.Data; if (starships?.results == null) { return(starships); } while (!string.IsNullOrWhiteSpace(starships?.next)) { StarshipResultModel newStarships = _client.Execute <StarshipResultModel>(new RestRequest(starships.next, Method.GET))?.Data; if (newStarships?.results == null) { continue; } starships.results.AddRange(newStarships.results); starships.next = newStarships.next; } return(starships); }