summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorseth <[email protected]>2024-04-20 02:31:40 +0000
committerGitHub <[email protected]>2024-04-19 22:31:40 -0400
commit3d07413690c551d9f034c93af85ae8da5a495e14 (patch)
tree517d2e053ebdeb9a3be0ffce6dec36cbc4ce316e /src/api
parent1b92b254bc64b356f5c59657d2f0acc767bb2964 (diff)
spring cleaning (#165)
* treewide: lightly refactor everything * once_cell -> std::sync * remove build.rs we can get our target at runtime * commands::copypasta: refactor selection * drop owo_colors * reactboard: always remove author from count * commands: better handle behavior outside of guilds * ci: garnix -> gha * nix: drop flake-parts & pre-commit-hooks * nix: fix rust flags in derivation * add gha badge to readme * ci: fail when format changes are made * ci: only run on push to main * nix: fix nil script * nix: add libiconv to darwin deps * ci: disable fail-fast * nix: fix actionlint & static checks * ci: add release gates * nix: fix nil check again * ci: give release gates unique names * ci: only build static packages in docker workflow * nix: move dev outputs to subflake * fix some typos * nix: cleanup checks & dev shell * add editorconfig
Diffstat (limited to 'src/api')
-rw-r--r--src/api/guzzle.rs28
-rw-r--r--src/api/mod.rs34
-rw-r--r--src/api/shiggy.rs24
3 files changed, 37 insertions, 49 deletions
diff --git a/src/api/guzzle.rs b/src/api/guzzle.rs
index 437dbe6..c5093da 100644
--- a/src/api/guzzle.rs
+++ b/src/api/guzzle.rs
@@ -1,31 +1,19 @@
-use crate::api::REQWEST_CLIENT;
-
-use eyre::{eyre, Result};
+use eyre::Result;
use log::debug;
-use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
-struct GuzzleResponse {
- pub url: String,
+struct RandomTeawieResponse {
+ url: String,
}
const GUZZLE: &str = "https://api.mydadleft.me";
const RANDOM_TEAWIE: &str = "/random_teawie";
-pub async fn get_random_teawie() -> Result<String> {
- let req = REQWEST_CLIENT
- .get(format!("{GUZZLE}{RANDOM_TEAWIE}"))
- .build()?;
-
- debug!("Making request to {}", req.url());
- let resp = REQWEST_CLIENT.execute(req).await?;
- let status = resp.status();
+pub async fn random_teawie() -> Result<String> {
+ let url = format!("{GUZZLE}{RANDOM_TEAWIE}");
+ debug!("Making request to {url}");
+ let json: RandomTeawieResponse = super::get_json(&url).await?;
- if let StatusCode::OK = status {
- let data: GuzzleResponse = resp.json().await?;
- Ok(data.url)
- } else {
- Err(eyre!("Failed to get random Teawie with {status}"))
- }
+ Ok(json.url)
}
diff --git a/src/api/mod.rs b/src/api/mod.rs
index 6554553..dac9209 100644
--- a/src/api/mod.rs
+++ b/src/api/mod.rs
@@ -1,17 +1,29 @@
-use once_cell::sync::Lazy;
+use std::sync::OnceLock;
+
+use eyre::Result;
+use reqwest::Client;
+use serde::de::DeserializeOwned;
pub mod guzzle;
pub mod shiggy;
-pub static USER_AGENT: Lazy<String> = Lazy::new(|| {
- let version = option_env!("CARGO_PKG_VERSION").unwrap_or("development");
+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())
+}
- format!("teawieBot/{version}")
-});
+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?;
-pub static REQWEST_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| {
- reqwest::Client::builder()
- .user_agent(USER_AGENT.to_string())
- .build()
- .unwrap_or_default()
-});
+ Ok(json)
+}
diff --git a/src/api/shiggy.rs b/src/api/shiggy.rs
index b1d4a87..d6a6238 100644
--- a/src/api/shiggy.rs
+++ b/src/api/shiggy.rs
@@ -1,8 +1,5 @@
-use crate::api::REQWEST_CLIENT;
-
-use eyre::{eyre, Result};
+use eyre::Result;
use log::debug;
-use reqwest::StatusCode;
use serde::Deserialize;
const SHIGGY: &str = "https://safebooru.donmai.us";
@@ -14,19 +11,10 @@ struct SafebooruResponse {
}
#[allow(clippy::module_name_repetitions)]
-pub async fn get_random_shiggy() -> Result<String> {
- let req = REQWEST_CLIENT
- .get(format!("{SHIGGY}{RANDOM_SHIGGY}"))
- .build()?;
-
- debug!("Making request to {}", req.url());
- let resp = REQWEST_CLIENT.execute(req).await?;
- let status = resp.status();
+pub async fn random_shiggy() -> Result<String> {
+ let url = format!("{SHIGGY}{RANDOM_SHIGGY}");
+ debug!("Making request to {url}");
- if let StatusCode::OK = status {
- let data: SafebooruResponse = resp.json().await?;
- Ok(data.file_url)
- } else {
- Err(eyre!("Failed to get random shiggy with {status}"))
- }
+ let resp: SafebooruResponse = super::get_json(&url).await?;
+ Ok(resp.file_url)
}