Ejemplo n.º 1
0
        // Added as specified by requirements, but changed to version below for efficiency

        /*
         * public async Task<IEnumerable<Covid19ApiDataModel>> GetDataAsync()
         * {
         *
         *  var covid19ApiConfig = new Covid19ApiConfig();
         *  _configuration.GetSection("Covid19ApiConfig").Bind(covid19ApiConfig);
         *
         *  List<Covid19ApiDataModel> covid19ApiDataModels = new List<Covid19ApiDataModel>();
         *  using (HttpClient httpClient = new HttpClient())
         *  {
         *      var responseString = await httpClient.GetStringAsync($"{covid19ApiConfig.BaseUrl}{covid19ApiConfig.RequestUrl}");
         *      covid19ApiDataModels = JsonConvert.DeserializeObject<Covid19ApiDataModel>(responseString);
         *  }
         *
         *
         *  covid19ApiDataModels[0].Countries.ForEach(async (covid19ApiDataModel) =>
         *  {
         *
         *      var location = _locator.GetLocationCovid19Api(covid19ApiDataModel.CountryCode);
         *      covid19ApiDataModel.Location = location == null ? new LocationDataModel() { Latitude = 0, Longitude = 0 } : location;
         *
         *  });
         *
         *  return covid19ApiDataModels;
         * }
         */


        public async Task <Covid19ApiDataModel> GetDataAsync()
        {
            var covid19ApiConfig = new Covid19ApiConfig();

            _configuration.GetSection("Covid19ApiConfig").Bind(covid19ApiConfig);

            Covid19ApiDataModel covid19ApiDataModels = new Covid19ApiDataModel();

            using (HttpClient httpClient = new HttpClient())
            {
                var responseString = await httpClient.GetStringAsync($"{covid19ApiConfig.BaseUrl}{covid19ApiConfig.RequestUrl}");

                covid19ApiDataModels = JsonConvert.DeserializeObject <Covid19ApiDataModel>(responseString);
            }


            covid19ApiDataModels.Countries.ForEach(async(covid19ApiDataModel) =>
            {
                var location = _locator.GetLocationCovid19Api(covid19ApiDataModel.CountryCode);
                covid19ApiDataModel.Location = location == null ? new LocationDataModel()
                {
                    Latitude = 0, Longitude = 0
                } : location;
            });

            return(covid19ApiDataModels);
        }