diff options
| author | seth <[email protected]> | 2023-11-30 22:18:51 -0500 |
|---|---|---|
| committer | seth <[email protected]> | 2023-12-01 07:12:49 -0500 |
| commit | 76c0f94e6d7aa108424b34826eb7d8514b026287 (patch) | |
| tree | 7315bd6dfe52c158041bed64ba39781718a69335 /src/api | |
| parent | db52e639b85d79bed870020aec7a045851ca5ee3 (diff) | |
feat: use eyre, better logging, & refactor
small commits be damned
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/guzzle.rs | 30 | ||||
| -rw-r--r-- | src/api/mod.rs | 6 | ||||
| -rw-r--r-- | src/api/shiggy.rs | 30 |
3 files changed, 22 insertions, 44 deletions
diff --git a/src/api/guzzle.rs b/src/api/guzzle.rs index 17d2c0c..83c159e 100644 --- a/src/api/guzzle.rs +++ b/src/api/guzzle.rs @@ -1,6 +1,6 @@ use crate::api::REQWEST_CLIENT; -use crate::Error; +use color_eyre::eyre::{eyre, Result}; use log::*; use reqwest::StatusCode; use serde::{Deserialize, Serialize}; @@ -11,32 +11,20 @@ struct GuzzleResponse { } const GUZZLE: &str = "https://api.mydadleft.me"; +const RANDOM_TEAWIE: &str = "/random_teawie"; -pub async fn get_random_teawie() -> Result<String, Error> { - let endpoint = "/get_random_teawie"; - +pub async fn get_random_teawie() -> Result<String> { let req = REQWEST_CLIENT - .get(format!("{GUZZLE}{endpoint}")) - .build() - .unwrap(); + .get(format!("{GUZZLE}{RANDOM_TEAWIE}")) + .build()?; info!("making request to {}", req.url()); - let resp = REQWEST_CLIENT.execute(req).await.unwrap(); + let resp = REQWEST_CLIENT.execute(req).await?; let status = resp.status(); if let StatusCode::OK = status { - match resp.json::<GuzzleResponse>().await { - Ok(data) => Ok(data.url), - Err(why) => { - if let Some(url) = why.url() { - error!("error parsing json from {}! {}", url, why) - } else { - error!("couldn't even get the url! {}", why); - } - - Err(Box::new(why)) - } - } + let data = resp.json::<GuzzleResponse>().await?; + Ok(data.url) } else { error!( "couldn't fetch random teawie from {}! {}", @@ -44,6 +32,6 @@ pub async fn get_random_teawie() -> Result<String, Error> { status ); - Err(status.to_string().into()) + Err(eyre!("failed to get random teawie with {status}")) } } diff --git a/src/api/mod.rs b/src/api/mod.rs index a1e0e97..2ce664e 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -3,11 +3,13 @@ use once_cell::sync::Lazy; pub mod guzzle; pub mod shiggy; -pub const USER_AGENT: &str = "teawieBot/0.1.0"; +pub const USER_AGENT: &str = "teawieBot/"; pub static REQWEST_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| { + let version = option_env!("CARGO_PKG_VERSION").unwrap_or("development"); + reqwest::Client::builder() - .user_agent(USER_AGENT) + .user_agent(format!("{USER_AGENT}/{version}")) .build() .unwrap_or_default() }); diff --git a/src/api/shiggy.rs b/src/api/shiggy.rs index 8dbadef..7a582ee 100644 --- a/src/api/shiggy.rs +++ b/src/api/shiggy.rs @@ -1,42 +1,30 @@ use crate::api::REQWEST_CLIENT; -use crate::Error; +use color_eyre::eyre::{eyre, Result}; use log::*; use reqwest::StatusCode; use serde::Deserialize; const SHIGGY: &str = "https://safebooru.donmai.us"; +const RANDOM_SHIGGY: &str = "/posts/random.json?tags=kemomimi-chan_(naga_u)+naga_u&only=file_url"; #[derive(Deserialize)] struct SafebooruResponse { file_url: String, } -pub async fn get_random_shiggy() -> Result<String, Error> { - let endpoint = "/posts/random.json?tags=kemomimi-chan_(naga_u)+naga_u&only=file_url"; - +pub async fn get_random_shiggy() -> Result<String> { let req = REQWEST_CLIENT - .get(format!("{SHIGGY}{endpoint}")) - .build() - .unwrap(); + .get(format!("{SHIGGY}{RANDOM_SHIGGY}")) + .build()?; info!("making request to {}", req.url()); - let resp = REQWEST_CLIENT.execute(req).await.unwrap(); + let resp = REQWEST_CLIENT.execute(req).await?; let status = resp.status(); if let StatusCode::OK = status { - match resp.json::<SafebooruResponse>().await { - Ok(data) => Ok(data.file_url), - Err(why) => { - if let Some(url) = why.url() { - error!("failed to make a request to {}! {}", url, why) - } else { - error!("couldn't even figure out the url! {}", why) - }; - - Err(Box::new(why)) - } - } + let data = resp.json::<SafebooruResponse>().await?; + Ok(data.file_url) } else { error!( "couldn't fetch random teawie from {}! {}", @@ -44,6 +32,6 @@ pub async fn get_random_shiggy() -> Result<String, Error> { status ); - Err(status.to_string().into()) + Err(eyre!("failed to get random teawie with {status}")) } } |
