]> git.proxmox.com Git - proxmox.git/blobdiff - proxmox-http/src/client/sync.rs
http: concat! user agent instead of format!
[proxmox.git] / proxmox-http / src / client / sync.rs
index 228f839c0ac17e1f4f8722f84df60edde563f989..195ce98fb48cfb8caf28bdf8795e56d8202874d7 100644 (file)
@@ -7,8 +7,6 @@ use http::Response;
 use crate::HttpClient;
 use crate::HttpOptions;
 
-pub const DEFAULT_USER_AGENT_STRING: &str = "proxmox-sync-http-client/0.1";
-
 #[derive(Default)]
 /// Blocking HTTP client for usage with [`HttpClient`].
 pub struct Client {
@@ -23,12 +21,10 @@ impl Client {
     fn agent(&self) -> Result<ureq::Agent, Error> {
         let mut builder = ureq::AgentBuilder::new();
 
-        builder = builder.user_agent(
-            self.options
-                .user_agent
-                .as_deref()
-                .unwrap_or(DEFAULT_USER_AGENT_STRING),
-        );
+        builder = builder.user_agent(self.options.user_agent.as_deref().unwrap_or(concat!(
+            "proxmox-sync-http-client/",
+            env!("CARGO_PKG_VERSION")
+        )));
 
         if let Some(proxy_config) = &self.options.proxy_config {
             builder = builder.proxy(ureq::Proxy::new(proxy_config.to_proxy_string()?)?);
@@ -37,11 +33,11 @@ impl Client {
         Ok(builder.build())
     }
 
-    fn call(&self, req: ureq::Request) -> Result<ureq::Response, Error> {
+    fn call(req: ureq::Request) -> Result<ureq::Response, Error> {
         req.call().map_err(Into::into)
     }
 
-    fn send<R>(&self, req: ureq::Request, body: R) -> Result<ureq::Response, Error>
+    fn send<R>(req: ureq::Request, body: R) -> Result<ureq::Response, Error>
     where
         R: Read,
     {
@@ -109,7 +105,7 @@ impl HttpClient<String, String> for Client {
         let req = self.agent()?.get(uri);
         let req = Self::add_headers(req, None, extra_headers);
 
-        self.call(req).and_then(Self::convert_response_to_string)
+        Self::call(req).and_then(Self::convert_response_to_string)
     }
 
     fn post(
@@ -123,8 +119,8 @@ impl HttpClient<String, String> for Client {
         let req = Self::add_headers(req, content_type, extra_headers);
 
         match body {
-            Some(body) => self.send(req, body.as_bytes()),
-            None => self.call(req),
+            Some(body) => Self::send(req, body.as_bytes()),
+            None => Self::call(req),
         }
         .and_then(Self::convert_response_to_string)
     }
@@ -142,8 +138,7 @@ impl HttpClient<String, String> for Client {
             }
         }
 
-        self.send(req, request.body().as_bytes())
-            .and_then(Self::convert_response_to_string)
+        Self::send(req, request.body().as_bytes()).and_then(Self::convert_response_to_string)
     }
 }
 
@@ -156,7 +151,7 @@ impl HttpClient<&[u8], Vec<u8>> for Client {
         let req = self.agent()?.get(uri);
         let req = Self::add_headers(req, None, extra_headers);
 
-        self.call(req).and_then(Self::convert_response_to_vec)
+        Self::call(req).and_then(Self::convert_response_to_vec)
     }
 
     fn post(
@@ -170,8 +165,8 @@ impl HttpClient<&[u8], Vec<u8>> for Client {
         let req = Self::add_headers(req, content_type, extra_headers);
 
         match body {
-            Some(body) => self.send(req, body),
-            None => self.call(req),
+            Some(body) => Self::send(req, body),
+            None => Self::call(req),
         }
         .and_then(Self::convert_response_to_vec)
     }
@@ -189,8 +184,7 @@ impl HttpClient<&[u8], Vec<u8>> for Client {
             }
         }
 
-        self.send(req, *request.body())
-            .and_then(Self::convert_response_to_vec)
+        Self::send(req, *request.body()).and_then(Self::convert_response_to_vec)
     }
 }
 
@@ -203,7 +197,7 @@ impl HttpClient<Box<dyn Read>, Box<dyn Read>> for Client {
         let req = self.agent()?.get(uri);
         let req = Self::add_headers(req, None, extra_headers);
 
-        self.call(req).and_then(Self::convert_response_to_reader)
+        Self::call(req).and_then(Self::convert_response_to_reader)
     }
 
     fn post(
@@ -217,8 +211,8 @@ impl HttpClient<Box<dyn Read>, Box<dyn Read>> for Client {
         let req = Self::add_headers(req, content_type, extra_headers);
 
         match body {
-            Some(body) => self.send(req, body),
-            None => self.call(req),
+            Some(body) => Self::send(req, body),
+            None => Self::call(req),
         }
         .and_then(Self::convert_response_to_reader)
     }
@@ -238,7 +232,6 @@ impl HttpClient<Box<dyn Read>, Box<dyn Read>> for Client {
             }
         }
 
-        self.send(req, Box::new(request.body_mut()))
-            .and_then(Self::convert_response_to_reader)
+        Self::send(req, Box::new(request.body_mut())).and_then(Self::convert_response_to_reader)
     }
 }