monoio_http_client/
response.rs1use bytes::Bytes;
2use http::{Extensions, HeaderMap, HeaderValue, StatusCode, Version};
3use monoio_http::common::body::{BodyExt, HttpBody};
4
5pub struct ClientResponse {
6 status: StatusCode,
8 version: Version,
10 headers: HeaderMap<HeaderValue>,
12 extensions: Extensions,
14 body: HttpBody,
16}
17
18impl ClientResponse {
19 pub fn new(inner: http::Response<HttpBody>) -> Self {
20 let (head, body) = inner.into_parts();
21 Self {
22 status: head.status,
23 version: head.version,
24 headers: head.headers,
25 extensions: head.extensions,
26 body,
27 }
28 }
29
30 #[inline]
32 pub fn status(&self) -> StatusCode {
33 self.status
34 }
35
36 #[inline]
38 pub fn version(&self) -> Version {
39 self.version
40 }
41
42 #[inline]
44 pub fn headers(&self) -> &HeaderMap {
45 &self.headers
46 }
47
48 #[inline]
50 pub fn headers_mut(&mut self) -> &mut HeaderMap {
51 &mut self.headers
52 }
53
54 pub fn extensions(&self) -> &http::Extensions {
56 &self.extensions
57 }
58
59 pub fn extensions_mut(&mut self) -> &mut http::Extensions {
61 &mut self.extensions
62 }
63
64 pub async fn bytes(self) -> crate::Result<Bytes> {
66 let body = self.body;
67 body.bytes().await.map_err(Into::into)
68 }
69
70 pub fn raw_body(self) -> HttpBody {
72 self.body
73 }
74
75 pub async fn json<T: serde::de::DeserializeOwned>(self) -> crate::Result<T> {
80 let bytes = self.body.bytes().await?;
81 let d = serde_json::from_slice(&bytes)?;
82 Ok(d)
83 }
84}