diff options
Diffstat (limited to 'src/api/shiggy.rs')
| -rw-r--r-- | src/api/shiggy.rs | 30 |
1 files changed, 9 insertions, 21 deletions
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}")) } } |
