diff options
| author | seth <[email protected]> | 2023-07-10 00:18:36 -0400 |
|---|---|---|
| committer | seth <[email protected]> | 2023-11-16 00:35:07 +0000 |
| commit | a4a9353e1c8f902b7d7b3cf74e3e5b129c214330 (patch) | |
| tree | b58da1d30af52e97c0251e0d6882cd0ccdfeb20a /src/api/guzzle.rs | |
| parent | 5e9ec7f008e01d25c0b7f782c5ae043bc9ca0933 (diff) | |
start using poise
Diffstat (limited to 'src/api/guzzle.rs')
| -rw-r--r-- | src/api/guzzle.rs | 28 |
1 files changed, 15 insertions, 13 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()) } } |
