From 5e40f2cf5217906c849f46ab25a8b14e18a31d11 Mon Sep 17 00:00:00 2001 From: gak Date: Wed, 30 Aug 2023 14:26:01 +1000 Subject: [PATCH] feat: slightly cleaner tracing output --- src/lib.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fa1aa4c..5684ede 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ use derive_more::{Display, FromStr}; use reqwest::Client; use serde::{Deserialize, Serialize}; use std::fmt::{Debug, Display}; -use tracing::{instrument, trace}; +use tracing::debug; pub mod auth; pub mod calendar_history; @@ -68,7 +68,6 @@ impl Api { } } - #[instrument(skip(self))] async fn get(&self, url: &str) -> Result, TeslatteError> where D: for<'de> Deserialize<'de> + Debug, @@ -76,29 +75,25 @@ impl Api { self.request(&RequestData::GET { url }).await } - #[instrument(skip(self))] async fn post(&self, url: &str, body: S) -> Result, TeslatteError> where S: Serialize + Debug, { - // let request_context = || format!("POST {url} {payload}"); - let payload = &serde_json::to_string(&body).expect("Should not fail creating the request struct."); - let request_data = RequestData::POST { url, payload }; let data = self.request::(&request_data).await?; - if data.data.result { - Ok(data) - } else { - Err(TeslatteError::ServerError { + if !data.data.result { + return Err(TeslatteError::ServerError { request: format!("{request_data}"), msg: data.data.reason, description: None, body: Some(data.body), - }) + }); } + + Ok(data) } async fn request( @@ -108,7 +103,7 @@ impl Api { where T: for<'de> Deserialize<'de> + Debug, { - trace!("{request_data}"); + debug!("{request_data}"); let request_builder = match request_data { RequestData::GET { url } => self.client.get(*url), @@ -135,6 +130,8 @@ impl Api { request: format!("{request_data}"), })?; + debug!("Response: {response_body}"); + Self::parse_json(request_data, response_body) }