feat: slightly cleaner tracing output
This commit is contained in:
parent
5c018e9f8d
commit
5e40f2cf52
21
src/lib.rs
21
src/lib.rs
|
@ -5,7 +5,7 @@ use derive_more::{Display, FromStr};
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt::{Debug, Display};
|
use std::fmt::{Debug, Display};
|
||||||
use tracing::{instrument, trace};
|
use tracing::debug;
|
||||||
|
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod calendar_history;
|
pub mod calendar_history;
|
||||||
|
@ -68,7 +68,6 @@ impl Api {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self))]
|
|
||||||
async fn get<D>(&self, url: &str) -> Result<ResponseData<D>, TeslatteError>
|
async fn get<D>(&self, url: &str) -> Result<ResponseData<D>, TeslatteError>
|
||||||
where
|
where
|
||||||
D: for<'de> Deserialize<'de> + Debug,
|
D: for<'de> Deserialize<'de> + Debug,
|
||||||
|
@ -76,29 +75,25 @@ impl Api {
|
||||||
self.request(&RequestData::GET { url }).await
|
self.request(&RequestData::GET { url }).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self))]
|
|
||||||
async fn post<S>(&self, url: &str, body: S) -> Result<ResponseData<PostResponse>, TeslatteError>
|
async fn post<S>(&self, url: &str, body: S) -> Result<ResponseData<PostResponse>, TeslatteError>
|
||||||
where
|
where
|
||||||
S: Serialize + Debug,
|
S: Serialize + Debug,
|
||||||
{
|
{
|
||||||
// let request_context = || format!("POST {url} {payload}");
|
|
||||||
|
|
||||||
let payload =
|
let payload =
|
||||||
&serde_json::to_string(&body).expect("Should not fail creating the request struct.");
|
&serde_json::to_string(&body).expect("Should not fail creating the request struct.");
|
||||||
|
|
||||||
let request_data = RequestData::POST { url, payload };
|
let request_data = RequestData::POST { url, payload };
|
||||||
let data = self.request::<PostResponse>(&request_data).await?;
|
let data = self.request::<PostResponse>(&request_data).await?;
|
||||||
|
|
||||||
if data.data.result {
|
if !data.data.result {
|
||||||
Ok(data)
|
return Err(TeslatteError::ServerError {
|
||||||
} else {
|
|
||||||
Err(TeslatteError::ServerError {
|
|
||||||
request: format!("{request_data}"),
|
request: format!("{request_data}"),
|
||||||
msg: data.data.reason,
|
msg: data.data.reason,
|
||||||
description: None,
|
description: None,
|
||||||
body: Some(data.body),
|
body: Some(data.body),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn request<T>(
|
async fn request<T>(
|
||||||
|
@ -108,7 +103,7 @@ impl Api {
|
||||||
where
|
where
|
||||||
T: for<'de> Deserialize<'de> + Debug,
|
T: for<'de> Deserialize<'de> + Debug,
|
||||||
{
|
{
|
||||||
trace!("{request_data}");
|
debug!("{request_data}");
|
||||||
|
|
||||||
let request_builder = match request_data {
|
let request_builder = match request_data {
|
||||||
RequestData::GET { url } => self.client.get(*url),
|
RequestData::GET { url } => self.client.get(*url),
|
||||||
|
@ -135,6 +130,8 @@ impl Api {
|
||||||
request: format!("{request_data}"),
|
request: format!("{request_data}"),
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
debug!("Response: {response_body}");
|
||||||
|
|
||||||
Self::parse_json(request_data, response_body)
|
Self::parse_json(request_data, response_body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue