|
1 | 1 | //! http-client implementation for reqwest |
2 | 2 |
|
3 | 3 | use super::{async_trait, Error, HttpClient, Request, Response}; |
| 4 | +use futures_util::stream::TryStreamExt; |
4 | 5 | use http_types::headers::{HeaderName, HeaderValue}; |
5 | 6 | use http_types::StatusCode; |
6 | 7 | use hyper::body::HttpBody; |
7 | 8 | use hyper_tls::HttpsConnector; |
8 | 9 | use std::convert::TryFrom; |
| 10 | +use std::io; |
9 | 11 | use std::str::FromStr; |
10 | 12 |
|
11 | 13 | /// Hyper-based HTTP Client. |
@@ -100,20 +102,11 @@ struct HttpTypesResponse { |
100 | 102 |
|
101 | 103 | impl HttpTypesResponse { |
102 | 104 | async fn try_from(value: hyper::Response<hyper::Body>) -> Result<Self, Error> { |
103 | | - let (parts, mut body) = value.into_parts(); |
104 | | - |
105 | | - let body = match body.data().await { |
106 | | - None => None, |
107 | | - Some(Ok(b)) => Some(b), |
108 | | - Some(Err(_)) => { |
109 | | - return Err(Error::from_str( |
110 | | - StatusCode::BadGateway, |
111 | | - "unable to read HTTP response body", |
112 | | - )) |
113 | | - } |
114 | | - } |
115 | | - .map(|b| http_types::Body::from_bytes(b.to_vec())) |
116 | | - .unwrap_or(http_types::Body::empty()); |
| 105 | + let (parts, body) = value.into_parts(); |
| 106 | + |
| 107 | + let size_hint = body.size_hint().upper().map(|s| s as usize); |
| 108 | + let body = body.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string())); |
| 109 | + let body = http_types::Body::from_reader(body.into_async_read(), size_hint); |
117 | 110 |
|
118 | 111 | let mut res = Response::new(parts.status); |
119 | 112 | res.set_version(Some(parts.version.into())); |
|
0 commit comments