]> git.proxmox.com Git - proxmox-backup.git/commitdiff
src/client/http_client.rs: use async for h2api_response()
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 5 Sep 2019 12:56:52 +0000 (14:56 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 5 Sep 2019 12:56:52 +0000 (14:56 +0200)
src/client/http_client.rs

index 75bb9d065f61200b809fda7657a20b9553759750..2d5dcd796ba1d854212e2216714f5d6052847f0c 100644 (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,32 +1194,29 @@ impl H2Client {
         // the data from memory.
         let mut release_capacity = body.release_capacity().clone();
 
-        body
-            .map_ok(move |chunk| {
-                // 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 {
-                let text = String::from_utf8(data.to_vec()).unwrap();
-                if status.is_success() {
-                    if text.len() > 0 {
-                        let mut value: Value = serde_json::from_str(&text)?;
-                        if let Some(map) = value.as_object_mut() {
-                            if let Some(data) = map.remove("data") {
-                                return Ok(data);
-                            }
-                        }
-                        bail!("got result without data property");
-                    } else {
-                        Ok(Value::Null)
+        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());
+            data.extend(chunk);
+        }
+
+        let text = String::from_utf8(data.to_vec()).unwrap();
+        if status.is_success() {
+            if text.len() > 0 {
+                let mut value: Value = serde_json::from_str(&text)?;
+                if let Some(map) = value.as_object_mut() {
+                    if let Some(data) = map.remove("data") {
+                        return Ok(data);
                     }
-                } 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