src/client/http_client.rs: use async for h2api_response()

This commit is contained in:
Dietmar Maurer 2019-09-05 14:56:52 +02:00
parent c18fddf80f
commit 9edd3bf1b8

View File

@ -1179,9 +1179,9 @@ impl H2Client {
})
}
fn h2api_response(
async fn h2api_response(
response: Response<h2::RecvStream>,
) -> impl Future<Output = Result<Value, Error>> {
) -> Result<Value, Error> {
let status = response.status();
let (_head, mut body) = response.into_parts();
@ -1194,15 +1194,13 @@ impl H2Client {
// the data from memory.
let mut release_capacity = body.release_capacity().clone();
body
.map_ok(move |chunk| {
let mut data = Vec::new();
while let Some(chunk) = body.try_next().await? {
// Let the server send more data.
let _ = release_capacity.release_capacity(chunk.len());
chunk
})
.try_concat()
.map_err(Error::from)
.and_then(move |data| async move {
data.extend(chunk);
}
let text = String::from_utf8(data.to_vec()).unwrap();
if status.is_success() {
if text.len() > 0 {
@ -1219,7 +1217,6 @@ impl H2Client {
} else {
bail!("HTTP Error {}: {}", status, text);
}
}.boxed())
}
// Note: We always encode parameters with the url