diff options
| author | seth <[email protected]> | 2024-05-27 04:55:45 -0400 |
|---|---|---|
| committer | seth <[email protected]> | 2024-05-27 04:56:48 -0400 |
| commit | c69eea2f4823da476628742fbbec600ee95ac049 (patch) | |
| tree | 7cf3d87f5f202e6049ba44a06ac6fe9d3558826b /src/http/mod.rs | |
initial commit
Diffstat (limited to 'src/http/mod.rs')
| -rw-r--r-- | src/http/mod.rs | 44 |
1 files changed, 44 insertions, 0 deletions
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<Response, Error>; + async fn get_json<T: DeserializeOwned>(&self, url: &str) -> Result<T, Error>; +} + +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<Response, Error> { + trace!("Making GET 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, Error> { + let resp = self.get_request(url).await?; + let json = resp.json().await?; + Ok(json) + } +} |
