コード例 #1
0
        /// <summary>
        /// Get Current Weather using a zip code
        /// </summary>
        /// <param name="zip">Zip code for current weather</param>
        /// <param name="countryCode">Optional: Country Code corresponding to zip.  US by default.</param>
        /// <param name="temperatureUnit">Temperature Unit - Imperial by default.</param>
        public async Task <CurrentWeatherResponse> GetCurrentWeatherByZip(string zip, string countryCode = "us", TemperatureUnit temperatureUnit = TemperatureUnit.Imperial)
        {
            if (string.IsNullOrEmpty(zip))
            {
                throw new ArgumentNullException(nameof(zip));
            }

            var parameters = new Dictionary <string, string>()
            {
                { "zip", $"{zip},{countryCode}" },
                { "units", temperatureUnit.ToString() }
            };

            HttpResponseMessage response;

            using (_client)
            {
                response = await _client.SendAsync(new HttpRequestMessage(HttpMethod.Get, BuildUri(@"https://api.openweathermap.org/data/2.5/weather", parameters)));
            }

            if (response.IsSuccessStatusCode)
            {
                return(CurrentWeatherResponse.FromJson(await response.Content.ReadAsStringAsync()));
            }

            throw new Exception(await response.Content.ReadAsStringAsync());
        }
コード例 #2
0
ファイル: WeatherService.cs プロジェクト: xamcat/xvts
 public Task <Weather> GetWeatherAsync(string city, TemperatureUnit unit = TemperatureUnit.Imperial)
 {
     return(GetAsync <Weather>($"api/forecasts/{city}?units={unit.ToString().ToLower()}", requestMessage =>
     {
         requestMessage.Headers.Add("key", "B50996A0-9D60-44AF-BF08-81029CE2B8C7");
     }));
 }
コード例 #3
0
        public Task <Forecast> GetForecastAsync(string city, TemperatureUnit unit = TemperatureUnit.Metric, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (string.IsNullOrWhiteSpace(city))
            {
                throw new ArgumentException(nameof(city));
            }

            return(GetAsync <Forecast>($"forecasts/{city}?units={unit.ToString()}", cancellationToken, SetApiKeyHeader));
        }
コード例 #4
0
ファイル: UnitUtility.cs プロジェクト: wind101net/atmo
        public static string GetFriendlyName(TemperatureUnit unit)
        {
            switch (unit)
            {
            case TemperatureUnit.Celsius: return("C");

            case TemperatureUnit.Fahrenheit: return("F");

            default: return(unit.ToString());
            }
        }
コード例 #5
0
        public string ToString(CultureInfo ci)
        {
            string tz;

            if (UseLocalTime)
            {
                tz = WxDateUnit.TimeZoneName(true, 0);
            }
            else
            {
                tz = UtcOffset.ToString(ci);
            }
            string sep = ci.TextInfo.ListSeparator;

            return(string.Format(CultureInfo.InvariantCulture,
                                 "{0}{8}{1}{8}{2}{8}{3}{8}{4}{8}{5}{8}{6}{8}{7}",
                                 tz, Temperature.ToString(),
                                 TemperatureRate.ToString(),
                                 Pressure.ToString(), Wind.ToString(),
                                 Rain.ToString(), RainRate.ToString(),
                                 "", sep));
        }
コード例 #6
0
        /// <summary>
        /// Get Current Weather using longitude and latitude
        /// </summary>
        /// <param name="longitude">Longitude</param>
        /// <param name="latitude">Latitude</param>
        /// <param name="temperatureUnit">Temperature Unit - Imperial by default.</param>
        /// <returns></returns>
        public async Task <CurrentWeatherResponse> GetCurrentWeatherByCoords(double longitude, double latitude, TemperatureUnit temperatureUnit = TemperatureUnit.Imperial)
        {
            var parameters = new Dictionary <string, string>()
            {
                { "lat", $"{latitude}" },
                { "lon", $"{longitude}" },
                { "units", temperatureUnit.ToString() }
            };

            HttpResponseMessage response;

            using (_client)
            {
                response = await _client.SendAsync(new HttpRequestMessage(HttpMethod.Get, BuildUri(@"https://api.openweathermap.org/data/2.5/weather", parameters)));
            }

            if (response.IsSuccessStatusCode)
            {
                return(CurrentWeatherResponse.FromJson(await response.Content.ReadAsStringAsync()));
            }

            throw new Exception(await response.Content.ReadAsStringAsync());
        }
コード例 #7
0
ファイル: TemperatureTests.cs プロジェクト: mp9007/UnitsNet
        public void TemperatureDeltaPlusTemperatureEqualsTemperature(TemperatureUnit unit, int deltaVal, int temperatureVal, string expected)
        {
            Temperature      temperature = Temperature.From(temperatureVal, unit);
            TemperatureDelta delta       = TemperatureDelta.From(deltaVal, (TemperatureDeltaUnit)Enum.Parse(typeof(TemperatureDeltaUnit), unit.ToString()));

            // Act
            Temperature resultTemp = delta + temperature;

            string actual = resultTemp.ToUnit(unit).ToString(CultureInfo.InvariantCulture, "{0:0} {1}");

            Assert.Equal(expected, actual);
        }
コード例 #8
0
 public Task <Forecast> GetForecastAsync(double latitude, double longitude, TemperatureUnit unit = TemperatureUnit.Metric, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(GetAsync <Forecast>($"forecasts/coordinates?latitude={latitude.ToString()}&longitude={longitude.ToString()}&units={unit.ToString()}", cancellationToken, SetApiKeyHeader));
 }
コード例 #9
0
ファイル: UnitUtility.cs プロジェクト: fparaggio/atmo
 public static string GetFriendlyName(TemperatureUnit unit)
 {
     switch (unit) {
     case TemperatureUnit.Celsius: return "C";
     case TemperatureUnit.Fahrenheit: return "F";
     default: return unit.ToString();
     }
 }
コード例 #10
0
ファイル: WeatherService.cs プロジェクト: xamcat/xvts
 public Task <Weather> GetForecastAsync(int cityId, TemperatureUnit unit = TemperatureUnit.Imperial)
 {
     return(GetAsync <Weather>($"ForecastByCityId?cityId={cityId}&units={unit.ToString().ToLower()}"));
 }
コード例 #11
0
ファイル: WeatherService.cs プロジェクト: xamcat/xvts
 public Task <Weather> GetWeatherAsync(string city, TemperatureUnit unit = TemperatureUnit.Imperial)
 {
     return(GetAsync <Weather>($"WeatherByCity?city={city}&unit={unit.ToString().ToLower()}"));
 }