public void Run() { _service = new SWAPIService(); bool continueToRun = true; while (continueToRun) { Console.Clear(); Console.WriteLine("What would you like to look up?\n\n" + "1. Person\n" + "2. Planet\n" + "3. Vehicle\n" + "4. StarShip\n" + "5. Search People\n" + "6. Search Planets\n" + "7. Exit\n\n"); string choice = Console.ReadLine(); switch (choice) { case "1": GetPerson(); break; case "2": ShowPlanet(); break; case "3": ShowVehicle(); break; case "4": ShowStarShip(); break; case "5": SearchPeople(); break; case "6": SearchPlanets(); break; case "7": continueToRun = false; break; } } }
static void Main(string[] args) { SWAPIService service = new SWAPIService(); SWAPIUI UI = new SWAPIUI(service); UI.Run(); // HeyperText Transfer Protocol // HTML = HyperText Markup Language /* * HttpClient httpClient = new HttpClient(); * * HttpResponseMessage response = * httpClient.GetAsync("https://swapi.dev/api/people/1/").Result; * * HttpResponseMessage planetResponse = * httpClient.GetAsync("https://swapi.dev/api/planets/1/").Result; * // Async means it's happening asynchronously * // (on another timeline) * Person person = response.Content.ReadAsAsync<Person>().Result; * if (response.IsSuccessStatusCode) * { * Planet planet = planetResponse.Content.ReadAsAsync<Planet>().Result; * * Console.WriteLine(response.Content.ReadAsStringAsync().Result); * Console.WriteLine($"\n\n{person.Name} is {person.Height} cm tall and has " + * $"{person.Eye_Color} eyes. {person.Name} is " + * $"from {person.HomeWorld}."); * } * * if (planetResponse.IsSuccessStatusCode) * { * Planet planet = planetResponse.Content.ReadAsAsync<Planet>().Result; * * Console.WriteLine(response.Content.ReadAsStringAsync().Result); * Console.WriteLine($"\n\n{person.Name} is {person.Height} cm tall and has " + * $"{person.Eye_Color} eyes. {person.Name} is " + * $"from {person.HomeWorld}."); * } * * */ }
static void Main(string[] args) { SWAPIService service = new SWAPIService(); SWAPIUI UI = new SWAPIUI(service); UI.Run(); // HyperText Transer Protocol // HTML = HyperText Markup Language HttpClient httpClient = new HttpClient(); HttpResponseMessage response = httpClient.GetAsync("http://swapi.dev/api/people/1/").Result; // Async means it's happening asynchronously // (on another timeline) aka depends on another computer doing something if (response.IsSuccessStatusCode) { // ReadAsString gives us the raw JSON string Console.WriteLine(response.Content.ReadAsStringAsync().Result); } // ReadAsSync converts the JSON to C# POCO Person person = response.Content.ReadAsAsync <Person>().Result; HttpResponseMessage planetResponse = httpClient.GetAsync(person.Homeworld).Result; if (planetResponse.IsSuccessStatusCode) { // POCO Plain Old C# Object Planet planet = planetResponse.Content.ReadAsAsync <Planet>().Result; Console.WriteLine($"\n\n{person.Name} is {person.Height}cm tall and has {person.Eye_Color} eyes. {person.Name} is from {planet.Climate} world of {planet.Name} "); } else { Console.WriteLine($"\n\n{person.Name} is {person.Height} cm tall and has {person.Eye_Color} eyes."); } }
// faux Dependency injection example (we would want to make an interface to enforce consistency between the services if we made more than one) : public SWAPIUI(SWAPIService service) { _service = service; }