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>, response: Response<h2::RecvStream>,
) -> impl Future<Output = Result<Value, Error>> { ) -> Result<Value, Error> {
let status = response.status(); let status = response.status();
let (_head, mut body) = response.into_parts(); let (_head, mut body) = response.into_parts();
@ -1194,32 +1194,29 @@ impl H2Client {
// the data from memory. // the data from memory.
let mut release_capacity = body.release_capacity().clone(); let mut release_capacity = body.release_capacity().clone();
body let mut data = Vec::new();
.map_ok(move |chunk| { while let Some(chunk) = body.try_next().await? {
// Let the server send more data. // Let the server send more data.
let _ = release_capacity.release_capacity(chunk.len()); let _ = release_capacity.release_capacity(chunk.len());
chunk data.extend(chunk);
}) }
.try_concat()
.map_err(Error::from) let text = String::from_utf8(data.to_vec()).unwrap();
.and_then(move |data| async move { if status.is_success() {
let text = String::from_utf8(data.to_vec()).unwrap(); if text.len() > 0 {
if status.is_success() { let mut value: Value = serde_json::from_str(&text)?;
if text.len() > 0 { if let Some(map) = value.as_object_mut() {
let mut value: Value = serde_json::from_str(&text)?; if let Some(data) = map.remove("data") {
if let Some(map) = value.as_object_mut() { return Ok(data);
if let Some(data) = map.remove("data") {
return Ok(data);
}
}
bail!("got result without data property");
} else {
Ok(Value::Null)
} }
} else {
bail!("HTTP Error {}: {}", status, text);
} }
}.boxed()) bail!("got result without data property");
} else {
Ok(Value::Null)
}
} else {
bail!("HTTP Error {}: {}", status, text);
}
} }
// Note: We always encode parameters with the url // Note: We always encode parameters with the url