diff --git a/proxmox-http/src/client/simple.rs b/proxmox-http/src/client/simple.rs index 9e333c2f..4a2876c4 100644 --- a/proxmox-http/src/client/simple.rs +++ b/proxmox-http/src/client/simple.rs @@ -159,11 +159,23 @@ impl Default for SimpleHttp { #[cfg(all(feature = "client-trait", feature = "proxmox-async"))] impl crate::HttpClient for SimpleHttp { - fn get(&self, uri: &str) -> Result, Error> { - let req = Request::builder() + fn get( + &self, + uri: &str, + extra_headers: Option<&HashMap>, + ) -> Result, 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 for SimpleHttp { ) -> Result, Error> { proxmox_async::runtime::block_on(self.post(uri, body.map(|s| s.to_owned()), content_type)) } + + fn request(&self, request: Request) -> Result, Error> { + proxmox_async::runtime::block_on(async move { self.request(request).await }) + } } #[cfg(all(feature = "client-trait", feature = "proxmox-async"))] impl crate::HttpClient for SimpleHttp { - fn get(&self, uri: &str) -> Result, Error> { - let req = Request::builder() + fn get( + &self, + uri: &str, + extra_headers: Option<&HashMap>, + ) -> Result, 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 for SimpleHttp { .await }) } + + fn request(&self, request: Request) -> Result, 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 + }) + } } diff --git a/proxmox-http/src/client_trait.rs b/proxmox-http/src/client_trait.rs index 7a6fa655..5b5545d3 100644 --- a/proxmox-http/src/client_trait.rs +++ b/proxmox-http/src/client_trait.rs @@ -1,8 +1,14 @@ +use std::collections::HashMap; + use anyhow::Error; -use http::Response; +use http::{Request, Response}; pub trait HttpClient { - fn get(&self, uri: &str) -> Result, Error>; + fn get( + &self, + uri: &str, + extra_headers: Option<&HashMap>, + ) -> Result, Error>; fn post( &self, @@ -10,4 +16,6 @@ pub trait HttpClient { body: Option<&str>, content_type: Option<&str>, ) -> Result, Error>; + + fn request(&self, request: Request) -> Result, Error>; }