summaryrefslogtreecommitdiff
path: root/src/api/shiggy.rs
diff options
context:
space:
mode:
authorseth <[email protected]>2023-11-16 23:19:15 -0500
committerseth <[email protected]>2023-11-18 01:42:03 +0000
commit86318aa4aa8a7547e6398342a0c7b609e307dd5d (patch)
tree789cbf510249b65a3fe5c434c282dad24d8cf489 /src/api/shiggy.rs
parentea085cd27e2058d5aed9afe51545d0c76b3f5df9 (diff)
chore: improve error handling and logging for api requests
Diffstat (limited to 'src/api/shiggy.rs')
-rw-r--r--src/api/shiggy.rs33
1 files changed, 28 insertions, 5 deletions
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())
}
}