]> git.proxmox.com Git - proxmox-backup.git/blame - src/client/http_client.rs
api3/admin/datastore.rs: implement list backups
[proxmox-backup.git] / src / client / http_client.rs
CommitLineData
597641fd
DM
1use failure::*;
2
3use http::Uri;
4use hyper::Body;
5use hyper::client::Client;
6use hyper::rt::{self, Future};
7
8pub struct HttpClient {
9 server: String,
10}
11
12impl HttpClient {
13
14 pub fn new(server: &str) -> Self {
15 Self {
16 server: String::from(server),
17 }
18 }
19
83bdac1e 20 pub fn upload(&self, content_type: &str, body: Body, path: &str) -> Result<(), Error> {
597641fd
DM
21
22 let client = Client::new();
23
24 let url: Uri = format!("http://{}:8007/{}", self.server, path).parse()?;
25
26 use http::Request;
27 use futures::stream::Stream;
28
29 let request = Request::builder()
30 .method("POST")
31 .uri(url)
32 .header("User-Agent", "proxmox-backup-client/1.0")
83bdac1e 33 .header("Content-Type", content_type)
597641fd
DM
34 .body(body)?;
35
36 let future = client
37 .request(request)
38 .map_err(|e| Error::from(e))
39 .and_then(|resp| {
40
41 let status = resp.status();
42
43 resp.into_body().concat2().map_err(|e| Error::from(e))
44 .and_then(move |data| {
45
46 let text = String::from_utf8(data.to_vec()).unwrap();
47 if status.is_success() {
48 println!("Result {} {}", status, text);
49 } else {
50 eprintln!("HTTP Error {}: {}", status, text);
51 }
52 Ok(())
53 })
54 })
55 .map_err(|err| {
56 eprintln!("Error: {}", err);
57 });
58
59 // drop client, else client keeps connectioon open (keep-alive feature)
60 drop(client);
61
62 rt::run(future);
63
64 Ok(())
65 }
66}