diff options
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/guzzle.rs | 28 | ||||
| -rw-r--r-- | src/api/shiggy.rs | 27 |
2 files changed, 27 insertions, 28 deletions
diff --git a/src/api/guzzle.rs b/src/api/guzzle.rs index a13243d..6d1e41b 100644 --- a/src/api/guzzle.rs +++ b/src/api/guzzle.rs @@ -1,4 +1,6 @@ use crate::api::REQWEST_CLIENT; +use crate::Error; + use reqwest::StatusCode; use serde::{Deserialize, Serialize}; @@ -9,22 +11,22 @@ struct GuzzleResponse { const GUZZLE: &str = "https://api.mydadleft.me"; -pub async fn get_random_teawie() -> String { - let endpoint = "get_random_teawie"; +pub async fn get_random_teawie() -> Result<String, Error> { + let endpoint = "/get_random_teawie"; + let req = REQWEST_CLIENT - .get(format!("{GUZZLE}/{endpoint}")) + .get(format!("{GUZZLE}{endpoint}")) .build() .unwrap(); - let resp = REQWEST_CLIENT.execute(req).await.unwrap(); // why did i have to own - // this constant? i have - // no idea! - let err_msg = "couldn't get a teawie"; - match resp.status() { - StatusCode::OK => match resp.json::<GuzzleResponse>().await { - Ok(data) => data.url, - Err(why) => format!("{} ({:?})", err_msg, why), - }, - other => format!("{} ({:?})", err_msg, other), + let resp = REQWEST_CLIENT.execute(req).await.unwrap(); + + if let StatusCode::OK = resp.status() { + match resp.json::<GuzzleResponse>().await { + Ok(data) => Ok(data.url), + Err(why) => Err(Box::new(why)), + } + } else { + Err(resp.status().to_string().into()) } } diff --git a/src/api/shiggy.rs b/src/api/shiggy.rs index 0e9fd19..97895d9 100644 --- a/src/api/shiggy.rs +++ b/src/api/shiggy.rs @@ -1,29 +1,26 @@ use crate::api::REQWEST_CLIENT; +use crate::Error; 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 ERROR_MSG: &str = "couldn't get a shiggy"; #[derive(Deserialize)] struct SafebooruResponse { file_url: String, } -pub async fn get_random_shiggy() -> String { - let resp = match REQWEST_CLIENT - .execute(REQWEST_CLIENT.get(URL).build().unwrap()) - .await - { - Ok(r) => r, - Err(e) => return format!("{} ({:?})", ERROR_MSG, e), - }; +pub async fn get_random_shiggy() -> Result<String, Error> { + let req = REQWEST_CLIENT.get(URL).build().unwrap(); - match resp.status() { - StatusCode::OK => match resp.json::<SafebooruResponse>().await { - Ok(sr) => sr.file_url, - Err(e) => format!("{} ({:?})", ERROR_MSG, e), - }, - other => format!("{} ({:?})", ERROR_MSG, other), + let resp = REQWEST_CLIENT.execute(req).await.unwrap(); + + if let StatusCode::OK = resp.status() { + match resp.json::<SafebooruResponse>().await { + Ok(data) => Ok(data.file_url), + Err(why) => Err(Box::new(why)), + } + } else { + Err(resp.status().to_string().into()) } } |
