Exemple #1
0
        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}.");
             * }
             *
             *
             */
        }
Exemple #2
0
        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.");
            }
        }