1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crate::types::Result;
use std::io::{BufReader, Read};
use hyper::body::Buf;
use hyper::{Body, Response};
use serde::de;
pub async fn resp_into_struct<T>(resp: Response<Body>) -> Result<T>
where
T: de::DeserializeOwned,
{
let body = hyper::body::aggregate(resp).await?;
let reader = BufReader::new(body.reader());
Ok(serde_json::from_reader(reader)?)
}
pub async fn into_struct_from_str<T>(resp: Response<Body>) -> Result<T>
where
T: de::DeserializeOwned,
{
let body_bytes = hyper::body::to_bytes(resp.into_body()).await?;
Ok(serde_json::from_str(std::str::from_utf8(&body_bytes)?)?)
}
pub async fn into_struct_from_slice<T>(resp: Response<Body>) -> Result<T>
where
T: de::DeserializeOwned,
{
let bytes = hyper::body::to_bytes(resp).await?;
Ok(serde_json::from_slice(&bytes)?)
}
pub async fn resp_to_string(resp: &mut Response<Body>) -> Result<String> {
let body = hyper::body::aggregate(resp).await?;
let mut reader = BufReader::new(body.reader());
let mut body_string = String::new();
reader.read_to_string(&mut body_string)?;
Ok(body_string)
}