2022-12-18 21:06:30 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-12-07 20:58:28 +00:00
|
|
|
|
|
|
|
/// Log metadata about this request based on the type given:
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2022-12-22 18:19:49 +00:00
|
|
|
/// use mastodon_async::log_serde;
|
2022-12-07 20:58:28 +00:00
|
|
|
/// tokio_test::block_on(async {
|
|
|
|
/// let request = reqwest::get("https://example.org/").await.unwrap();
|
|
|
|
/// log::warn!(
|
|
|
|
/// status = log_serde!(request Status),
|
|
|
|
/// headers = log_serde!(request Headers);
|
|
|
|
/// "test"
|
|
|
|
/// );
|
|
|
|
/// })
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! log_serde {
|
|
|
|
($response:ident $type_name:tt) => {
|
|
|
|
log::as_serde!($crate::helpers::log::$type_name::from(&$response))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serializable form of reqwest's Status type.
|
|
|
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct Status {
|
|
|
|
/// The numerical representation of the status
|
|
|
|
pub code: u16,
|
|
|
|
/// it's canonical reason.
|
|
|
|
pub message: Option<&'static str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Status {
|
|
|
|
/// New from reqwest's Status type (which is more useful but not
|
|
|
|
/// serializable).
|
|
|
|
pub fn new(status: reqwest::StatusCode) -> Self {
|
|
|
|
Self {
|
|
|
|
code: status.as_u16(),
|
|
|
|
message: status.canonical_reason(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&reqwest::Response> for Status {
|
|
|
|
fn from(value: &reqwest::Response) -> Self {
|
|
|
|
Self::new(value.status())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper for logging request headers
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Headers(pub reqwest::header::HeaderMap);
|
|
|
|
|
|
|
|
impl Serialize for Headers {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
serializer.collect_map(
|
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (format!("{k:?}"), format!("{v:?}"))),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&reqwest::Response> for Headers {
|
|
|
|
fn from(value: &reqwest::Response) -> Self {
|
|
|
|
Headers(value.headers().clone())
|
|
|
|
}
|
|
|
|
}
|