Compare commits

..

4 commits

5 changed files with 21 additions and 11 deletions

View file

@ -34,3 +34,7 @@ pub fn stringify_json(v: &serde_json::Value) -> String {
} }
} }
// keep it as separate fn so we can change it everywhere easily
pub fn full_name(namespace: &str, name: &str) -> String {
format!("{namespace}:{name}")
}

View file

@ -63,7 +63,7 @@ impl PrintableResult for ListResult {
println!(" + {key}={}", crate::ext::stringify_toml(&value)); println!(" + {key}={}", crate::ext::stringify_toml(&value));
} }
for (name, endpoint) in collection.route { for (name, endpoint) in collection.route.unwrap_or_default() {
let url = endpoint.url(collection.client.as_ref().and_then(|x| x.base.as_deref())) let url = endpoint.url(collection.client.as_ref().and_then(|x| x.base.as_deref()))
.split('?') .split('?')
.next() .next()

View file

@ -150,8 +150,9 @@ async fn run_collection_endpoints(
let client = std::sync::Arc::new(collection.client.unwrap_or_default()); let client = std::sync::Arc::new(collection.client.unwrap_or_default());
let env = std::sync::Arc::new(collection.env.unwrap_or_default()); let env = std::sync::Arc::new(collection.env.unwrap_or_default());
for (name, mut endpoint) in collection.route { for (name, mut endpoint) in collection.route.unwrap_or_default() {
if pattern.find(&name).is_none() { continue }; let full_name = ext::full_name(&namespace, &name);
if pattern.find(&full_name).is_none() { continue };
if debug { endpoint.extract = Some(ext::StringOr::T(model::ExtractorConfig::Debug)) }; if debug { endpoint.extract = Some(ext::StringOr::T(model::ExtractorConfig::Debug)) };
let _client = client.clone(); let _client = client.clone();
@ -160,7 +161,7 @@ async fn run_collection_endpoints(
let task = async move { let task = async move {
let before = chrono::Local::now(); let before = chrono::Local::now();
eprintln!(" : [{}] {_namespace}::{name} \tsending request...", before.format(fmt::TIMESTAMP_FMT)); eprintln!(" : [{}] {full_name} \tsending request...", before.format(fmt::TIMESTAMP_FMT));
let res = if dry_run { let res = if dry_run {
Ok("".to_string()) Ok("".to_string())
@ -211,7 +212,7 @@ fn load_collections(store: &mut IndexMap<String, PostWomanCollection>, mut path:
}, },
}; };
let name = path.to_string_lossy().to_string(); let name = path.to_string_lossy().replace(".toml", "");
let mut to_include = Vec::new(); let mut to_include = Vec::new();
if let Some(ref includes) = collection.include { if let Some(ref includes) = collection.include {

View file

@ -223,11 +223,16 @@ fn replace_recursive(element: toml::Value, from: &str, to: &str) -> toml::Value
async fn format_body(res: reqwest::Response) -> Result<String, PostWomanError> { async fn format_body(res: reqwest::Response) -> Result<String, PostWomanError> {
match res.headers().get("Content-Type") { match res.headers().get("Content-Type") {
None => Ok(res.text().await? + "\n"), None => Ok(res.text().await?),
Some(v) => match v.to_str()? { Some(v) => {
"application/json" => Ok(serde_json::to_string_pretty(&res.json::<serde_json::Value>().await?)? + "\n"), let content_type = v.to_str()?;
"text/plain" | "text/html" => Ok(res.text().await? + "\n"), if content_type.starts_with("application/json") {
_ => Ok(format!("base64({})\n", BASE64_STANDARD.encode(res.bytes().await?))), Ok(serde_json::to_string_pretty(&res.json::<serde_json::Value>().await?)?)
} else if content_type.starts_with("text/plain") || content_type.starts_with("text/html") {
Ok(res.text().await?)
} else {
Ok(format!("base64({})\n", BASE64_STANDARD.encode(res.bytes().await?)))
}
}, },
} }
} }

View file

@ -12,5 +12,5 @@ pub struct PostWomanCollection {
pub env: Option<toml::Table>, pub env: Option<toml::Table>,
pub include: Option<Vec<String>>, pub include: Option<Vec<String>>,
// it's weird to name it singular but makes more sense in config // it's weird to name it singular but makes more sense in config
pub route: indexmap::IndexMap<String, EndpointConfig>, pub route: Option<indexmap::IndexMap<String, EndpointConfig>>,
} }