diff options
Diffstat (limited to 'src/api/mod.rs')
| -rw-r--r-- | src/api/mod.rs | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/src/api/mod.rs b/src/api/mod.rs index 6554553..dac9209 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,17 +1,29 @@ -use once_cell::sync::Lazy; +use std::sync::OnceLock; + +use eyre::Result; +use reqwest::Client; +use serde::de::DeserializeOwned; pub mod guzzle; pub mod shiggy; -pub static USER_AGENT: Lazy<String> = Lazy::new(|| { - let version = option_env!("CARGO_PKG_VERSION").unwrap_or("development"); +pub fn client() -> &'static Client { + static USER_AGENT: OnceLock<String> = OnceLock::new(); + static CLIENT: OnceLock<Client> = OnceLock::new(); + + let user_agent = USER_AGENT.get_or_init(|| { + let version = option_env!("CARGO_PKG_VERSION").unwrap_or("development"); + + format!("teawieBot/{version}") + }); + + CLIENT.get_or_init(|| Client::builder().user_agent(user_agent).build().unwrap()) +} - format!("teawieBot/{version}") -}); +async fn get_json<T: DeserializeOwned>(url: &str) -> Result<T> { + let resp = client().get(url).send().await?; + resp.error_for_status_ref()?; + let json = resp.json().await?; -pub static REQWEST_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| { - reqwest::Client::builder() - .user_agent(USER_AGENT.to_string()) - .build() - .unwrap_or_default() -}); + Ok(json) +} |
