mirror of
https://github.com/katanemo/plano.git
synced 2026-06-17 15:25:17 +02:00
add post and get method query param processing
This commit is contained in:
parent
b4aa4cf43e
commit
b960862baf
4 changed files with 431 additions and 37 deletions
|
|
@ -17,6 +17,7 @@ rand = "0.8.5"
|
|||
serde_json = "1.0"
|
||||
hex = "0.4.3"
|
||||
urlencoding = "2.1.3"
|
||||
url = "2.5.4"
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.4.1"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use std::collections::{HashMap, HashSet};
|
||||
use url::Url;
|
||||
use urlencoding;
|
||||
|
||||
use crate::configuration::Parameter;
|
||||
|
|
@ -6,13 +7,14 @@ use crate::configuration::Parameter;
|
|||
pub fn replace_params_in_path(
|
||||
path: &str,
|
||||
tool_params: &HashMap<String, String>,
|
||||
prompt_target_params: &Vec<Parameter>,
|
||||
) -> Result<String, String> {
|
||||
let mut result = String::new();
|
||||
let mut in_param = false;
|
||||
prompt_target_params: &[Parameter],
|
||||
) -> Result<(String, String, HashMap<String, String>), String> {
|
||||
let mut query_string_replaced = String::new();
|
||||
let mut current_param = String::new();
|
||||
let mut vars_replaced = HashSet::new();
|
||||
let mut params: HashMap<String, String> = HashMap::new();
|
||||
|
||||
let mut in_param = false;
|
||||
for c in path.chars() {
|
||||
if c == '{' {
|
||||
in_param = true;
|
||||
|
|
@ -21,7 +23,7 @@ pub fn replace_params_in_path(
|
|||
let param_name = current_param.clone();
|
||||
if let Some(value) = tool_params.get(¶m_name) {
|
||||
let value = urlencoding::encode(value);
|
||||
result.push_str(value.into_owned().as_str());
|
||||
query_string_replaced.push_str(value.into_owned().as_str());
|
||||
vars_replaced.insert(param_name.clone());
|
||||
} else {
|
||||
return Err(format!("Missing value for parameter `{}`", param_name));
|
||||
|
|
@ -30,19 +32,20 @@ pub fn replace_params_in_path(
|
|||
} else if in_param {
|
||||
current_param.push(c);
|
||||
} else {
|
||||
result.push(c);
|
||||
query_string_replaced.push(c);
|
||||
}
|
||||
}
|
||||
|
||||
// add the remaining params in path
|
||||
for (param_name, value) in tool_params.iter() {
|
||||
let value = urlencoding::encode(value);
|
||||
let value = urlencoding::encode(value).into_owned();
|
||||
if !vars_replaced.contains(param_name) {
|
||||
vars_replaced.insert(param_name.clone());
|
||||
if result.contains("?") {
|
||||
result.push_str(&format!("&{}={}", param_name, value));
|
||||
params.insert(param_name.clone(), value.clone());
|
||||
if query_string_replaced.contains("?") {
|
||||
query_string_replaced.push_str(&format!("&{}={}", param_name, value));
|
||||
} else {
|
||||
result.push_str(&format!("?{}={}", param_name, value));
|
||||
query_string_replaced.push_str(&format!("?{}={}", param_name, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -50,14 +53,15 @@ pub fn replace_params_in_path(
|
|||
// add default values
|
||||
for param in prompt_target_params.iter() {
|
||||
if !vars_replaced.contains(¶m.name) && param.default.is_some() {
|
||||
if result.contains("?") {
|
||||
result.push_str(&format!(
|
||||
params.insert(param.name.clone(), param.default.clone().unwrap());
|
||||
if query_string_replaced.contains("?") {
|
||||
query_string_replaced.push_str(&format!(
|
||||
"&{}={}",
|
||||
param.name,
|
||||
param.default.as_ref().unwrap()
|
||||
));
|
||||
} else {
|
||||
result.push_str(&format!(
|
||||
query_string_replaced.push_str(&format!(
|
||||
"?{}={}",
|
||||
param.name,
|
||||
param.default.as_ref().unwrap()
|
||||
|
|
@ -66,11 +70,20 @@ pub fn replace_params_in_path(
|
|||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
let parsed_uri = Url::parse("http://dummy.com").unwrap();
|
||||
let parsed_uri = parsed_uri
|
||||
.join(&query_string_replaced)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let query_string = parsed_uri.query().unwrap_or("");
|
||||
let path_uri = parsed_uri.path();
|
||||
|
||||
Ok((path_uri.to_string(), query_string.to_string(), params))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::configuration::Parameter;
|
||||
|
||||
#[test]
|
||||
|
|
@ -93,21 +106,32 @@ mod test {
|
|||
format: None,
|
||||
}];
|
||||
|
||||
let out_params: HashMap<String, String> = vec![
|
||||
("country".to_string(), "US".to_string()),
|
||||
("hello".to_string(), "hello%20world".to_string()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
assert_eq!(
|
||||
super::replace_params_in_path(path, ¶ms, &prompt_target_params),
|
||||
Ok(
|
||||
"/cluster.open-cluster-management.io/v1/managedclusters/test1?hello=hello%20world&country=US"
|
||||
.to_string()
|
||||
)
|
||||
Ok((
|
||||
"/cluster.open-cluster-management.io/v1/managedclusters/test1".to_string(),
|
||||
"hello=hello%20world&country=US".to_string(),
|
||||
out_params.clone()
|
||||
))
|
||||
);
|
||||
|
||||
let out_params = HashMap::new();
|
||||
let prompt_target_params = vec![];
|
||||
|
||||
let path = "/cluster.open-cluster-management.io/v1/managedclusters";
|
||||
let params = vec![].into_iter().collect();
|
||||
assert_eq!(
|
||||
super::replace_params_in_path(path, ¶ms, &prompt_target_params),
|
||||
Ok("/cluster.open-cluster-management.io/v1/managedclusters".to_string())
|
||||
Ok((
|
||||
"/cluster.open-cluster-management.io/v1/managedclusters".to_string(),
|
||||
"".to_string(),
|
||||
out_params
|
||||
))
|
||||
);
|
||||
|
||||
let path = "/foo/{bar}/baz";
|
||||
|
|
@ -116,7 +140,7 @@ mod test {
|
|||
.collect();
|
||||
assert_eq!(
|
||||
super::replace_params_in_path(path, ¶ms, &prompt_target_params),
|
||||
Ok("/foo/qux/baz".to_string())
|
||||
Ok(("/foo/qux/baz".to_string(), "".to_string(), HashMap::new()))
|
||||
);
|
||||
|
||||
let path = "/foo/{bar}/baz/{qux}";
|
||||
|
|
@ -128,7 +152,44 @@ mod test {
|
|||
.collect();
|
||||
assert_eq!(
|
||||
super::replace_params_in_path(path, ¶ms, &prompt_target_params),
|
||||
Ok("/foo/qux/baz/quux".to_string())
|
||||
Ok((
|
||||
"/foo/qux/baz/quux".to_string(),
|
||||
"".to_string(),
|
||||
HashMap::new()
|
||||
))
|
||||
);
|
||||
|
||||
let path = "/foo/{bar}/baz/{qux}?hello=world";
|
||||
let params = vec![
|
||||
("bar".to_string(), "qux".to_string()),
|
||||
("qux".to_string(), "quux".to_string()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
assert_eq!(
|
||||
super::replace_params_in_path(path, ¶ms, &prompt_target_params),
|
||||
Ok((
|
||||
"/foo/qux/baz/quux".to_string(),
|
||||
"hello=world".to_string(),
|
||||
HashMap::new()
|
||||
))
|
||||
);
|
||||
|
||||
let path = "/foo/{bar}/baz/{qux}?hello={hello}";
|
||||
let params = vec![
|
||||
("bar".to_string(), "qux".to_string()),
|
||||
("qux".to_string(), "quux".to_string()),
|
||||
("hello".to_string(), "hello world".to_string()),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
assert_eq!(
|
||||
super::replace_params_in_path(path, ¶ms, &prompt_target_params),
|
||||
Ok((
|
||||
"/foo/qux/baz/quux".to_string(),
|
||||
"hello=hello%20world".to_string(),
|
||||
HashMap::new()
|
||||
))
|
||||
);
|
||||
|
||||
let path = "/foo/{bar}/baz/{qux}";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue