Exemplo n.º 1
0
        public IDayWeather GetCurrentWeather(Coordinates coord)
        {
            IDayWeather currentWeather = this.history.getCurrentWeatherResponse(coord);

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

            var baseUrl = "https://api.openweathermap.org";
            var url     = "data/2.5/weather?" +
                          $"lat={coord.Lat}&lon={coord.Lon}&appid={this.Key}";

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(baseUrl);
                client.DefaultRequestHeaders.Add("User-Agent", "Anything");
                client.DefaultRequestHeaders.Accept
                .Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var response = client.GetAsync(url).Result;
                response.EnsureSuccessStatusCode();
                currentWeather = response.Content.ReadAsAsync <OwmCurrentDayWeather>().Result;
            }

            this.history.addCurrentWeatherResponse(coord, currentWeather);

            return(currentWeather);
        }
Exemplo n.º 2
0
        public void addCurrentWeatherResponse(Coordinates coord, IDayWeather response)
        {
            var key = coord.ToString();

            this.CurrentWeatherHistory.Remove(key);
            var val = new OwmApiCurrentWeatherResponse();

            val.Date  = DateTime.Now;
            val.Value = response;
            this.CurrentWeatherHistory.Add(key, val);
        }