]> git.proxmox.com Git - proxmox.git/commitdiff
http: implement HttpClient for SimpleHttp
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Thu, 23 Jun 2022 09:57:25 +0000 (11:57 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Thu, 30 Jun 2022 10:42:17 +0000 (12:42 +0200)
Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
proxmox-http/Cargo.toml
proxmox-http/src/client/simple.rs
proxmox-http/src/client_trait.rs

index 0d633f7ddbd4338a4c8be4dd8e4b022b128ea3d0..aa7654f835fa97d61396589d511af0f1d1ce30ae 100644 (file)
@@ -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" }
index 6e92f9d0fe3ce91019e1c6015fd919dc7896e4b6..ee0461bc37a96fb0d89a257977a8168da5869f48 100644 (file)
@@ -146,9 +146,26 @@ impl SimpleHttp {
     }
 
     pub async fn response_body_string(res: Response<Body>) -> Result<String, Error> {
-        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<Response<Body>, Error>,
+    ) -> Result<Response<String>, 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<Body> for SimpleHttp {
+    fn get(&self, uri: &str) -> Result<Response<Body>, 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<Response<Body>, 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<String> for SimpleHttp {
+    fn get(&self, uri: &str) -> Result<Response<String>, 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<Response<String>, 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
+        })
+    }
+}
index bcefa8f9eb8d9ec247814d822f3b8385a83f1d85..7a6fa655777a0972ae8915c7d4e1eb836920d608 100644 (file)
@@ -7,7 +7,7 @@ pub trait HttpClient<T> {
     fn post(
         &self,
         uri: &str,
-        body: Option<String>,
+        body: Option<&str>,
         content_type: Option<&str>,
     ) -> Result<Response<T>, Error>;
 }