From 9e391bb7f04412458516ebdea9a738305a878d59 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 13 May 2019 09:12:03 +0200 Subject: [PATCH] src/client/http_client.rs: allow to pass parameters (encode them as query string) --- src/bin/h2test.rs | 5 ++-- src/bin/proxmox-backup-client.rs | 41 +++++++++++++------------------- src/client/http_client.rs | 26 +++++++++++++------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/bin/h2test.rs b/src/bin/h2test.rs index f5c3f207..66f6c086 100644 --- a/src/bin/h2test.rs +++ b/src/bin/h2test.rs @@ -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, path: &str) -> impl Future { @@ -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"); diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index 8016f668..7e2fcc2e 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -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(¶m, "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(¶m, "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, }; diff --git a/src/client/http_client.rs b/src/client/http_client.rs index cabef404..cc9ee742 100644 --- a/src/client/http_client.rs +++ b/src/client/http_client.rs @@ -176,15 +176,15 @@ impl HttpClient { }) } - pub fn get(&self, path: &str) -> impl Future { + pub fn get(&self, path: &str, data: Option) -> impl Future { - 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 { + pub fn delete(&mut self, path: &str, data: Option) -> impl Future { - 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, Error=Error> { + pub fn h2upgrade( + &mut self, path: + &str, param: Option + ) -> impl Future, 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()