]> git.proxmox.com Git - proxmox.git/commitdiff
http: extend HttpClient trait
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Thu, 4 Aug 2022 07:53:58 +0000 (09:53 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Wed, 7 Sep 2022 07:17:45 +0000 (09:17 +0200)
to allow get requests with extra headers (such as `Authorization`) and a
generic `request` fn to increase flexibility even more.

this is a breaking change.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
proxmox-http/src/client/simple.rs
proxmox-http/src/client_trait.rs

index 9e333c2f4e5a562bb5c43986d96c87f79dfb56ee..4a2876c4b2c4cab3f15c704e488e50f3a503a194 100644 (file)
@@ -159,11 +159,23 @@ impl Default for SimpleHttp {
 
 #[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()
+    fn get(
+        &self,
+        uri: &str,
+        extra_headers: Option<&HashMap<String, String>>,
+    ) -> Result<Response<Body>, Error> {
+        let mut req = Request::builder()
             .method("GET")
             .uri(uri)
             .body(Body::empty())?;
+
+        if let Some(extra_headers) = extra_headers {
+            let headers = req.headers_mut();
+            for (header, value) in extra_headers {
+                headers.insert(HeaderName::from_str(header)?, HeaderValue::from_str(value)?);
+            }
+        }
+
         proxmox_async::runtime::block_on(self.request(req))
     }
 
@@ -175,15 +187,31 @@ impl crate::HttpClient<Body> for SimpleHttp {
     ) -> Result<Response<Body>, Error> {
         proxmox_async::runtime::block_on(self.post(uri, body.map(|s| s.to_owned()), content_type))
     }
+
+    fn request(&self, request: Request<Body>) -> Result<Response<Body>, Error> {
+        proxmox_async::runtime::block_on(async move { self.request(request).await })
+    }
 }
 
 #[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()
+    fn get(
+        &self,
+        uri: &str,
+        extra_headers: Option<&HashMap<String, String>>,
+    ) -> Result<Response<String>, Error> {
+        let mut req = Request::builder()
             .method("GET")
             .uri(uri)
             .body(Body::empty())?;
+
+        if let Some(extra_headers) = extra_headers {
+            let headers = req.headers_mut();
+            for (header, value) in extra_headers {
+                headers.insert(HeaderName::from_str(header)?, HeaderValue::from_str(value)?);
+            }
+        }
+
         proxmox_async::runtime::block_on(async move {
             Self::convert_body_to_string(self.request(req).await).await
         })
@@ -203,4 +231,13 @@ impl crate::HttpClient<String> for SimpleHttp {
             .await
         })
     }
+
+    fn request(&self, request: Request<String>) -> Result<Response<String>, Error> {
+        proxmox_async::runtime::block_on(async move {
+            let (parts, body) = request.into_parts();
+            let body = Body::from(body);
+            let request = Request::from_parts(parts, body);
+            Self::convert_body_to_string(self.request(request).await).await
+        })
+    }
 }
index 7a6fa655777a0972ae8915c7d4e1eb836920d608..5b5545d34ae474c80f49ea232aa8cb9e6283d076 100644 (file)
@@ -1,8 +1,14 @@
+use std::collections::HashMap;
+
 use anyhow::Error;
-use http::Response;
+use http::{Request, Response};
 
 pub trait HttpClient<T> {
-    fn get(&self, uri: &str) -> Result<Response<T>, Error>;
+    fn get(
+        &self,
+        uri: &str,
+        extra_headers: Option<&HashMap<String, String>>,
+    ) -> Result<Response<T>, Error>;
 
     fn post(
         &self,
@@ -10,4 +16,6 @@ pub trait HttpClient<T> {
         body: Option<&str>,
         content_type: Option<&str>,
     ) -> Result<Response<T>, Error>;
+
+    fn request(&self, request: Request<T>) -> Result<Response<T>, Error>;
 }