Exemplo n.º 1
0
        public ActionResult GetCovid19Data()
        {
            var covid19 = new Covid19ViewModel();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://api.covid19api.com/");
                try
                {
                    var response =
                        client.GetAsync("summary?fbclid=IwAR0OKbr_6W79uEpZ5HExsnOie2NdJCu7hSev_JQgYV4SM5KiM9A5LVCTyDs").Result;

                    if (response.IsSuccessStatusCode)
                    {
                        covid19 = response.Content.ReadAsAsync <Covid19ViewModel>().Result;             // install-package Microsoft.AspNet.WebApi.Client is needed for ReadAsAsync<>
                    }
                    else
                    {
                        ViewBag.Message = " server error";
                    }
                }
                catch (Exception ex)
                {
                    GlobalViewModel globalViewModel = new GlobalViewModel();
                    globalViewModel.TotalDeaths  = 0;
                    globalViewModel.NewConfirmed = 0;
                    globalViewModel.NewDeaths    = 0;
                    covid19.Global = globalViewModel;
                    Console.WriteLine("Missing data..");
                }
            }

            return(PartialView("_GetCovid19Data", covid19));
        }
Exemplo n.º 2
0
        public ActionResult GetCovid19Data( )
        {
            var covid19 = new Covid19ViewModel();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://api.covid19api.com/");

                var response =
                    client.GetAsync("summary?fbclid=IwAR0OKbr_6W79uEpZ5HExsnOie2NdJCu7hSev_JQgYV4SM5KiM9A5LVCTyDs").Result;



                if (response.IsSuccessStatusCode)
                {
                    covid19 = response.Content.ReadAsAsync <Covid19ViewModel>().Result;             // install-package Microsoft.AspNet.WebApi.Client is needed for ReadAsAsync<>
                }
                else
                {
                    ViewBag.Message = " server error";
                }
            }


            return(PartialView("_GetCovid19Data", covid19));
        }
Exemplo n.º 3
0
        public Covid19Model Get(int airportId)
        {
            AirportModel airport = new AirportModel();

            using (var connection = GetOpenConnection())
            {
                airport = connection.Query <AirportModel>(
                    @"select AirportId, AirportName, CityName, CountryName, AirportCode, Latitude, Longitude
                            from Airports where AirportId = " + airportId).FirstOrDefault();//<= TODO: use parameters
            }


            Covid19ViewModel covidData = new Covid19ViewModel();
            //TODO: add link to config
            string         url = "https://covid-193.p.rapidapi.com/statistics?country=" + airport.CountryName;
            string         answer;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            //TODO: add these to config
            request.Headers.Add("x-rapidapi-key: 7a9ab3b348mshfd960b72abb93f5p1b5cbajsn7b6c5b1140b7");
            request.Headers.Add("x-rapidapi-host: covid-193.p.rapidapi.com");
            request.Headers.Add("useQueryString: true");

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        answer = reader.ReadToEnd();
                    }

            covidData = JsonConvert.DeserializeObject <Covid19ViewModel>(answer);
            Covid19Model model = new Covid19Model();

            covidData.Response.ForEach(c =>
                                       model = new Covid19Model
            {
                CountryName = c.Country,
                Continent   = c.Continent,
                Cases       = c.Cases.Total == null ? 0 : (int)c.Cases.Total,
                NewCases    = c.Cases.New == null ? 0 : int.Parse(c.Cases.New.Split('+')[1]),
                Deaths      = c.Deaths.Total == null ? 0 : (int)c.Deaths.Total,
                NewDeaths   = c.Deaths.New == null ? 0 : int.Parse(c.Deaths.New.Split('+')[1]),
                Recoverd    = c.Cases.Recovered == null ? 0 : (int)c.Cases.Recovered,
                Tests       = c.Tests.Total == null ? 0 : (int)c.Tests.Total,
                Updated     = c.Time
            }
                                       );

            return(model);
        }