open-meteo-cli/src/open_meteo.rs

22 lines
837 B
Rust
Raw Normal View History

2023-06-08 23:10:21 +02:00
use crate::{
coords::Coordinates,
current_weather::CurrentWeather,
speed_unit::{speed_to_string, SpeedUnit},
temp_unit::{temp_to_string, TempUnit},
};
2023-06-08 23:10:21 +02:00
pub fn request_current_weather(
coords: &Coordinates,
temp_unit: &TempUnit,
speed_unit: &SpeedUnit,
) -> Result<CurrentWeather, ureq::Error> {
let url = format!(
2023-06-08 23:10:21 +02:00
"https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&temperature_unit={}&windspeed_unit={}&current_weather=true",
coords.latitude, coords.longitude, temp_to_string(temp_unit), speed_to_string(speed_unit)
);
let mut body: serde_json::Value = ureq::get(&url).call()?.into_json()?;
let current_weather = body["current_weather"].take();
let current_weather: CurrentWeather = serde_json::from_value(current_weather).unwrap();
Ok(current_weather)
}