public DataController(IStarWarsApiClient starWarsApiClient) => this.starWarsApiClient = starWarsApiClient;
/// <summary> /// version 2 of the function. For each starship that meets the criteria this will start a new async task to /// get that pilots data. When all of the tasks complete for the given starship then it will yield return the /// required string output then it will move to the next sharship. /// </summary> /// <param name="client">inversion of control</param> /// <param name="passengers"></param> /// <param name="displayFunc">this will allow you to specify how to display the results</param> /// <returns></returns> public static IEnumerable <string> GetStarshipPilotCombosByPassengersVersion2(IStarWarsApiClient client, int passengers, Func <Starship, Person, string> displayFunc = null) { int parsedPassengers = 0; var starships = client.GetStarships(x => Int32.TryParse(x.Passengers, out parsedPassengers) && parsedPassengers >= passengers); //getstarships will handle the paging for me. it will yield return each pages results then move on to the next page var pilotTasks = new List <Task <Person> >(); foreach (var starship in starships) { pilotTasks.Clear(); foreach (var pilot in starship.Pilots) { pilotTasks.Add(Task.Run(() => client.GetPersonAsync(pilot))); //here we are going to fire off a task for each pilot for this starship } Task.WaitAll(pilotTasks.ToArray()); //when we have data on all of the pilots for this starship foreach (var task in pilotTasks) { yield return(displayFunc != null ? displayFunc(starship, task.Result) : $"{starship.Name} - {task.Result.Name}"); //yield return the pilots for this starship } } }
public StarWarsApiController(ILogger <StarWarsApiController> logger) { _logger = logger; _starWarsCient = RestService.For <IStarWarsApiClient>("https://swapi.co/api/"); }
/// <summary> /// version 1 of the function. This effectively undoes all of my work to make everything as async as possible. /// for each of the starships it will lookup each of those pilots 1 at a time and it will block and wait for /// each pilot to complete before moving on. /// </summary> /// <param name="client">inversion of control</param> /// <param name="passengers"></param> /// <param name="displayFunc">this will allow you to specify how to display the results</param> /// <returns></returns> public static IEnumerable <string> GetStarshipPilotCombosByPassengersVersion1(IStarWarsApiClient client, int passengers, Func <Starship, Person, string> displayFunc = null) { int parsedPassengers = 0; var starships = client.GetStarships(x => Int32.TryParse(x.Passengers, out parsedPassengers) && parsedPassengers >= passengers); //getstarships will handle the paging for me. it will yield return each pages results then move on to the next page foreach (var starship in starships) { foreach (var pilot in starship.Pilots) { var pilotResult = (client.GetPersonAsync(pilot)).Result; yield return(displayFunc != null ? displayFunc(starship, pilotResult) : $"{starship.Name} - {pilotResult.Name}"); //the .Result is going to block and wait on this pilot to finish loading before it yield returns and moves on to the next pilot. } } }