From a4a9353e1c8f902b7d7b3cf74e3e5b129c214330 Mon Sep 17 00:00:00 2001 From: seth Date: Mon, 10 Jul 2023 00:18:36 -0400 Subject: start using poise --- src/api/guzzle.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'src/api/guzzle.rs') 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 { + 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::().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::().await { + Ok(data) => Ok(data.url), + Err(why) => Err(Box::new(why)), + } + } else { + Err(resp.status().to_string().into()) } } -- cgit v1.2.3