diff --git a/proxmox-http/Cargo.toml b/proxmox-http/Cargo.toml index 0d633f7d..aa7654f8 100644 --- a/proxmox-http/Cargo.toml +++ b/proxmox-http/Cargo.toml @@ -23,6 +23,7 @@ tokio = { version = "1.0", features = [], optional = true } tokio-openssl = { version = "0.6.1", optional = true } url = { version = "2", optional = true } +proxmox-async = { path = "../proxmox-async", optional = true, version = "0.4.1" } proxmox-sys = { path = "../proxmox-sys", optional = true, version = "0.3.0" } proxmox-io = { path = "../proxmox-io", optional = true, version = "1.0.0" } proxmox-lang = { path = "../proxmox-lang", optional = true, version = "1.1" } diff --git a/proxmox-http/src/client/simple.rs b/proxmox-http/src/client/simple.rs index 6e92f9d0..ee0461bc 100644 --- a/proxmox-http/src/client/simple.rs +++ b/proxmox-http/src/client/simple.rs @@ -146,9 +146,26 @@ impl SimpleHttp { } pub async fn response_body_string(res: Response) -> Result { - let buf = hyper::body::to_bytes(res).await?; - String::from_utf8(buf.to_vec()) - .map_err(|err| format_err!("Error converting HTTP result data: {}", err)) + Self::convert_body_to_string(Ok(res)) + .await + .map(|res| res.into_body()) + } + + async fn convert_body_to_string( + response: Result, Error>, + ) -> Result, Error> { + match response { + Ok(res) => { + let (parts, body) = res.into_parts(); + + let buf = hyper::body::to_bytes(body).await?; + let new_body = String::from_utf8(buf.to_vec()) + .map_err(|err| format_err!("Error converting HTTP result data: {}", err))?; + + Ok(Response::from_parts(parts, new_body)) + } + Err(err) => Err(err), + } } } @@ -157,3 +174,51 @@ impl Default for SimpleHttp { Self::new() } } + +#[cfg(all(feature = "client-trait", feature = "proxmox-async"))] +impl crate::HttpClient for SimpleHttp { + fn get(&self, uri: &str) -> Result, Error> { + let req = Request::builder() + .method("GET") + .uri(uri) + .body(Body::empty())?; + proxmox_async::runtime::block_on(self.request(req)) + } + + fn post( + &self, + uri: &str, + body: Option<&str>, + content_type: Option<&str>, + ) -> Result, Error> { + proxmox_async::runtime::block_on(self.post(uri, body.map(|s| s.to_owned()), content_type)) + } +} + +#[cfg(all(feature = "client-trait", feature = "proxmox-async"))] +impl crate::HttpClient for SimpleHttp { + fn get(&self, uri: &str) -> Result, Error> { + let req = Request::builder() + .method("GET") + .uri(uri) + .body(Body::empty())?; + proxmox_async::runtime::block_on(async move { + Self::convert_body_to_string(self.request(req).await).await + }) + } + + fn post( + &self, + uri: &str, + body: Option<&str>, + content_type: Option<&str>, + ) -> Result, Error> { + proxmox_async::runtime::block_on(async move { + Self::convert_body_to_string( + self.post(uri, body.map(|s| s.to_owned()), content_type) + .await, + ) + .await + }) + } +} diff --git a/proxmox-http/src/client_trait.rs b/proxmox-http/src/client_trait.rs index bcefa8f9..7a6fa655 100644 --- a/proxmox-http/src/client_trait.rs +++ b/proxmox-http/src/client_trait.rs @@ -7,7 +7,7 @@ pub trait HttpClient { fn post( &self, uri: &str, - body: Option, + body: Option<&str>, content_type: Option<&str>, ) -> Result, Error>; }