/// <summary> /// Base Constructor for the manager /// </summary> public AssetPropertyRetriever() { _swapi = RestService.For <IStarWarsApi>("https://swapi.co/api"); _returnedValues = new List <string>(); _addValuesLock = new object(); _typeDictionary = new Dictionary <string, Action <Film, string> > { { "characters", ProcessPeople }, { "planets", ProcessPlanets }, { "starships", ProcessStarships } }; }
/// <summary> /// Activity 8.2: this overload accepts where and orderBy delegates. /// Ideally, this should be reworked into the GetAllAsync method so that it has the same filter and sort options /// </summary> public static async Task <IEnumerable <Planet> > GetAllPlanetsAsync(this IStarWarsApi api, Func <Planet, bool> where = null, Func <Planet, object> orderBy = null) { var results = await GetAllPlanetsAsync(api); if (where != null) { results = results.Where(where); } if (orderBy != null) { results = results.OrderBy(orderBy); } return(results); }
/// <summary> /// more generic way to get all pages of any endpoint that accepts a "page" argument /// </summary> private static async Task <IEnumerable <T> > GetAllAsync <T>(this IStarWarsApi api, Func <IStarWarsApi, int, Task <ApiResult <List <T> > > > invoke) { List <T> results = new List <T>(); int page = 0; do { page++; ApiResult <List <T> > result = await invoke(api, page); results.AddRange(result.Data); if (result.Next == null) { break; } } while (true); return(results); }
/// <summary> /// this initial version of GetAllPlanetsAsync used a dedicated implementation not reusable with other methods, /// but we're keeping it because it was an acceptable solution before implementing other API endpoints /// </summary> public static async Task <IEnumerable <Planet> > GetAllPlanetsAsyncOld(this IStarWarsApi api) { List <Planet> results = new List <Planet>(); int page = 0; do { page++; ApiResult <List <Planet> > result = await api.GetPlanetsAsync(page); results.AddRange(result.Data); if (result.Next == null) { break; } } while (true); return(results); }
public static async Task <IEnumerable <Film> > GetAllFilmsAsync(this IStarWarsApi api) => await GetAllAsync(api, (api, page) => api.GetFilmsAsync(page));
public static async Task <IEnumerable <Person> > GetAllPeopleAsync(this IStarWarsApi api) => await GetAllAsync(api, (api, page) => api.GetPeopleAsync(page));
/// <summary> /// this uses our more generic GetAllAsync method. Use this pattern going forward /// </summary> public static async Task <IEnumerable <Planet> > GetAllPlanetsAsync(this IStarWarsApi api) => await GetAllAsync(api, (api, page) => api.GetPlanetsAsync(page));
public StarWarsCoolService() { var settings = new RefitSettings(); Api = RestService.For <IStarWarsApi>("https://swapi.dev/api"); }