octocrab/
from_response.rs

1use bytes::Bytes;
2use http_body::Body;
3use http_body_util::BodyExt;
4use snafu::ResultExt;
5
6/// A trait for mapping from a `http::Response` to an another type.
7#[async_trait::async_trait]
8pub trait FromResponse: Sized {
9    async fn from_response<B>(response: http::Response<B>) -> crate::Result<Self>
10    where
11        B: Body<Data = Bytes, Error = crate::Error> + Send;
12}
13
14#[async_trait::async_trait]
15impl<T: serde::de::DeserializeOwned> FromResponse for T {
16    async fn from_response<B>(response: http::Response<B>) -> crate::Result<Self>
17    where
18        B: Body<Data = Bytes, Error = crate::Error> + Send,
19    {
20        let (_, body) = response.into_parts();
21        let body = body.collect().await?.to_bytes();
22        let de = &mut serde_json::Deserializer::from_slice(&body);
23        return serde_path_to_error::deserialize(de).context(crate::error::JsonSnafu);
24    }
25}