/// <summary> /// Request all available star ships from the Api /// </summary> public void Api_StarshipColler() { try { StarshipListModel starshipListModel = _apiHelper.GetStarshipList(); StarshipListModel starshipListModelFail = null; Console.WriteLine("\nAvailable star ships count:" + starshipListModel.StarshipList.Count + "\n"); var valid = false; double distance = 0; while (!valid) { Console.WriteLine("Enter the requested input distance in mega lights (MGLT), or enter 0 as input to print the default distances for each star ship can reach with one supply"); var val = Console.ReadLine(); valid = !string.IsNullOrWhiteSpace(val) && val.All(c => c >= '0' && c <= '9'); if (valid) { double.TryParse(val, out distance); } } Console.WriteLine("\n"); foreach (var starship in starshipListModel.StarshipList) { int countMglt = 0; var validApiData = int.TryParse(starship.Mglt, out countMglt); if (validApiData) { var countResupply = StarshipResupplyCalculater(starship, distance); if (distance == 0) { Console.WriteLine("Name: " + starship.Name + ",\t Distance : " + countResupply + " MGLT"); } else { Console.WriteLine("Name: " + starship.Name + ",\t Resupply : " + countResupply); } } else { if (starshipListModelFail == null) { starshipListModelFail = new StarshipListModel(); starshipListModelFail.StarshipList = new System.Collections.Generic.List <Starship>(); } starshipListModelFail.StarshipList.Add(starship); } } if (starshipListModelFail != null) { var countStarshipFail = starshipListModelFail.StarshipList.Count; Console.WriteLine("\nFailed to calculate " + countStarshipFail + (countStarshipFail > 1 ? " star ship:" : " star ships:")); for (int i = 0; i < countStarshipFail; i++) { Console.WriteLine("Name: " + starshipListModelFail.StarshipList[i].Name); } } } catch (Exception exception) { Console.WriteLine(exception); } Console.WriteLine("\n\nDo you want to try again?\nPres Enter to retry or Escape to exit application.\n"); ConsoleKeyInfo consoleKeyInfo; consoleKeyInfo = Console.ReadKey(); if (consoleKeyInfo.Key == ConsoleKey.Escape) { Environment.Exit(0); } Api_StarshipColler(); }
/// <summary> /// Request all star ships from the SWAPI /// </summary> /// <returns>model contains list of star ships</returns> public StarshipListModel GetStarshipList() { try { int countPage = 1; int countTotal = 0; StarshipListModel starshipListModel = new StarshipListModel(); starshipListModel.StarshipList = new List <Starship>(); do { string requestMethod = Constant.StarshipGetAllTheResources; if (countPage > 1) { requestMethod = Constant.StarshipGetAllTheResourcesPage; requestMethod += countPage; } countPage++; var apiResponse_StarshipsList = GetResponseFromSwapi <ApiResponse_Starships>(requestMethod); if (apiResponse_StarshipsList == null) { throw new Exception("SWAPI is not reachable !\n"); } else if (apiResponse_StarshipsList.Count == 0) { throw new Exception("SWAPI did not provide me with data !\n"); } foreach (var item in apiResponse_StarshipsList.Results) { starshipListModel.StarshipList.Add( new Starship { Name = item.Name, Model = item.Model, StarshipClass = item.Starship_Class, Manufacturer = item.Manufacturer, CostInCredits = item.Cost_In_Credits, Crew = item.Crew, Passengers = item.Passengers, MaxAtmospheringSpeed = item.Max_Atmosphering_Speed, HyperdriveRating = item.Hyperdrive_Rating, Mglt = item.Mglt, CargoCapacity = item.Cargo_Capacity, Consumables = item.Consumables, FilmList = item.Films, PilotList = item.Pilots, DateCreate = item.Created, DateEdit = item.Edited, }); } if (countTotal > 0) { countTotal = countTotal - apiResponse_StarshipsList.Results.Count(); } else { countTotal = apiResponse_StarshipsList.Count - apiResponse_StarshipsList.Results.Count(); } }while (countTotal > 0); return(starshipListModel); } catch (Exception) { throw; } }