src/client/http_client.rs: allow to pass parameters (encode them as query string)

This commit is contained in:
Dietmar Maurer 2019-05-13 09:12:03 +02:00
parent fbb798f6d2
commit 9e391bb7f0
3 changed files with 38 additions and 34 deletions

View File

@ -1,7 +1,7 @@
use failure::*;
use futures::*;
use serde_json::Value;
use serde_json::{json, Value};
use proxmox_backup::client::*;
fn get(mut h2: h2::client::SendRequest<bytes::Bytes>, path: &str) -> impl Future<Item=Value, Error=Error> {
@ -50,7 +50,8 @@ fn run() -> Result<(), Error> {
let mut client = HttpClient::new(host, username)?;
let h2client = client.h2upgrade("/api2/json/admin/datastore/store2/backup?backup-type=host&backup-id=test");
let param = json!({"backup-type": "host", "backup-id": "test" });
let h2client = client.h2upgrade("/api2/json/admin/datastore/store2/backup", Some(param));
let res = h2client.and_then(|mut h2| {
println!("start http2");

View File

@ -198,7 +198,7 @@ fn list_backups(
let path = format!("api2/json/admin/datastore/{}/backups", repo.store());
let result = client.get(&path)?;
let result = client.get(&path, None)?;
record_repository(&repo);
@ -240,7 +240,7 @@ fn list_backup_groups(
let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
let mut result = client.get(&path).wait()?;
let mut result = client.get(&path, None).wait()?;
record_repository(&repo);
@ -296,17 +296,14 @@ fn list_snapshots(
let path = tools::required_string_param(&param, "group")?;
let group = BackupGroup::parse(path)?;
let query = tools::json_object_to_query(json!({
"backup-type": group.backup_type(),
"backup-id": group.backup_id(),
}))?;
let client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/snapshots?{}", repo.store(), query);
let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
// fixme: params
let result = client.get(&path).wait()?;
let result = client.get(&path, Some(json!({
"backup-type": group.backup_type(),
"backup-id": group.backup_id(),
}))).wait()?;
record_repository(&repo);
@ -344,17 +341,15 @@ fn forget_snapshots(
let path = tools::required_string_param(&param, "snapshot")?;
let snapshot = BackupDir::parse(path)?;
let query = tools::json_object_to_query(json!({
let mut client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
let result = client.delete(&path, Some(json!({
"backup-type": snapshot.group().backup_type(),
"backup-id": snapshot.group().backup_id(),
"backup-time": snapshot.backup_time().timestamp(),
}))?;
let mut client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/snapshots?{}", repo.store(), query);
let result = client.delete(&path).wait()?;
}))).wait()?;
record_repository(&repo);
@ -511,13 +506,11 @@ fn restore(
if path.matches('/').count() == 1 {
let group = BackupGroup::parse(path)?;
let subquery = tools::json_object_to_query(json!({
let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
let result = client.get(&path, Some(json!({
"backup-type": group.backup_type(),
"backup-id": group.backup_id(),
}))?;
let path = format!("api2/json/admin/datastore/{}/snapshots?{}", repo.store(), subquery);
let result = client.get(&path).wait()?;
}))).wait()?;
let list = result["data"].as_array().unwrap();
if list.len() == 0 {
@ -588,7 +581,7 @@ fn try_get(repo: &BackupRepository, url: &str) -> Value {
_ => return Value::Null,
};
let mut resp = match client.get(url).wait() {
let mut resp = match client.get(url, None).wait() {
Ok(v) => v,
_ => return Value::Null,
};

View File

@ -176,15 +176,15 @@ impl HttpClient {
})
}
pub fn get(&self, path: &str) -> impl Future<Item=Value, Error=Error> {
pub fn get(&self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
let req = Self::request_builder(&self.server, "GET", path, None).unwrap();
let req = Self::request_builder(&self.server, "GET", path, data).unwrap();
self.request(req)
}
pub fn delete(&mut self, path: &str) -> impl Future<Item=Value, Error=Error> {
pub fn delete(&mut self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
let req = Self::request_builder(&self.server, "DELETE", path, None).unwrap();
let req = Self::request_builder(&self.server, "DELETE", path, data).unwrap();
self.request(req)
}
@ -239,9 +239,12 @@ impl HttpClient {
self.request(req)
}
pub fn h2upgrade(&mut self, path: &str) -> impl Future<Item=h2::client::SendRequest<bytes::Bytes>, Error=Error> {
pub fn h2upgrade(
&mut self, path:
&str, param: Option<Value>
) -> impl Future<Item=h2::client::SendRequest<bytes::Bytes>, Error=Error> {
let mut req = Self::request_builder(&self.server, "GET", path, None).unwrap();
let mut req = Self::request_builder(&self.server, "GET", path, param).unwrap();
let login = self.auth.listen();
@ -360,9 +363,16 @@ impl HttpClient {
.body(Body::from(data.to_string()))?;
return Ok(request);
} else {
unimplemented!();
let query = tools::json_object_to_query(data)?;
let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?;
let request = Request::builder()
.method(method)
.uri(url)
.header("User-Agent", "proxmox-backup-client/1.0")
.header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
.body(Body::empty())?;
return Ok(request);
}
}
let request = Request::builder()