summaryrefslogtreecommitdiff
path: root/src/http/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/http/mod.rs')
-rw-r--r--src/http/mod.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs
new file mode 100644
index 0000000..0f16852
--- /dev/null
+++ b/src/http/mod.rs
@@ -0,0 +1,43 @@
+use eyre::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!(
+ "teawie-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)
+ }
+}