From c69eea2f4823da476628742fbbec600ee95ac049 Mon Sep 17 00:00:00 2001 From: seth Date: Mon, 27 May 2024 04:55:45 -0400 Subject: initial commit --- src/http/mod.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/http/mod.rs (limited to 'src/http/mod.rs') diff --git a/src/http/mod.rs b/src/http/mod.rs new file mode 100644 index 0000000..fa60d67 --- /dev/null +++ b/src/http/mod.rs @@ -0,0 +1,44 @@ +use serde::de::DeserializeOwned; +use tracing::trace; + +mod github; + +pub use github::*; + +pub type Client = reqwest::Client; +pub type Response = reqwest::Response; +pub type Error = reqwest::Error; + +/// Fun trait for functions we use with [Client] +pub trait HttpClientExt { + fn default() -> Self; + async fn get_request(&self, url: &str) -> Result; + async fn get_json(&self, url: &str) -> Result; +} + +impl HttpClientExt for Client { + fn default() -> Self { + reqwest::Client::builder() + .user_agent(format!( + "nixpkgs-tracker-bot/{}", + option_env!("CARGO_PKG_VERSION").unwrap_or_else(|| "development") + )) + .build() + .unwrap() + } + + async fn get_request(&self, url: &str) -> Result { + trace!("Making GET request to {url}"); + + let resp = self.get(url).send().await?; + resp.error_for_status_ref()?; + + Ok(resp) + } + + async fn get_json(&self, url: &str) -> Result { + let resp = self.get_request(url).await?; + let json = resp.json().await?; + Ok(json) + } +} -- cgit v1.2.3