use chrono::{DateTime, FixedOffset};
use serde::Deserialize;
use crate::common::Lang;
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Response {
pub obs_time: DateTime<FixedOffset>,
pub hourly_rainfall: Vec<HourlyRainfall>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HourlyRainfall {
pub automatic_weather_station: String,
#[serde(rename = "automaticWeatherStationID")]
pub automatic_weather_station_id: String,
#[serde(deserialize_with = "crate::internal::deserialize::deserialize_to_rainfall_value")]
pub value: RainfallValue,
pub unit: String,
}
#[derive(Clone, Debug)]
pub enum RainfallValue {
UnderMaintenance,
Rainfall(u32),
}
#[must_use]
pub fn url(lang: &Lang) -> String {
format!("https://data.weather.gov.hk/weatherAPI/opendata/hourlyRainfall.php?lang={lang}")
}
#[allow(clippy::missing_errors_doc)]
#[cfg(feature = "fetch")]
#[cfg_attr(docsrs, doc(cfg(feature = "fetch")))]
pub async fn fetch(lang: &Lang) -> anyhow::Result<Response> {
let client = reqwest::Client::builder().build()?;
fetch_with_client(lang, client).await
}
#[allow(clippy::missing_errors_doc)]
#[cfg(feature = "fetch")]
#[cfg_attr(docsrs, doc(cfg(feature = "fetch")))]
pub async fn fetch_with_client(lang: &Lang, client: reqwest::Client) -> anyhow::Result<Response> {
Ok(client.get(url(lang)).send().await?.json().await?)
}
#[cfg(feature = "test")]
#[cfg(test)]
mod test;