summaryrefslogtreecommitdiff
path: root/src/api/mod.rs
blob: dac92090772de021be944e2fa9046f31f6e76d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::sync::OnceLock;

use eyre::Result;
use reqwest::Client;
use serde::de::DeserializeOwned;

pub mod guzzle;
pub mod shiggy;

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())
}

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?;

	Ok(json)
}