use crate::error::Error;
use crate::response::Response;
use crate::Result;
#[cfg(feature = "json")]
pub fn into_api_result<T, E>(response: Response) -> Result<std::result::Result<T, E>>
where
T: serde::de::DeserializeOwned,
E: serde::de::DeserializeOwned,
{
if response.is_success() {
return Ok(Ok(response.into_json()?));
}
let status = response.status();
let body = response.bytes().clone();
match serde_json::from_slice::<E>(&body) {
Ok(err_body) => Ok(std::result::Result::Err(err_body)),
Err(_) => Err(Error::http(
status,
"failed to deserialize error response body",
Some(body),
)),
}
}
#[cfg(feature = "json")]
pub trait ApiResponseExt {
fn into_api_result<T, E>(self) -> Result<std::result::Result<T, E>>
where
T: serde::de::DeserializeOwned,
E: serde::de::DeserializeOwned;
}
#[cfg(feature = "json")]
impl ApiResponseExt for Response {
fn into_api_result<T, E>(self) -> Result<std::result::Result<T, E>>
where
T: serde::de::DeserializeOwned,
E: serde::de::DeserializeOwned,
{
into_api_result(self)
}
}