From: Fabian Grünbichler Date: Fri, 5 Aug 2022 07:42:23 +0000 (+0200) Subject: http: client_trait: make request body generic X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=08a6d56eae9d40a156b288a8994ad6076fea49c6;p=proxmox.git http: client_trait: make request body generic like the response body, instead of hard-coding Read. --- diff --git a/proxmox-http/src/client/simple.rs b/proxmox-http/src/client/simple.rs index 083f1d99..e9910802 100644 --- a/proxmox-http/src/client/simple.rs +++ b/proxmox-http/src/client/simple.rs @@ -1,7 +1,6 @@ use anyhow::{bail, format_err, Error}; use std::collections::HashMap; -use std::io::Read; #[cfg(all(feature = "client-trait", feature = "proxmox-async"))] use std::str::FromStr; @@ -82,23 +81,13 @@ impl Client { self.client.request(request).map_err(Error::from).await } - pub async fn post( + pub async fn post( &self, uri: &str, - body: Option, + body: Option, content_type: Option<&str>, extra_headers: Option<&HashMap>, - ) -> Result, Error> - where - R: Read, - { - let body = if let Some(mut body) = body { - let mut body_vec = Vec::new(); - body.read_to_end(&mut body_vec)?; - Body::from(body_vec) - } else { - Body::empty() - }; + ) -> Result, Error> { let content_type = content_type.unwrap_or("application/json"); let mut request = Request::builder() @@ -112,7 +101,7 @@ impl Client { } } - let request = request.body(body)?; + let request = request.body(body.unwrap_or_default())?; self.request(request).await } @@ -173,7 +162,7 @@ impl Default for Client { } #[cfg(all(feature = "client-trait", feature = "proxmox-async"))] -impl crate::HttpClient for Client { +impl crate::HttpClient for Client { fn get( &self, uri: &str, @@ -194,16 +183,13 @@ impl crate::HttpClient for Client { proxmox_async::runtime::block_on(self.request(req)) } - fn post( + fn post( &self, uri: &str, - body: Option, + body: Option, content_type: Option<&str>, extra_headers: Option<&HashMap>, - ) -> Result, Error> - where - R: Read, - { + ) -> Result, Error> { proxmox_async::runtime::block_on(self.post(uri, body, content_type, extra_headers)) } @@ -213,7 +199,7 @@ impl crate::HttpClient for Client { } #[cfg(all(feature = "client-trait", feature = "proxmox-async"))] -impl crate::HttpClient for Client { +impl crate::HttpClient for Client { fn get( &self, uri: &str, @@ -236,17 +222,15 @@ impl crate::HttpClient for Client { }) } - fn post( + fn post( &self, uri: &str, - body: Option, + body: Option, content_type: Option<&str>, extra_headers: Option<&HashMap>, - ) -> Result, Error> - where - R: Read, - { + ) -> Result, Error> { proxmox_async::runtime::block_on(async move { + let body = body.map(|s| Body::from(s.into_bytes())); Self::convert_body_to_string(self.post(uri, body, content_type, extra_headers).await) .await }) diff --git a/proxmox-http/src/client/sync.rs b/proxmox-http/src/client/sync.rs index 7aa3a8e1..41b9c79c 100644 --- a/proxmox-http/src/client/sync.rs +++ b/proxmox-http/src/client/sync.rs @@ -106,7 +106,7 @@ impl Client { } } -impl HttpClient for Client { +impl HttpClient for Client { fn get( &self, uri: &str, @@ -118,21 +118,18 @@ impl HttpClient for Client { self.call(req).and_then(Self::convert_response_to_string) } - fn post( + fn post( &self, uri: &str, - body: Option, + body: Option, content_type: Option<&str>, extra_headers: Option<&HashMap>, - ) -> Result, Error> - where - R: Read, - { + ) -> Result, Error> { let req = self.agent()?.post(uri); let req = Self::add_headers(req, content_type, extra_headers); match body { - Some(body) => self.send(req, body), + Some(body) => self.send(req, body.as_bytes()), None => self.call(req), } .and_then(Self::convert_response_to_string) @@ -157,7 +154,7 @@ impl HttpClient for Client { } } -impl HttpClient> for Client { +impl HttpClient<&[u8], Vec> for Client { fn get( &self, uri: &str, @@ -169,16 +166,13 @@ impl HttpClient> for Client { self.call(req).and_then(Self::convert_response_to_vec) } - fn post( + fn post( &self, uri: &str, - body: Option, + body: Option<&[u8]>, content_type: Option<&str>, extra_headers: Option<&HashMap>, - ) -> Result>, Error> - where - R: Read, - { + ) -> Result>, Error> { let req = self.agent()?.post(uri); let req = Self::add_headers(req, content_type, extra_headers); @@ -189,7 +183,7 @@ impl HttpClient> for Client { .and_then(Self::convert_response_to_vec) } - fn request(&self, request: http::Request>) -> Result>, Error> { + fn request(&self, request: http::Request<&[u8]>) -> Result>, Error> { let mut req = self .agent()? .request(request.method().as_str(), &request.uri().to_string()); @@ -203,12 +197,12 @@ impl HttpClient> for Client { } } - self.send(req, request.body().as_slice()) + self.send(req, *request.body()) .and_then(Self::convert_response_to_vec) } } -impl HttpClient> for Client { +impl HttpClient, Box> for Client { fn get( &self, uri: &str, @@ -220,16 +214,13 @@ impl HttpClient> for Client { self.call(req).and_then(Self::convert_response_to_reader) } - fn post( + fn post( &self, uri: &str, - body: Option, + body: Option>, content_type: Option<&str>, extra_headers: Option<&HashMap>, - ) -> Result>, Error> - where - R: Read, - { + ) -> Result>, Error> { let req = self.agent()?.post(uri); let req = Self::add_headers(req, content_type, extra_headers); diff --git a/proxmox-http/src/client_trait.rs b/proxmox-http/src/client_trait.rs index 77234821..83227649 100644 --- a/proxmox-http/src/client_trait.rs +++ b/proxmox-http/src/client_trait.rs @@ -1,24 +1,22 @@ -use std::{collections::HashMap, io::Read}; +use std::collections::HashMap; use anyhow::Error; use http::{Request, Response}; -pub trait HttpClient { +pub trait HttpClient { fn get( &self, uri: &str, extra_headers: Option<&HashMap>, - ) -> Result, Error>; + ) -> Result, Error>; - fn post( + fn post( &self, uri: &str, - body: Option, + body: Option, content_type: Option<&str>, extra_headers: Option<&HashMap>, - ) -> Result, Error> - where - R: Read; + ) -> Result, Error>; - fn request(&self, request: Request) -> Result, Error>; + fn request(&self, request: Request) -> Result, Error>; } diff --git a/proxmox-subscription/src/check.rs b/proxmox-subscription/src/check.rs index 530f7cdf..33fca579 100644 --- a/proxmox-subscription/src/check.rs +++ b/proxmox-subscription/src/check.rs @@ -18,7 +18,7 @@ lazy_static! { const SHOP_URI: &str = "https://shop.proxmox.com/modules/servers/licensing/verify.php"; /// (Re)-register a subscription key with the WHMCS server. -fn register_subscription>( +fn register_subscription>( key: &str, server_id: &str, checktime: i64, @@ -39,7 +39,7 @@ fn register_subscription>( let query = json_object_to_query(params)?; let response = client.post( SHOP_URI, - Some(&mut query.as_bytes()), + Some(query), Some("application/x-www-form-urlencoded"), None, )?; @@ -164,7 +164,7 @@ fn test_parse_register_response() -> Result<(), Error> { /// Queries the WHMCS server to register/update the subscription key information, parsing the /// response into a [SubscriptionInfo]. -pub fn check_subscription>( +pub fn check_subscription>( key: String, server_id: String, product_url: String,