public void FromJson() { //Prepare var obt = CityInfo.FromJson(jsonTest); //Verify obt.Should().BeEquivalentTo(new CityInfo { Coord = new Coord { Lat = -18.92, Lon = -48.28 }, Weather = new[] { new Weather { Description = "clear sky", Icon = "01n", Id = 800L, Main = "Clear" } }, Base = "stations", Main = new Main { Humidity = 49L, Pressure = 1023L, Temp = 294.15, TempMax = 294.15, TempMin = 294.15 }, Visibility = 10000L, Wind = new Wind { Deg = 30L, Speed = 3.1 }, Clouds = new Clouds { All = 0L }, Dt = 1566949302L, Sys = new Sys { Country = "BR", Id = 8471L, Message = 0.0063, Sunrise = 1566897924L, Sunset = 1566939870L, Type = 1L }, Timezone = -10800L, Id = 3445831L, Name = "Uberlandia", Cod = 200L }); }
public async Task <double> GetTemperatureByCoord(float lat, float lon) { var request = new HttpRequestMessage(HttpMethod.Get, $"http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={Config.Value.weather}"); var client = ClientFactory.CreateClient(); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var json = await response.Content .ReadAsStringAsync(); var info = CityInfo.FromJson(json); var farTemp = info.Main.Temp; //(32°F − 32) × 5/9 = 0°C var celTemp = (farTemp - 32) * 5 / 9; return(celTemp); } else { throw new ApplicationException(response.StatusCode.ToString()); } }
public async Task <double> GetTemperatureByCityName(string cityname) { var request = new HttpRequestMessage(HttpMethod.Get, $"http://api.openweathermap.org/data/2.5/weather?q={cityname}&appid={Config.Value.weather}"); var client = ClientFactory.CreateClient(); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var json = await response.Content .ReadAsStringAsync(); var info = CityInfo.FromJson(json); var kelTemp = info.Main.Temp; //From Kelvin to Celsius var celTemp = kelTemp - 273.15; return(celTemp); } else { throw new ApplicationException(response.StatusCode.ToString()); } }