diff options
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/guzzle.rs | 23 | ||||
| -rw-r--r-- | src/api/shiggy.rs | 33 |
2 files changed, 48 insertions, 8 deletions
diff --git a/src/api/guzzle.rs b/src/api/guzzle.rs index 6d1e41b..17d2c0c 100644 --- a/src/api/guzzle.rs +++ b/src/api/guzzle.rs @@ -1,6 +1,7 @@ use crate::api::REQWEST_CLIENT; use crate::Error; +use log::*; use reqwest::StatusCode; use serde::{Deserialize, Serialize}; @@ -19,14 +20,30 @@ pub async fn get_random_teawie() -> Result<String, Error> { .build() .unwrap(); + info!("making request to {}", req.url()); let resp = REQWEST_CLIENT.execute(req).await.unwrap(); + let status = resp.status(); - if let StatusCode::OK = resp.status() { + if let StatusCode::OK = status { match resp.json::<GuzzleResponse>().await { Ok(data) => Ok(data.url), - Err(why) => Err(Box::new(why)), + 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)) + } } } else { - Err(resp.status().to_string().into()) + error!( + "couldn't fetch random teawie from {}! {}", + resp.url(), + status + ); + + Err(status.to_string().into()) } } diff --git a/src/api/shiggy.rs b/src/api/shiggy.rs index 97895d9..8dbadef 100644 --- a/src/api/shiggy.rs +++ b/src/api/shiggy.rs @@ -1,9 +1,11 @@ use crate::api::REQWEST_CLIENT; use crate::Error; + +use log::*; 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 SHIGGY: &str = "https://safebooru.donmai.us"; #[derive(Deserialize)] struct SafebooruResponse { @@ -11,16 +13,37 @@ struct SafebooruResponse { } pub async fn get_random_shiggy() -> Result<String, Error> { - let req = REQWEST_CLIENT.get(URL).build().unwrap(); + let endpoint = "/posts/random.json?tags=kemomimi-chan_(naga_u)+naga_u&only=file_url"; + + let req = REQWEST_CLIENT + .get(format!("{SHIGGY}{endpoint}")) + .build() + .unwrap(); + info!("making request to {}", req.url()); let resp = REQWEST_CLIENT.execute(req).await.unwrap(); + let status = resp.status(); - if let StatusCode::OK = resp.status() { + if let StatusCode::OK = status { match resp.json::<SafebooruResponse>().await { Ok(data) => Ok(data.file_url), - Err(why) => Err(Box::new(why)), + 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)) + } } } else { - Err(resp.status().to_string().into()) + error!( + "couldn't fetch random teawie from {}! {}", + resp.url(), + status + ); + + Err(status.to_string().into()) } } |
