summaryrefslogtreecommitdiff
path: root/src/http/mod.rs
blob: dde769c54e39d428fc7b61ed011c08c765029df5 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use anyhow::Result;
use log::trace;
use serde::de::DeserializeOwned;

pub mod shiggy;
pub mod teawie;

pub type Client = reqwest::Client;
pub type Response = reqwest::Response;

/// Primary extensions for HTTP Client
pub trait Ext {
	async fn get_request(&self, url: &str) -> Result<Response>;
	async fn get_json<T: DeserializeOwned>(&self, url: &str) -> Result<T>;
	fn default() -> Self;
}

impl Ext for Client {
	fn default() -> Self {
		reqwest::ClientBuilder::new()
			.user_agent(format!(
				"chill-discord-bot/{}",
				option_env!("CARGO_PKG_VERSION").unwrap_or("development")
			))
			.build()
			.unwrap()
	}

	async fn get_request(&self, url: &str) -> Result<Response> {
		trace!("Making request to {url}");
		let resp = self.get(url).send().await?;
		resp.error_for_status_ref()?;

		Ok(resp)
	}

	async fn get_json<T: DeserializeOwned>(&self, url: &str) -> Result<T> {
		let resp = self.get_request(url).await?;
		let json = resp.json().await?;

		Ok(json)
	}
}