mirror of
https://github.com/katanemo/plano.git
synced 2026-07-11 16:12:13 +02:00
path replacement
This commit is contained in:
parent
800e227399
commit
16f5025bc9
5 changed files with 37 additions and 9 deletions
|
|
@ -16,6 +16,7 @@ tiktoken-rs = "0.5.9"
|
|||
rand = "0.8.5"
|
||||
serde_json = "1.0"
|
||||
hex = "0.4.3"
|
||||
urlencoding = "2.1.3"
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.4.1"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use urlencoding;
|
||||
|
||||
pub fn replace_params_in_path(
|
||||
path: &str,
|
||||
|
|
@ -7,6 +8,7 @@ pub fn replace_params_in_path(
|
|||
let mut result = String::new();
|
||||
let mut in_param = false;
|
||||
let mut current_param = String::new();
|
||||
let mut vars_replaced = HashSet::new();
|
||||
|
||||
for c in path.chars() {
|
||||
if c == '{' {
|
||||
|
|
@ -15,7 +17,9 @@ pub fn replace_params_in_path(
|
|||
in_param = false;
|
||||
let param_name = current_param.clone();
|
||||
if let Some(value) = params.get(¶m_name) {
|
||||
result.push_str(value);
|
||||
let value = urlencoding::encode(value);
|
||||
result.push_str(value.into_owned().as_str());
|
||||
vars_replaced.insert(param_name.clone());
|
||||
} else {
|
||||
return Err(format!("Missing value for parameter `{}`", param_name));
|
||||
}
|
||||
|
|
@ -27,6 +31,18 @@ pub fn replace_params_in_path(
|
|||
}
|
||||
}
|
||||
|
||||
// add the remaining params in path
|
||||
for (param_name, value) in params.iter() {
|
||||
let value = urlencoding::encode(value);
|
||||
if !vars_replaced.contains(param_name) {
|
||||
if result.contains("?") {
|
||||
result.push_str(&format!("&{}={}", param_name, value));
|
||||
} else {
|
||||
result.push_str(&format!("?{}={}", param_name, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
|
|
@ -35,12 +51,18 @@ mod test {
|
|||
#[test]
|
||||
fn test_replace_path() {
|
||||
let path = "/cluster.open-cluster-management.io/v1/managedclusters/{cluster_name}";
|
||||
let params = vec![("cluster_name".to_string(), "test1".to_string())]
|
||||
.into_iter()
|
||||
.collect();
|
||||
let params = vec![
|
||||
("cluster_name".to_string(), "test1".to_string()),
|
||||
("hello".to_string(), "hello world".to_string()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
assert_eq!(
|
||||
super::replace_params_in_path(path, ¶ms),
|
||||
Ok("/cluster.open-cluster-management.io/v1/managedclusters/test1".to_string())
|
||||
Ok(
|
||||
"/cluster.open-cluster-management.io/v1/managedclusters/test1?hello=hello%20world"
|
||||
.to_string()
|
||||
)
|
||||
);
|
||||
|
||||
let path = "/cluster.open-cluster-management.io/v1/managedclusters";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue