Exemple #1
0
        static void Main(string[] args)
        {
            var           url          = "http://localhost:50938/api/search/";
            List <string> theProgLangs = args.ToList();

            Console.WriteLine();
            Console.Write("The results of programming languages search fight are: ");
            Console.WriteLine();
            SearchApiClient apiClient     = new SearchApiClient(url);
            SearchManager   searchManager = new SearchManager();

            try
            {
                var fullResults = searchManager.BuildResults(theProgLangs, apiClient);
                if (fullResults.Count > 0)
                {
                    ShowResults(fullResults);
                    Console.WriteLine();
                    ShowWinnersBySearchEngine(searchManager.BuildResultsBySearchEngine(fullResults));
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("No programming languages were provided for searching.");
                }
            }
            catch
            {
                Console.WriteLine();
                Console.WriteLine("An error ocurred during the search contest");
            }

            Console.ReadLine();
        }
        /// <summary>
        /// Builds the list of search results for all the programming languages.
        /// </summary>
        /// <param name="progLangs">List of programming languages for searching and building the search results</param>
        /// <param name="searchApiClient">Proxy Http client to invoke Search Api</param>
        /// <returns>Results of search by programming language</returns>
        public Dictionary <string, Dictionary <string, long> > BuildResults(List <string> progLangs, SearchApiClient searchApiClient)
        {
            Dictionary <string, Dictionary <string, long> > fullResults = new Dictionary <string, Dictionary <string, long> >();

            try
            {
                foreach (var progLang in progLangs)
                {
                    HttpResponseMessage response = searchApiClient.Client.GetAsync(searchApiClient.Url + progLang).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var result         = response.Content.ReadAsStringAsync().Result;
                        var progLangResult = JsonConvert.DeserializeObject <Dictionary <string, long> >(result);
                        fullResults.Add(progLang, progLangResult);
                    }
                }
            }
            catch
            {
                throw;
            }

            return(fullResults);
        }