use crate::common::Lang;
pub trait API {
const BASE: &'static str;
const DATATYPE: &'static str;
#[must_use]
fn url(lang: Lang) -> String {
format!(
"https://data.weather.gov.hk/weatherAPI/opendata/{}.php?dataType={}&lang={}",
Self::BASE,
Self::DATATYPE,
lang
)
}
}
macro_rules! impl_api {
($t:ty, $b:ident, $d:ident) => {
impl crate::fetch::API for $t {
const BASE: &'static str = stringify!($b);
const DATATYPE: &'static str = stringify!($d);
}
};
}
pub(crate) use impl_api;
#[cfg(feature = "fetch")]
#[doc(cfg(feature = "fetch"))]
pub async fn fetch<T>(lang: Lang) -> anyhow::Result<T>
where
T: API + serde::de::DeserializeOwned,
{
use reqwest::get;
Ok(serde_json::from_str(
&get(T::url(lang)).await?.text().await?,
)?)
}