all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [pdm-devel] [PATCH proxmox-datacenter-manager 1/2] pdm-client: make use of query builder
@ 2025-03-26 15:06 Maximiliano Sandoval
  2025-03-26 15:06 ` [pdm-devel] [PATCH proxmox-datacenter-manager 2/2] pbs-client: " Maximiliano Sandoval
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Maximiliano Sandoval @ 2025-03-26 15:06 UTC (permalink / raw)
  To: pdm-devel

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---

This is a continuation of the RFC [1] and depends on the patch at [2].

[1] https://lore.proxmox.com/pdm-devel/7qh5kchmrg4rlavkowqx44jiamcznarxn4konxww24zj7msuuv@gshqpr4ldxui/T/#u
[2] https://lore.proxmox.com/pbs-devel/20250326144410.430616-1-m.sandoval@proxmox.com/T/#u

 lib/pdm-client/src/lib.rs | 164 ++++++++++++++++++++------------------
 1 file changed, 85 insertions(+), 79 deletions(-)

diff --git a/lib/pdm-client/src/lib.rs b/lib/pdm-client/src/lib.rs
index a41b82c..61a8ebd 100644
--- a/lib/pdm-client/src/lib.rs
+++ b/lib/pdm-client/src/lib.rs
@@ -12,7 +12,7 @@ use pve_api_types::StartQemuMigrationType;
 use serde::{Deserialize, Serialize};
 use serde_json::{json, Value};
 
-use proxmox_client::{Error, HttpApiClient};
+use proxmox_client::{ApiPathBuilder, Error, HttpApiClient};
 use proxmox_rrd_api_types::{RrdMode, RrdTimeframe};
 
 use types::*;
@@ -134,13 +134,9 @@ impl<T: HttpApiClient> PdmClient<T> {
     }
 
     pub async fn list_users(&self, include_api_tokens: bool) -> Result<Vec<UserWithTokens>, Error> {
-        let mut path = "/api2/extjs/access/users".to_string();
-        add_query_arg(
-            &mut path,
-            &mut '?',
-            "include_tokens",
-            &Some(include_api_tokens),
-        );
+        let path = ApiPathBuilder::new("/api2/extjs/access/users")
+            .arg("include_tokens", include_api_tokens)
+            .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -221,8 +217,9 @@ impl<T: HttpApiClient> PdmClient<T> {
         password: Option<&str>,
         id: &str,
     ) -> Result<(), proxmox_client::Error> {
-        let mut path = format!("/api2/extjs/access/tfa/{userid}/{id}");
-        add_query_arg(&mut path, &mut '?', "password", &password);
+        let path = ApiPathBuilder::new(format!("/api2/extjs/access/tfa/{userid}/{id}"))
+            .maybe_arg("password", &password)
+            .build();
         self.0.delete(&path).await?.nodata()
     }
 
@@ -346,8 +343,9 @@ impl<T: HttpApiClient> PdmClient<T> {
         remote: &str,
         kind: Option<pve_api_types::ClusterResourceKind>,
     ) -> Result<Vec<PveResource>, Error> {
-        let mut query = format!("/api2/extjs/pve/remotes/{remote}/resources");
-        add_query_arg(&mut query, &mut '?', "kind", &kind);
+        let query = ApiPathBuilder::new(format!("/api2/extjs/pve/remotes/{remote}/resources"))
+            .maybe_arg("kind", &kind)
+            .build();
         Ok(self.0.get(&query).await?.expect_json()?.data)
     }
 
@@ -356,8 +354,9 @@ impl<T: HttpApiClient> PdmClient<T> {
         remote: &str,
         target_endpoint: Option<&str>,
     ) -> Result<Vec<ClusterNodeStatus>, Error> {
-        let mut query = format!("/api2/extjs/pve/remotes/{remote}/cluster-status");
-        add_query_arg(&mut query, &mut '?', "target-endpoint", &target_endpoint);
+        let query = ApiPathBuilder::new(format!("/api2/extjs/pve/remotes/{remote}/cluster-status"))
+            .maybe_arg("target-endpoint", &target_endpoint)
+            .build();
         Ok(self.0.get(&query).await?.expect_json()?.data)
     }
 
@@ -366,8 +365,9 @@ impl<T: HttpApiClient> PdmClient<T> {
         remote: &str,
         node: Option<&str>,
     ) -> Result<Vec<pve_api_types::VmEntry>, Error> {
-        let mut path = format!("/api2/extjs/pve/remotes/{remote}/qemu");
-        add_query_arg(&mut path, &mut '?', "node", &node);
+        let path = ApiPathBuilder::new(format!("/api2/extjs/pve/remotes/{remote}/qemu"))
+            .maybe_arg("node", &node)
+            .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -376,8 +376,9 @@ impl<T: HttpApiClient> PdmClient<T> {
         remote: &str,
         node: Option<&str>,
     ) -> Result<Vec<pve_api_types::VmEntry>, Error> {
-        let mut path = format!("/api2/extjs/pve/remotes/{remote}/lxc");
-        add_query_arg(&mut path, &mut '?', "node", &node);
+        let path = ApiPathBuilder::new(format!("/api2/extjs/pve/remotes/{remote}/lxc"))
+            .maybe_arg("node", &node)
+            .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -389,11 +390,13 @@ impl<T: HttpApiClient> PdmClient<T> {
         state: ConfigurationState,
         snapshot: Option<&str>,
     ) -> Result<pve_api_types::QemuConfig, Error> {
-        let mut path = format!("/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/config");
-        let mut sep = '?';
-        add_query_arg(&mut path, &mut sep, "state", &Some(&state));
-        add_query_arg(&mut path, &mut sep, "node", &node);
-        add_query_arg(&mut path, &mut sep, "snapshot", &snapshot);
+        let path = ApiPathBuilder::new(format!(
+            "/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/config"
+        ))
+        .arg("state", state)
+        .maybe_arg("node", &node)
+        .maybe_arg("snapshot", &snapshot)
+        .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -403,9 +406,11 @@ impl<T: HttpApiClient> PdmClient<T> {
         node: Option<&str>,
         vmid: u32,
     ) -> Result<pve_api_types::QemuStatus, Error> {
-        let mut path = format!("/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/status");
-        let mut sep = '?';
-        add_query_arg(&mut path, &mut sep, "node", &node);
+        let path = ApiPathBuilder::new(format!(
+            "/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/status"
+        ))
+        .maybe_arg("node", &node)
+        .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -415,9 +420,11 @@ impl<T: HttpApiClient> PdmClient<T> {
         node: Option<&str>,
         vmid: u32,
     ) -> Result<pve_api_types::LxcStatus, Error> {
-        let mut path = format!("/api2/extjs/pve/remotes/{remote}/lxc/{vmid}/status");
-        let mut sep = '?';
-        add_query_arg(&mut path, &mut sep, "node", &node);
+        let path = ApiPathBuilder::new(format!(
+            "/api2/extjs/pve/remotes/{remote}/lxc/{vmid}/status"
+        ))
+        .maybe_arg("node", &node)
+        .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -526,11 +533,13 @@ impl<T: HttpApiClient> PdmClient<T> {
         state: ConfigurationState,
         snapshot: Option<&str>,
     ) -> Result<pve_api_types::LxcConfig, Error> {
-        let mut path = format!("/api2/extjs/pve/remotes/{remote}/lxc/{vmid}/config");
-        let mut sep = '?';
-        add_query_arg(&mut path, &mut sep, "node", &node);
-        add_query_arg(&mut path, &mut sep, "state", &Some(&state));
-        add_query_arg(&mut path, &mut sep, "snapshot", &snapshot);
+        let path = ApiPathBuilder::new(format!(
+            "/api2/extjs/pve/remotes/{remote}/lxc/{vmid}/config"
+        ))
+        .maybe_arg("node", &node)
+        .arg("state", state)
+        .maybe_arg("snapshot", &snapshot)
+        .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -620,9 +629,9 @@ impl<T: HttpApiClient> PdmClient<T> {
         remote: &str,
         node: Option<&str>,
     ) -> Result<Vec<pve_api_types::ListTasksResponse>, Error> {
-        let mut query = format!("/api2/extjs/pve/remotes/{remote}/tasks");
-        let mut sep = '?';
-        pve_api_types::client::add_query_arg(&mut query, &mut sep, "node", &node);
+        let query = ApiPathBuilder::new(format!("/api2/extjs/pve/remotes/{remote}/tasks"))
+            .maybe_arg("node", &node)
+            .build();
         Ok(self.0.get(&query).await?.expect_json()?.data)
     }
 
@@ -657,9 +666,10 @@ impl<T: HttpApiClient> PdmClient<T> {
         path: Option<&str>,
         exact: bool,
     ) -> Result<(Vec<AclListItem>, Option<ConfigDigest>), Error> {
-        let mut query = format!("/api2/extjs/access/acl?exact={}", exact as u8);
-        let mut sep = '?';
-        pve_api_types::client::add_query_arg(&mut query, &mut sep, "path", &path);
+        let query = ApiPathBuilder::new("/api2/extjs/access/acl")
+            .arg("exact", exact as u8)
+            .maybe_arg("path", &path)
+            .build();
         let mut res = self.0.get(&query).await?.expect_json()?;
         Ok((res.data, res.attribs.remove("digest").map(ConfigDigest)))
     }
@@ -747,8 +757,11 @@ impl<T: HttpApiClient> PdmClient<T> {
         store: &str,
         namespace: Option<&str>,
     ) -> Result<Vec<pbs_api_types::SnapshotListItem>, Error> {
-        let mut path = format!("/api2/extjs/pbs/remotes/{remote}/datastore/{store}/snapshots");
-        add_query_arg(&mut path, &mut '?', "ns", &namespace);
+        let path = ApiPathBuilder::new(format!(
+            "/api2/extjs/pbs/remotes/{remote}/datastore/{store}/snapshots"
+        ))
+        .maybe_arg("ns", &namespace)
+        .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -770,16 +783,19 @@ impl<T: HttpApiClient> PdmClient<T> {
         mode: RrdMode,
         timeframe: RrdTimeframe,
     ) -> Result<Vec<PbsDatastoreDataPoint>, Error> {
-        let mut path = format!("/api2/extjs/pbs/remotes/{remote}/datastore/{store}/rrddata");
-        let mut sep = '?';
-        add_query_arg(&mut path, &mut sep, "cf", &Some(mode));
-        add_query_arg(&mut path, &mut sep, "timeframe", &Some(timeframe));
+        let path = ApiPathBuilder::new(format!(
+            "/api2/extjs/pbs/remotes/{remote}/datastore/{store}/rrddata"
+        ))
+        .arg("cf", mode)
+        .arg("timeframe", timeframe)
+        .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
     pub async fn resources(&self, max_age: Option<u64>) -> Result<Vec<RemoteResources>, Error> {
-        let mut path = "/api2/extjs/resources/list".to_string();
-        add_query_arg(&mut path, &mut '?', "max-age", &max_age);
+        let path = ApiPathBuilder::new("/api2/extjs/resources/list")
+            .maybe_arg("max-age", &max_age)
+            .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -789,9 +805,11 @@ impl<T: HttpApiClient> PdmClient<T> {
         node: &str,
         interface_type: Option<ListNetworksType>,
     ) -> Result<Vec<NetworkInterface>, Error> {
-        let mut path = format!("/api2/extjs/pve/remotes/{remote}/nodes/{node}/network");
-        let mut sep = '?';
-        add_query_arg(&mut path, &mut sep, "interface-type", &interface_type);
+        let path = ApiPathBuilder::new(format!(
+            "/api2/extjs/pve/remotes/{remote}/nodes/{node}/network"
+        ))
+        .maybe_arg("interface-type", &interface_type)
+        .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -805,17 +823,20 @@ impl<T: HttpApiClient> PdmClient<T> {
         storage: Option<String>,
         target: Option<String>,
     ) -> Result<Vec<StorageInfo>, Error> {
-        let mut path = format!("/api2/extjs/pve/remotes/{remote}/nodes/{node}/storage");
-        let mut sep = '?';
-        add_query_arg(&mut path, &mut sep, "enabled", &enabled);
-        add_query_arg(&mut path, &mut sep, "format", &format);
-        add_query_arg(&mut path, &mut sep, "storage", &storage);
-        add_query_arg(&mut path, &mut sep, "target", &target);
+        let mut builder = ApiPathBuilder::new(format!(
+            "/api2/extjs/pve/remotes/{remote}/nodes/{node}/storage"
+        ))
+        .maybe_arg("enabled", &enabled)
+        .maybe_arg("format", &format)
+        .maybe_arg("storage", &storage)
+        .maybe_arg("target", &target);
         if let Some(content) = content {
             for ty in content {
-                add_query_arg(&mut path, &mut sep, "content", &Some(ty));
+                builder = builder.arg("content", ty);
             }
         }
+        let path = builder.build();
+
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 
@@ -836,10 +857,12 @@ impl<T: HttpApiClient> PdmClient<T> {
         vmid: u32,
         target: Option<String>,
     ) -> Result<QemuMigratePreconditions, Error> {
-        let mut path = format!("/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/migrate");
-        let mut sep = '?';
-        add_query_arg(&mut path, &mut sep, "node", &node);
-        add_query_arg(&mut path, &mut sep, "target", &target);
+        let path = ApiPathBuilder::new(format!(
+            "/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/migrate"
+        ))
+        .maybe_arg("node", &node)
+        .maybe_arg("target", &target)
+        .build();
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
 }
@@ -1150,23 +1173,6 @@ impl AddTfaEntry {
     }
 }
 
-/// Add an optional string parameter to the query, and if it was added, change `separator` to `&`.
-pub fn add_query_arg<T>(query: &mut String, separator: &mut char, name: &str, value: &Option<T>)
-where
-    T: std::fmt::Display,
-{
-    if let Some(value) = value {
-        query.push(*separator);
-        *separator = '&';
-        query.push_str(name);
-        query.push('=');
-        query.extend(percent_encoding::percent_encode(
-            value.to_string().as_bytes(),
-            percent_encoding::NON_ALPHANUMERIC,
-        ));
-    }
-}
-
 /// ACL entries are either for a user or for a group.
 #[derive(Clone, Serialize)]
 pub enum AclRecipient<'a> {
-- 
2.39.5



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [pdm-devel] [PATCH proxmox-datacenter-manager 2/2] pbs-client: make use of query builder
  2025-03-26 15:06 [pdm-devel] [PATCH proxmox-datacenter-manager 1/2] pdm-client: make use of query builder Maximiliano Sandoval
@ 2025-03-26 15:06 ` Maximiliano Sandoval
  2025-03-28 12:37   ` Max Carrara
  2025-03-28 12:37 ` [pdm-devel] [PATCH proxmox-datacenter-manager 1/2] pdm-client: " Max Carrara
  2025-05-06 11:57 ` [pdm-devel] applied-series: " Wolfgang Bumiller
  2 siblings, 1 reply; 5+ messages in thread
From: Maximiliano Sandoval @ 2025-03-26 15:06 UTC (permalink / raw)
  To: pdm-devel

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
 server/src/pbs_client.rs | 33 +++++++++------------------------
 1 file changed, 9 insertions(+), 24 deletions(-)

diff --git a/server/src/pbs_client.rs b/server/src/pbs_client.rs
index c4115c1..20d31f2 100644
--- a/server/src/pbs_client.rs
+++ b/server/src/pbs_client.rs
@@ -7,7 +7,7 @@
 use anyhow::bail; // don't import Error as default error in here
 use serde::Deserialize;
 
-use proxmox_client::{Error, HttpApiClient};
+use proxmox_client::{ApiPathBuilder, Error, HttpApiClient};
 use proxmox_router::stream::JsonRecords;
 use proxmox_schema::api;
 use proxmox_section_config::typed::SectionConfigData;
@@ -100,8 +100,10 @@ impl PbsClient {
         datastore: &str,
         namespace: Option<&str>,
     ) -> Result<JsonRecords<pbs_api_types::SnapshotListItem>, anyhow::Error> {
-        let mut path = format!("/api2/extjs/admin/datastore/{datastore}/snapshots");
-        add_query_arg(&mut path, &mut '?', "ns", &namespace);
+        let path =
+            ApiPathBuilder::new(format!("/api2/extjs/admin/datastore/{datastore}/snapshots"))
+                .maybe_arg("ns", &namespace)
+                .build();
         let response = self
             .0
             .streaming_request(http::Method::GET, &path, None::<()>)
@@ -169,10 +171,10 @@ impl PbsClient {
         history: Option<bool>,
         start_time: Option<i64>,
     ) -> Result<pbs_api_types::Metrics, Error> {
-        let mut path = "/api2/extjs/status/metrics".to_string();
-        let mut sep = '?';
-        add_query_arg(&mut path, &mut sep, "history", &history);
-        add_query_arg(&mut path, &mut sep, "start-time", &start_time);
+        let path = ApiPathBuilder::new("/api2/extjs/status/metrics")
+            .maybe_arg("history", &history)
+            .maybe_arg("start-time", &start_time)
+            .build();
 
         Ok(self.0.get(&path).await?.expect_json()?.data)
     }
@@ -192,20 +194,3 @@ impl PbsClient {
 struct JsonData<T> {
     data: T,
 }
-
-/// Add an optional string parameter to the query, and if it was added, change `separator` to `&`.
-fn add_query_arg<T>(query: &mut String, separator: &mut char, name: &str, value: &Option<T>)
-where
-    T: std::fmt::Display,
-{
-    if let Some(value) = value {
-        query.push(*separator);
-        *separator = '&';
-        query.push_str(name);
-        query.push('=');
-        query.extend(percent_encoding::percent_encode(
-            value.to_string().as_bytes(),
-            percent_encoding::NON_ALPHANUMERIC,
-        ));
-    }
-}
-- 
2.39.5



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pdm-devel] [PATCH proxmox-datacenter-manager 1/2] pdm-client: make use of query builder
  2025-03-26 15:06 [pdm-devel] [PATCH proxmox-datacenter-manager 1/2] pdm-client: make use of query builder Maximiliano Sandoval
  2025-03-26 15:06 ` [pdm-devel] [PATCH proxmox-datacenter-manager 2/2] pbs-client: " Maximiliano Sandoval
