diff options
| author | uku <[email protected]> | 2023-10-13 18:31:21 +0200 |
|---|---|---|
| committer | seth <[email protected]> | 2023-10-13 13:05:37 -0400 |
| commit | dec178a883769e221c8da3c125f83c6512b733e3 (patch) | |
| tree | 2c83398eea4375a12ba3e6583ec43ae3a6bf3070 /src | |
| parent | 05e0831f7926dd4afda8e72c6ea5aa7a466b6bea (diff) | |
move client & logic into api module
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/guzzle.rs | 9 | ||||
| -rw-r--r-- | src/api/mod.rs | 10 | ||||
| -rw-r--r-- | src/api/shiggy.rs | 29 | ||||
| -rw-r--r-- | src/commands/random_shiggy.rs | 31 |
4 files changed, 42 insertions, 37 deletions
diff --git a/src/api/guzzle.rs b/src/api/guzzle.rs index b58f323..a13243d 100644 --- a/src/api/guzzle.rs +++ b/src/api/guzzle.rs @@ -1,4 +1,4 @@ -use once_cell::sync::Lazy; +use crate::api::REQWEST_CLIENT; use reqwest::StatusCode; use serde::{Deserialize, Serialize}; @@ -9,13 +9,6 @@ struct GuzzleResponse { const GUZZLE: &str = "https://api.mydadleft.me"; -pub static REQWEST_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| { - reqwest::Client::builder() - .user_agent("teawieBot/0.1.0") - .build() - .unwrap_or_default() -}); - pub async fn get_random_teawie() -> String { let endpoint = "get_random_teawie"; let req = REQWEST_CLIENT diff --git a/src/api/mod.rs b/src/api/mod.rs index c2ad56f..6224ab1 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1 +1,11 @@ +use once_cell::sync::Lazy; + pub mod guzzle; +pub mod shiggy; + +pub static REQWEST_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| { + reqwest::Client::builder() + .user_agent("teawieBot/0.1.0") + .build() + .unwrap_or_default() +}); diff --git a/src/api/shiggy.rs b/src/api/shiggy.rs new file mode 100644 index 0000000..0e9fd19 --- /dev/null +++ b/src/api/shiggy.rs @@ -0,0 +1,29 @@ +use crate::api::REQWEST_CLIENT; +use reqwest::StatusCode; +use serde::Deserialize; + +const URL: &str = "https://safebooru.donmai.us/posts/random.json?tags=kemomimi-chan_(naga_u)+naga_u&only=file_url"; +const ERROR_MSG: &str = "couldn't get a shiggy"; + +#[derive(Deserialize)] +struct SafebooruResponse { + file_url: String, +} + +pub async fn get_random_shiggy() -> String { + let resp = match REQWEST_CLIENT + .execute(REQWEST_CLIENT.get(URL).build().unwrap()) + .await + { + Ok(r) => r, + Err(e) => return format!("{} ({:?})", ERROR_MSG, e), + }; + + match resp.status() { + StatusCode::OK => match resp.json::<SafebooruResponse>().await { + Ok(sr) => sr.file_url, + Err(e) => format!("{} ({:?})", ERROR_MSG, e), + }, + other => format!("{} ({:?})", ERROR_MSG, other), + } +} diff --git a/src/commands/random_shiggy.rs b/src/commands/random_shiggy.rs index b1f1511..c6aa6de 100644 --- a/src/commands/random_shiggy.rs +++ b/src/commands/random_shiggy.rs @@ -1,36 +1,9 @@ -use crate::api::guzzle::REQWEST_CLIENT; -use reqwest::StatusCode; -use serde::Deserialize; +use crate::api::shiggy::get_random_shiggy; use serenity::builder::CreateApplicationCommand; use serenity::model::prelude::application_command::CommandDataOption; -const URL: &str = "https://safebooru.donmai.us/posts/random.json?tags=kemomimi-chan_(naga_u)+naga_u&only=file_url"; -const ERROR_MSG: &str = "couldn't get a shiggy"; - -#[derive(Deserialize)] -struct SafebooruResponse { - file_url: String, -} - pub async fn run(_: &[CommandDataOption]) -> String { - let resp = match REQWEST_CLIENT - .execute(REQWEST_CLIENT.get(URL).build().unwrap()) - .await - { - Ok(r) => r, - Err(e) => return format!("{} ({:?})", ERROR_MSG, e), - }; - - match resp.status() { - StatusCode::OK => match resp.json::<SafebooruResponse>().await { - Ok(sr) => sr.file_url, - Err(e) => format!("{} ({:?})", ERROR_MSG, e), - }, - other => { - println!("{}", resp.text().await.unwrap()); - format!("{} ({:?})", ERROR_MSG, other) - } - } + get_random_shiggy().await } pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { |
