]> git.proxmox.com Git - proxmox-backup.git/commitdiff
client/http_client.rs: new helper class
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 17 Jan 2019 10:29:38 +0000 (11:29 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 17 Jan 2019 10:38:03 +0000 (11:38 +0100)
src/client/http_client.rs [new file with mode: 0644]
src/lib.rs

diff --git a/src/client/http_client.rs b/src/client/http_client.rs
new file mode 100644 (file)
index 0000000..18cca32
--- /dev/null
@@ -0,0 +1,65 @@
+use failure::*;
+
+use http::Uri;
+use hyper::Body;
+use hyper::client::Client;
+use hyper::rt::{self, Future};
+
+pub struct HttpClient {
+    server: String,
+}
+
+impl HttpClient {
+
+    pub fn new(server: &str) -> Self {
+        Self {
+            server: String::from(server),
+        }
+    }
+
+    pub fn upload(&self, body: Body, path: &str) -> Result<(), Error> {
+
+        let client = Client::new();
+
+        let url: Uri = format!("http://{}:8007/{}", self.server, path).parse()?;
+
+        use http::Request;
+        use futures::stream::Stream;
+
+        let request = Request::builder()
+            .method("POST")
+            .uri(url)
+            .header("User-Agent", "proxmox-backup-client/1.0")
+            .body(body)?;
+
+        let future = client
+            .request(request)
+            .map_err(|e| Error::from(e))
+            .and_then(|resp| {
+
+                let status = resp.status();
+
+                resp.into_body().concat2().map_err(|e| Error::from(e))
+                    .and_then(move |data| {
+
+                        let text = String::from_utf8(data.to_vec()).unwrap();
+                        if status.is_success() {
+                            println!("Result {} {}", status, text);
+                        } else {
+                            eprintln!("HTTP Error {}: {}", status, text);
+                        }
+                        Ok(())
+                    })
+            })
+            .map_err(|err| {
+                eprintln!("Error: {}", err);
+            });
+
+        // drop client, else client keeps connectioon open (keep-alive feature)
+        drop(client);
+
+        rt::run(future);
+
+        Ok(())
+    }
+}
index 725d8e7a4ee5f04971410a8eb4ba46dcaa1183c6..017fa843b7ff381bd608aa739a1e9410748954fe 100644 (file)
@@ -59,5 +59,6 @@ pub mod api3;
 
 pub mod client {
 
+    pub mod http_client;
     pub mod catar_backup_stream;
 }