@ 2025-03-28 12:37 ` Max Carrara
  2025-05-06 11:57 ` [pdm-devel] applied-series: " Wolfgang Bumiller
  2 siblings, 0 replies; 5+ messages in thread
From: Max Carrara @ 2025-03-28 12:37 UTC (permalink / raw)
  To: Proxmox Datacenter Manager development discussion

On Wed Mar 26, 2025 at 4:06 PM CET, Maximiliano Sandoval wrote:
> Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
> ---
>
> This is a continuation of the RFC [1] and depends on the patch at [2].

Having also looked at the patch for proxmox-client [2], this seems to
be pretty straightforward. LGTM.

Consider:

Reviewed-by: Max Carrara <m.carrara@proxmox.com>

>
> [1] https://lore.proxmox.com/pdm-devel/7qh5kchmrg4rlavkowqx44jiamcznarxn4konxww24zj7msuuv@gshqpr4ldxui/T/#u
> [2] https://lore.proxmox.com/pbs-devel/20250326144410.430616-1-m.sandoval@proxmox.com/T/#u
>
>  lib/pdm-client/src/lib.rs | 164 ++++++++++++++++++++------------------
>  1 file changed, 85 insertions(+), 79 deletions(-)
>
> diff --git a/lib/pdm-client/src/lib.rs b/lib/pdm-client/src/lib.rs
> index a41b82c..61a8ebd 100644
> --- a/lib/pdm-client/src/lib.rs
> +++ b/lib/pdm-client/src/lib.rs
> @@ -12,7 +12,7 @@ use pve_api_types::StartQemuMigrationType;
>  use serde::{Deserialize, Serialize};
>  use serde_json::{json, Value};
>  
> -use proxmox_client::{Error, HttpApiClient};
> +use proxmox_client::{ApiPathBuilder, Error, HttpApiClient};
>  use proxmox_rrd_api_types::{RrdMode, RrdTimeframe};
>  
>  use types::*;
> @@ -134,13 +134,9 @@ impl<T: HttpApiClient> PdmClient<T> {
>      }
>  
>      pub async fn list_users(&self, include_api_tokens: bool) -> Result<Vec<UserWithTokens>, Error> {
> -        let mut path = "/api2/extjs/access/users".to_string();
> -        add_query_arg(
> -            &mut path,
> -            &mut '?',
> -            "include_tokens",
> -            &Some(include_api_tokens),
> -        );
> +        let path = ApiPathBuilder::new("/api2/extjs/access/users")
> +            .arg("include_tokens", include_api_tokens)
> +            .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -221,8 +217,9 @@ impl<T: HttpApiClient> PdmClient<T> {
>          password: Option<&str>,
>          id: &str,
>      ) -> Result<(), proxmox_client::Error> {
> -        let mut path = format!("/api2/extjs/access/tfa/{userid}/{id}");
> -        add_query_arg(&mut path, &mut '?', "password", &password);
> +        let path = ApiPathBuilder::new(format!("/api2/extjs/access/tfa/{userid}/{id}"))
> +            .maybe_arg("password", &password)
> +            .build();
>          self.0.delete(&path).await?.nodata()
>      }
>  
> @@ -346,8 +343,9 @@ impl<T: HttpApiClient> PdmClient<T> {
>          remote: &str,
>          kind: Option<pve_api_types::ClusterResourceKind>,
>      ) -> Result<Vec<PveResource>, Error> {
> -        let mut query = format!("/api2/extjs/pve/remotes/{remote}/resources");
> -        add_query_arg(&mut query, &mut '?', "kind", &kind);
> +        let query = ApiPathBuilder::new(format!("/api2/extjs/pve/remotes/{remote}/resources"))
> +            .maybe_arg("kind", &kind)
> +            .build();
>          Ok(self.0.get(&query).await?.expect_json()?.data)
>      }
>  
> @@ -356,8 +354,9 @@ impl<T: HttpApiClient> PdmClient<T> {
>          remote: &str,
>          target_endpoint: Option<&str>,
>      ) -> Result<Vec<ClusterNodeStatus>, Error> {
> -        let mut query = format!("/api2/extjs/pve/remotes/{remote}/cluster-status");
> -        add_query_arg(&mut query, &mut '?', "target-endpoint", &target_endpoint);
> +        let query = ApiPathBuilder::new(format!("/api2/extjs/pve/remotes/{remote}/cluster-status"))
> +            .maybe_arg("target-endpoint", &target_endpoint)
> +            .build();
>          Ok(self.0.get(&query).await?.expect_json()?.data)
>      }
>  
> @@ -366,8 +365,9 @@ impl<T: HttpApiClient> PdmClient<T> {
>          remote: &str,
>          node: Option<&str>,
>      ) -> Result<Vec<pve_api_types::VmEntry>, Error> {
> -        let mut path = format!("/api2/extjs/pve/remotes/{remote}/qemu");
> -        add_query_arg(&mut path, &mut '?', "node", &node);
> +        let path = ApiPathBuilder::new(format!("/api2/extjs/pve/remotes/{remote}/qemu"))
> +            .maybe_arg("node", &node)
> +            .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -376,8 +376,9 @@ impl<T: HttpApiClient> PdmClient<T> {
>          remote: &str,
>          node: Option<&str>,
>      ) -> Result<Vec<pve_api_types::VmEntry>, Error> {
> -        let mut path = format!("/api2/extjs/pve/remotes/{remote}/lxc");
> -        add_query_arg(&mut path, &mut '?', "node", &node);
> +        let path = ApiPathBuilder::new(format!("/api2/extjs/pve/remotes/{remote}/lxc"))
> +            .maybe_arg("node", &node)
> +            .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -389,11 +390,13 @@ impl<T: HttpApiClient> PdmClient<T> {
>          state: ConfigurationState,
>          snapshot: Option<&str>,
>      ) -> Result<pve_api_types::QemuConfig, Error> {
> -        let mut path = format!("/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/config");
> -        let mut sep = '?';
> -        add_query_arg(&mut path, &mut sep, "state", &Some(&state));
> -        add_query_arg(&mut path, &mut sep, "node", &node);
> -        add_query_arg(&mut path, &mut sep, "snapshot", &snapshot);
> +        let path = ApiPathBuilder::new(format!(
> +            "/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/config"
> +        ))
> +        .arg("state", state)
> +        .maybe_arg("node", &node)
> +        .maybe_arg("snapshot", &snapshot)
> +        .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -403,9 +406,11 @@ impl<T: HttpApiClient> PdmClient<T> {
>          node: Option<&str>,
>          vmid: u32,
>      ) -> Result<pve_api_types::QemuStatus, Error> {
> -        let mut path = format!("/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/status");
> -        let mut sep = '?';
> -        add_query_arg(&mut path, &mut sep, "node", &node);
> +        let path = ApiPathBuilder::new(format!(
> +            "/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/status"
> +        ))
> +        .maybe_arg("node", &node)
> +        .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -415,9 +420,11 @@ impl<T: HttpApiClient> PdmClient<T> {
>          node: Option<&str>,
>          vmid: u32,
>      ) -> Result<pve_api_types::LxcStatus, Error> {
> -        let mut path = format!("/api2/extjs/pve/remotes/{remote}/lxc/{vmid}/status");
> -        let mut sep = '?';
> -        add_query_arg(&mut path, &mut sep, "node", &node);
> +        let path = ApiPathBuilder::new(format!(
> +            "/api2/extjs/pve/remotes/{remote}/lxc/{vmid}/status"
> +        ))
> +        .maybe_arg("node", &node)
> +        .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -526,11 +533,13 @@ impl<T: HttpApiClient> PdmClient<T> {
>          state: ConfigurationState,
>          snapshot: Option<&str>,
>      ) -> Result<pve_api_types::LxcConfig, Error> {
> -        let mut path = format!("/api2/extjs/pve/remotes/{remote}/lxc/{vmid}/config");
> -        let mut sep = '?';
> -        add_query_arg(&mut path, &mut sep, "node", &node);
> -        add_query_arg(&mut path, &mut sep, "state", &Some(&state));
> -        add_query_arg(&mut path, &mut sep, "snapshot", &snapshot);
> +        let path = ApiPathBuilder::new(format!(
> +            "/api2/extjs/pve/remotes/{remote}/lxc/{vmid}/config"
> +        ))
> +        .maybe_arg("node", &node)
> +        .arg("state", state)
> +        .maybe_arg("snapshot", &snapshot)
> +        .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -620,9 +629,9 @@ impl<T: HttpApiClient> PdmClient<T> {
>          remote: &str,
>          node: Option<&str>,
>      ) -> Result<Vec<pve_api_types::ListTasksResponse>, Error> {
> -        let mut query = format!("/api2/extjs/pve/remotes/{remote}/tasks");
> -        let mut sep = '?';
> -        pve_api_types::client::add_query_arg(&mut query, &mut sep, "node", &node);
> +        let query = ApiPathBuilder::new(format!("/api2/extjs/pve/remotes/{remote}/tasks"))
> +            .maybe_arg("node", &node)
> +            .build();
>          Ok(self.0.get(&query).await?.expect_json()?.data)
>      }
>  
> @@ -657,9 +666,10 @@ impl<T: HttpApiClient> PdmClient<T> {
>          path: Option<&str>,
>          exact: bool,
>      ) -> Result<(Vec<AclListItem>, Option<ConfigDigest>), Error> {
> -        let mut query = format!("/api2/extjs/access/acl?exact={}", exact as u8);
> -        let mut sep = '?';
> -        pve_api_types::client::add_query_arg(&mut query, &mut sep, "path", &path);
> +        let query = ApiPathBuilder::new("/api2/extjs/access/acl")
> +            .arg("exact", exact as u8)
> +            .maybe_arg("path", &path)
> +            .build();
>          let mut res = self.0.get(&query).await?.expect_json()?;
>          Ok((res.data, res.attribs.remove("digest").map(ConfigDigest)))
>      }
> @@ -747,8 +757,11 @@ impl<T: HttpApiClient> PdmClient<T> {
>          store: &str,
>          namespace: Option<&str>,
>      ) -> Result<Vec<pbs_api_types::SnapshotListItem>, Error> {
> -        let mut path = format!("/api2/extjs/pbs/remotes/{remote}/datastore/{store}/snapshots");
> -        add_query_arg(&mut path, &mut '?', "ns", &namespace);
> +        let path = ApiPathBuilder::new(format!(
> +            "/api2/extjs/pbs/remotes/{remote}/datastore/{store}/snapshots"
> +        ))
> +        .maybe_arg("ns", &namespace)
> +        .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -770,16 +783,19 @@ impl<T: HttpApiClient> PdmClient<T> {
>          mode: RrdMode,
>          timeframe: RrdTimeframe,
>      ) -> Result<Vec<PbsDatastoreDataPoint>, Error> {
> -        let mut path = format!("/api2/extjs/pbs/remotes/{remote}/datastore/{store}/rrddata");
> -        let mut sep = '?';
> -        add_query_arg(&mut path, &mut sep, "cf", &Some(mode));
> -        add_query_arg(&mut path, &mut sep, "timeframe", &Some(timeframe));
> +        let path = ApiPathBuilder::new(format!(
> +            "/api2/extjs/pbs/remotes/{remote}/datastore/{store}/rrddata"
> +        ))
> +        .arg("cf", mode)
> +        .arg("timeframe", timeframe)
> +        .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
>      pub async fn resources(&self, max_age: Option<u64>) -> Result<Vec<RemoteResources>, Error> {
> -        let mut path = "/api2/extjs/resources/list".to_string();
> -        add_query_arg(&mut path, &mut '?', "max-age", &max_age);
> +        let path = ApiPathBuilder::new("/api2/extjs/resources/list")
> +            .maybe_arg("max-age", &max_age)
> +            .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -789,9 +805,11 @@ impl<T: HttpApiClient> PdmClient<T> {
>          node: &str,
>          interface_type: Option<ListNetworksType>,
>      ) -> Result<Vec<NetworkInterface>, Error> {
> -        let mut path = format!("/api2/extjs/pve/remotes/{remote}/nodes/{node}/network");
> -        let mut sep = '?';
> -        add_query_arg(&mut path, &mut sep, "interface-type", &interface_type);
> +        let path = ApiPathBuilder::new(format!(
> +            "/api2/extjs/pve/remotes/{remote}/nodes/{node}/network"
> +        ))
> +        .maybe_arg("interface-type", &interface_type)
> +        .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -805,17 +823,20 @@ impl<T: HttpApiClient> PdmClient<T> {
>          storage: Option<String>,
>          target: Option<String>,
>      ) -> Result<Vec<StorageInfo>, Error> {
> -        let mut path = format!("/api2/extjs/pve/remotes/{remote}/nodes/{node}/storage");
> -        let mut sep = '?';
> -        add_query_arg(&mut path, &mut sep, "enabled", &enabled);
> -        add_query_arg(&mut path, &mut sep, "format", &format);
> -        add_query_arg(&mut path, &mut sep, "storage", &storage);
> -        add_query_arg(&mut path, &mut sep, "target", &target);
> +        let mut builder = ApiPathBuilder::new(format!(
> +            "/api2/extjs/pve/remotes/{remote}/nodes/{node}/storage"
> +        ))
> +        .maybe_arg("enabled", &enabled)
> +        .maybe_arg("format", &format)
> +        .maybe_arg("storage", &storage)
> +        .maybe_arg("target", &target);
>          if let Some(content) = content {
>              for ty in content {
> -                add_query_arg(&mut path, &mut sep, "content", &Some(ty));
> +                builder = builder.arg("content", ty);
>              }
>          }
> +        let path = builder.build();
> +
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  
> @@ -836,10 +857,12 @@ impl<T: HttpApiClient> PdmClient<T> {
>          vmid: u32,
>          target: Option<String>,
>      ) -> Result<QemuMigratePreconditions, Error> {
> -        let mut path = format!("/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/migrate");
> -        let mut sep = '?';
> -        add_query_arg(&mut path, &mut sep, "node", &node);
> -        add_query_arg(&mut path, &mut sep, "target", &target);
> +        let path = ApiPathBuilder::new(format!(
> +            "/api2/extjs/pve/remotes/{remote}/qemu/{vmid}/migrate"
> +        ))
> +        .maybe_arg("node", &node)
> +        .maybe_arg("target", &target)
> +        .build();
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
>  }
> @@ -1150,23 +1173,6 @@ impl AddTfaEntry {
>      }
>  }
>  
> -/// Add an optional string parameter to the query, and if it was added, change `separator` to `&`.
> -pub fn add_query_arg<T>(query: &mut String, separator: &mut char, name: &str, value: &Option<T>)
> -where
> -    T: std::fmt::Display,
> -{
> -    if let Some(value) = value {
> -        query.push(*separator);
> -        *separator = '&';
> -        query.push_str(name);
> -        query.push('=');
> -        query.extend(percent_encoding::percent_encode(
> -            value.to_string().as_bytes(),
> -            percent_encoding::NON_ALPHANUMERIC,
> -        ));
> -    }
> -}
> -
>  /// ACL entries are either for a user or for a group.
>  #[derive(Clone, Serialize)]
>  pub enum AclRecipient<'a> {



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pdm-devel] [PATCH proxmox-datacenter-manager 2/2] pbs-client: make use of query builder
  2025-03-26 15:06 ` [pdm-devel] [PATCH proxmox-datacenter-manager 2/2] pbs-client: " Maximiliano Sandoval
@ 2025-03-28 12:37   ` Max Carrara
  0 siblings, 0 replies; 5+ messages in thread
From: Max Carrara @ 2025-03-28 12:37 UTC (permalink / raw)
  To: Proxmox Datacenter Manager development discussion

On Wed Mar 26, 2025 at 4:06 PM CET, Maximiliano Sandoval wrote:
> Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>

What I said in my response to patch 1 also applies here: Since I've
also looked at the patch for proxmox-client [1], this LGTM.

Consider:

Reviewed-by: Max Carrara <m.carrara@proxmox.com>

[1]: https://lore.proxmox.com/pbs-devel/20250326144410.430616-1-m.sandoval@proxmox.com/T/#u

> ---
>  server/src/pbs_client.rs | 33 +++++++++------------------------
>  1 file changed, 9 insertions(+), 24 deletions(-)
>
> diff --git a/server/src/pbs_client.rs b/server/src/pbs_client.rs
> index c4115c1..20d31f2 100644
> --- a/server/src/pbs_client.rs
> +++ b/server/src/pbs_client.rs
> @@ -7,7 +7,7 @@
>  use anyhow::bail; // don't import Error as default error in here
>  use serde::Deserialize;
>  
> -use proxmox_client::{Error, HttpApiClient};
> +use proxmox_client::{ApiPathBuilder, Error, HttpApiClient};
>  use proxmox_router::stream::JsonRecords;
>  use proxmox_schema::api;
>  use proxmox_section_config::typed::SectionConfigData;
> @@ -100,8 +100,10 @@ impl PbsClient {
>          datastore: &str,
>          namespace: Option<&str>,
>      ) -> Result<JsonRecords<pbs_api_types::SnapshotListItem>, anyhow::Error> {
> -        let mut path = format!("/api2/extjs/admin/datastore/{datastore}/snapshots");
> -        add_query_arg(&mut path, &mut '?', "ns", &namespace);
> +        let path =
> +            ApiPathBuilder::new(format!("/api2/extjs/admin/datastore/{datastore}/snapshots"))
> +                .maybe_arg("ns", &namespace)
> +                .build();
>          let response = self
>              .0
>              .streaming_request(http::Method::GET, &path, None::<()>)
> @@ -169,10 +171,10 @@ impl PbsClient {
>          history: Option<bool>,
>          start_time: Option<i64>,
>      ) -> Result<pbs_api_types::Metrics, Error> {
> -        let mut path = "/api2/extjs/status/metrics".to_string();
> -        let mut sep = '?';
> -        add_query_arg(&mut path, &mut sep, "history", &history);
> -        add_query_arg(&mut path, &mut sep, "start-time", &start_time);
> +        let path = ApiPathBuilder::new("/api2/extjs/status/metrics")
> +            .maybe_arg("history", &history)
> +            .maybe_arg("start-time", &start_time)
> +            .build();
>  
>          Ok(self.0.get(&path).await?.expect_json()?.data)
>      }
> @@ -192,20 +194,3 @@ impl PbsClient {
>  struct JsonData<T> {
>      data: T,
>  }
> -
> -/// Add an optional string parameter to the query, and if it was added, change `separator` to `&`.
> -fn add_query_arg<T>(query: &mut String, separator: &mut char, name: &str, value: &Option<T>)
> -where
> -    T: std::fmt::Display,
> -{
> -    if let Some(value) = value {
> -        query.push(*separator);
> -        *separator = '&';
> -        query.push_str(name);
> -        query.push('=');
> -        query.extend(percent_encoding::percent_encode(
> -            value.to_string().as_bytes(),
> -            percent_encoding::NON_ALPHANUMERIC,
> -        ));
> -    }
> -}



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [pdm-devel] applied-series: [PATCH proxmox-datacenter-manager 1/2] pdm-client: make use of query builder
  2025-03-26 15:06 [pdm-devel] [PATCH proxmox-datacenter-manager 1/2] pdm-client: make use of query builder Maximiliano Sandoval
  2025-03-26 15:06 ` [pdm-devel] [PATCH proxmox-datacenter-manager 2/2] pbs-client: " Maximiliano Sandoval
  2025-03-28 12:37 ` [pdm-devel] [PATCH proxmox-datacenter-manager 1/2] pdm-client: " Max Carrara
@ 2025-05-06 11:57 ` Wolfgang Bumiller
  2 siblings, 0 replies; 5+ messages in thread
From: Wolfgang Bumiller @ 2025-05-06 11:57 UTC (permalink / raw)
  To: Maximiliano Sandoval; +Cc: pdm-devel

applied series, thanks


_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-05-06 11:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-26 15:06 [pdm-devel] [PATCH proxmox-datacenter-manager 1/2] pdm-client: make use of query builder Maximiliano Sandoval
2025-03-26 15:06 ` [pdm-devel] [PATCH proxmox-datacenter-manager 2/2] pbs-client: " Maximiliano Sandoval
2025-03-28 12:37   ` Max Carrara
2025-03-28 12:37 ` [pdm-devel] [PATCH proxmox-datacenter-manager 1/2] pdm-client: " Max Carrara
2025-05-06 11:57 ` [pdm-devel] applied-series: " Wolfgang Bumiller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal