コード例 #1
0
        public WeatherData GetWeather(string zipCode)
        {
            String uri = $"http://api.openweathermap.org/data/2.5/weather?units={Units}&zip={zipCode},us&appid={ApiKey}";

            HttpClient httpClient   = new HttpClient();
            String     responseJson = httpClient.GetStringAsync(uri).Result;

            OpenWeatherApiResponse response    = JsonConvert.DeserializeObject <OpenWeatherApiResponse>(responseJson);
            WeatherData            weatherData = MapResponse(response);

            return(weatherData);
        }
コード例 #2
0
        internal WeatherData MapResponse(OpenWeatherApiResponse response)
        {
            WeatherData weatherData = new WeatherData()
            {
                DataProvider         = "Open Weather API",
                ObservationTime      = DateTimeOffset.FromUnixTimeSeconds(response.dt).UtcDateTime.ToLocalTime(),
                Location             = response.name,
                CurrentConditions    = response.weather.FirstOrDefault()?.main,
                Temperature          = response.main.temp,
                Humidity             = response.main.humidity,
                Pressure             = response.main.pressure,
                WindSpeed            = response.wind.speed,
                WindDirection        = CompassDirection.Decode(response.wind.deg),
                WindDirectionDegrees = response.wind.deg
            };

            return(weatherData);
        }