From 76c0f94e6d7aa108424b34826eb7d8514b026287 Mon Sep 17 00:00:00 2001 From: seth Date: Thu, 30 Nov 2023 22:18:51 -0500 Subject: feat: use eyre, better logging, & refactor small commits be damned --- src/api/guzzle.rs | 30 +++++++++--------------------- src/api/mod.rs | 6 ++++-- src/api/shiggy.rs | 30 +++++++++--------------------- 3 files changed, 22 insertions(+), 44 deletions(-) (limited to 'src/api') 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 { - let endpoint = "/get_random_teawie"; - +pub async fn get_random_teawie() -> Result { 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::().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::().await?; + Ok(data.url) } else { error!( "couldn't fetch random teawie from {}! {}", @@ -44,6 +32,6 @@ pub async fn get_random_teawie() -> Result { 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 = 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 { - let endpoint = "/posts/random.json?tags=kemomimi-chan_(naga_u)+naga_u&only=file_url"; - +pub async fn get_random_shiggy() -> Result { 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::().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::().await?; + Ok(data.file_url) } else { error!( "couldn't fetch random teawie from {}! {}", @@ -44,6 +32,6 @@ pub async fn get_random_shiggy() -> Result { status ); - Err(status.to_string().into()) + Err(eyre!("failed to get random teawie with {status}")) } } -- cgit v1.2.3