Exemple #1
0
        static async Task Main(string[] args)
        {
            ApiHelper.InitializeApiClient();
            string anotherLookUp = "";

            do
            {
                Console.WriteLine("Enter an name");
                string searchTerm = Console.ReadLine();

                try
                {
                    ResultModel res = await ResultProcessor.GetStarWarsRootinfo(searchTerm);

                    Console.WriteLine($"{res.Count } Matches found");
                    for (int i = 0; i < res.Count; i++)
                    {
                        if (res.Count != 0)
                        {
                            Console.WriteLine($"{res.results[i].FullName}");
                            Console.WriteLine($"{res.results[i].Gender}");

                            string[]     speciesUrl   = res.results[i].Species;
                            SpeciesModel speciesmodel = await SpeciesProcessor.GetStarWarsSpeciesInfo(speciesUrl[0]);

                            Console.WriteLine(speciesmodel.SpeciesName);


                            string[] movies = res.results[i].Movies;
                            for (i = 0; i < movies.Length; i++)
                            {
                                MovieModel movieModel = await MovieProcessor.GetStarWarsMovieInfo(movies[i]);

                                Console.WriteLine(movieModel.MovieName);
                            }
                        }

                        else
                        {
                            Console.WriteLine("No matches found.");
                        }
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                }
                Console.WriteLine("Look up another person Yes/No: ");
                anotherLookUp = Console.ReadLine();
                Console.Clear();
            } while (anotherLookUp.ToLower() == "yes");
        }
        /// <summary>
        /// Makes a request to the SWAPI api for information about a Star Wars characters species
        /// Maps and returns requested information as an instance of SpeciesModel
        /// </summary>
        /// <param name="url">the url that points to the species information of a star wars character on the SWAPI Api</param>
        /// <returns></returns>
        public static async Task <SpeciesModel> GetStarWarsSpeciesInfo(string url)
        {
            SpeciesModel cached = SpeciesCache.Where(x => x.SpeciesUrl == url).FirstOrDefault();

            if (cached != null)
            {
                return(cached);
            }

            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    SpeciesModel output = await response.Content.ReadAsAsync <SpeciesModel>();

                    SpeciesCache.Add(output);
                    return(output);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }