mastodon API rust lib elefren, fixed and updated. and also all ASYNC! NB. most examples are now wrong.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

/// Helpers for serializing to/deserializing from toml
///
/// In order to use this module, set the "toml" feature in your Cargo.toml:
///
/// ```toml,ignore
/// [dependencies.elefren]
/// version = "0.22"
/// features = ["toml"]
/// ```
pub mod toml;
/// Helpers for serializing to/deserializing from json
///
/// In order to use this module, set the "json" feature in your Cargo.toml:
///
/// ```toml,ignore
/// [dependencies.elefen]
/// version = "0.22"
/// features = ["json"]
/// ```
pub mod json;
/// Helpers for working with the command line
pub mod cli;
/// # Low-level API for extending
// Convert the HTTP response body from JSON. Pass up deserialization errors
// transparently.
pub async fn deserialise_response<T: for<'de> serde::Deserialize<'de>>(response: reqwest::Response) -> crate::Result<T> {
let bytes = response.bytes().await?;
match serde_json::from_slice(&bytes) {
Ok(t) => {
debug!("{}", String::from_utf8_lossy(&bytes));
Ok(t)
}
// If deserializing into the desired type fails try again to
// see if this is an error response.
Err(e) => {
error!("{}", String::from_utf8_lossy(&bytes));
if let Ok(error) = serde_json::from_slice(&bytes) {
return Err(crate::Error::Api(error));
}
Err(e.into())
}
}
}