/// <summary> /// Get all the starship resources /// </summary> public async Task <List <Starship> > GetAllStarshipsAsync(string pageNumber = "1") { List <Starship> ships = new List <Starship>(); bool isLastPage = false; try { do { StarshipResults result = await GetAllPaginatedAsync <Starship>("/starships/", pageNumber); ships.AddRange(result.results); if (string.IsNullOrEmpty(result.nextPageNo)) { isLastPage = true; } else { pageNumber = result.nextPageNo; } } while (isLastPage == false); return(ships); } catch (Exception) { throw; } }
/// <summary> /// Get all starship resources by page /// </summary> private async Task <StarshipResults> GetAllPaginatedAsync <T>(string entityName, string pageNumber = "1") { try { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("page", pageNumber); StarshipResults result = await GetMultipleAsync <T>(entityName, parameters); result.nextPageNo = String.IsNullOrEmpty(result.next) ? null : GetQueryParameters(result.next)["page"]; result.previousPageNo = String.IsNullOrEmpty(result.previous) ? null : GetQueryParameters(result.previous)["page"]; return(result); } catch (Exception) { throw; } }
private async Task <StarshipResults> GetMultipleAsync <T>(string endpoint, Dictionary <string, string> parameters) { try { string serializedParameters = ""; if (parameters != null) { serializedParameters = "?" + SerializeDictionary(parameters); } string json = await RequestAsync( string.Format("{0}{1}{2}", apiUrl, endpoint, serializedParameters), HttpMethod.GET, data : null, isProxyEnabled : false); StarshipResults swapiResponse = JsonConvert.DeserializeObject <StarshipResults>(json); return(swapiResponse); } catch (Exception) { throw; } }