2019-01-17 10:29:38 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use http::Uri;
|
|
|
|
use hyper::Body;
|
|
|
|
use hyper::client::Client;
|
|
|
|
use hyper::rt::{self, Future};
|
|
|
|
|
2019-01-21 17:56:48 +00:00
|
|
|
use http::Request;
|
|
|
|
use futures::stream::Stream;
|
|
|
|
|
|
|
|
use serde_json::{Value};
|
|
|
|
|
2019-01-17 10:29:38 +00:00
|
|
|
pub struct HttpClient {
|
|
|
|
server: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HttpClient {
|
|
|
|
|
|
|
|
pub fn new(server: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
server: String::from(server),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:56:48 +00:00
|
|
|
fn run_request(request: Request<Body>) -> Result<Value, Error> {
|
2019-01-17 10:29:38 +00:00
|
|
|
let client = Client::new();
|
|
|
|
|
2019-01-21 17:56:48 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-01-17 10:29:38 +00:00
|
|
|
|
2019-01-21 17:56:48 +00:00
|
|
|
let mutex = Arc::new(Mutex::new(Err(format_err!("no data"))));
|
|
|
|
let mutex_c1 = mutex.clone();
|
|
|
|
let mutex_c2 = mutex.clone();
|
2019-01-17 10:29:38 +00:00
|
|
|
|
|
|
|
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() {
|
2019-01-21 17:56:48 +00:00
|
|
|
if text.len() > 0 {
|
|
|
|
let value: Value = serde_json::from_str(&text)?;
|
|
|
|
Ok(value)
|
|
|
|
} else {
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
2019-01-17 10:29:38 +00:00
|
|
|
} else {
|
2019-01-21 17:56:48 +00:00
|
|
|
bail!("HTTP Error {}: {}", status, text);
|
2019-01-17 10:29:38 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-01-21 17:56:48 +00:00
|
|
|
.map(move |res| {
|
|
|
|
*mutex_c1.lock().unwrap() = Ok(res);
|
|
|
|
})
|
|
|
|
.map_err(move |err| {
|
|
|
|
*mutex_c2.lock().unwrap() = Err(err);
|
2019-01-17 10:29:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// drop client, else client keeps connectioon open (keep-alive feature)
|
|
|
|
drop(client);
|
|
|
|
|
|
|
|
rt::run(future);
|
|
|
|
|
2019-01-21 17:56:48 +00:00
|
|
|
Arc::try_unwrap(mutex).unwrap()
|
|
|
|
.into_inner().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, path: &str) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let url: Uri = format!("http://{}:8007/{}", self.server, path).parse()?;
|
|
|
|
|
|
|
|
let request = Request::builder()
|
|
|
|
.method("GET")
|
|
|
|
.uri(url)
|
|
|
|
.header("User-Agent", "proxmox-backup-client/1.0")
|
|
|
|
.body(Body::empty())?;
|
|
|
|
|
|
|
|
Self::run_request(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn upload(&self, content_type: &str, body: Body, path: &str) -> Result<Value, 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")
|
|
|
|
.header("Content-Type", content_type)
|
|
|
|
.body(body)?;
|
|
|
|
|
|
|
|
Self::run_request(request)
|
2019-01-17 10:29:38 +00:00
|
|
|
}
|
|
|
|
}
|