Compare commits

..

No commits in common. "a5cef7f2693f310025dfed05b82820e7408c9235" and "1fdd40a68ee518f8a2330a3eb6b0e1fa8828ed66" have entirely different histories.

9 changed files with 189 additions and 184 deletions

71
Cargo.lock generated
View file

@ -665,6 +665,16 @@ version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
[[package]]
name = "lock_api"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.22"
@ -789,6 +799,29 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "parking_lot"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
]
[[package]]
name = "percent-encoding"
version = "2.3.1"
@ -852,6 +885,15 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f"
dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.11.0"
@ -1012,6 +1054,12 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "security-framework"
version = "2.11.1"
@ -1094,6 +1142,15 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
dependencies = [
"libc",
]
[[package]]
name = "slab"
version = "0.4.9"
@ -1236,11 +1293,25 @@ dependencies = [
"bytes",
"libc",
"mio",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys 0.52.0",
]
[[package]]
name = "tokio-macros"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio-native-tls"
version = "0.3.1"

View file

@ -22,13 +22,6 @@ reqwest = { version = "0.12", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0.64"
tokio = { version = "1.40", features = ["rt-multi-thread"] }
tokio = { version = "1.40", features = ["full"] }
toml = { version = "0.8", features = ["preserve_order"] }
toml_edit = { version = "0.22", features = ["serde"] } # only to pretty print tables ...
[profile.release]
opt-level = "z"
lto = true
codegen-units = 1
strip = "symbols"
panic = "abort"

View file

@ -1,7 +1,5 @@
[client] # HTTP client configuration
user_agent = "postwoman@sample/0.2.0"
timeout = 60 # max time for each request to complete, in seconds
redirects = 5 # allow up to five redirects, defaults to none
[env] # these will be replaced in routes options. environment vars overrule these
PW_TOKEN = "set-me-as-and-environment-variable!"

View file

@ -7,8 +7,11 @@ pub enum PostWomanError {
#[error("invalid method: {0:?}")]
InvalidMethod(#[from] http::method::InvalidMethod),
#[error("invalid header: {0:?}")]
InvalidHeader(#[from] InvalidHeaderError),
#[error("invalid header name: {0:?}")]
InvalidHeaderName(#[from] reqwest::header::InvalidHeaderName),
#[error("invalid header value: {0:?}")]
InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
#[error("contains Unprintable characters: {0:?}")]
Unprintable(#[from] reqwest::header::ToStrError),
@ -16,6 +19,9 @@ pub enum PostWomanError {
#[error("header '{0}' not found in response")]
HeaderNotFound(String),
#[error("invalid header: '{0}'")]
InvalidHeader(String),
#[error("error opening collection: {0:?}")]
ErrorOpeningCollection(#[from] std::io::Error),
@ -40,13 +46,3 @@ pub enum PostWomanError {
#[error("regex failed matching in content: {0}")]
NoMatch(String),
}
#[derive(Debug, thiserror::Error)]
pub enum InvalidHeaderError {
#[error("invalid header name: {0:?}")]
Name(#[from] http::header::InvalidHeaderName),
#[error("invalid header value: {0:?}")]
Value(#[from] http::header::InvalidHeaderValue),
#[error("invalid header format: {0}")]
Format(String)
}

View file

@ -2,11 +2,12 @@ mod model;
mod errors;
mod ext;
use std::sync::Arc;
use clap::{Parser, Subcommand};
pub use model::PostWomanCollection;
pub use errors::PostWomanError;
use model::PostWomanConfig;
pub use errors::PostWomanError;
pub static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
/// API tester and debugger from your CLI
@ -20,13 +21,9 @@ struct PostWomanArgs {
/// action to run
#[clap(subcommand)]
action: Option<PostWomanActions>,
/// start a multi-thread runtime, with multiple worker threads
#[arg(long, default_value_t = false)]
multi_threaded: bool,
}
#[derive(Subcommand, Debug)]
#[derive(Subcommand, Debug, Default)]
pub enum PostWomanActions {
/// execute specific endpoint requests
Run {
@ -47,77 +44,63 @@ pub enum PostWomanActions {
},
/// show all registered routes in current collection
List {
/// show verbose details for each route
#[arg(short = 'V', long, default_value_t = false)]
verbose: bool,
},
#[default]
List,
// Save {
// /// name for new endpoint
// name: String,
// /// url of endpoint
// url: String,
// /// method
// method: Option<String>,
// /// headers
// headers: Vec<String>,
// /// body
// body: Option<String>,
// }
}
const TIMESTAMP_FMT: &str = "%H:%M:%S%.6f";
fn main() -> Result<(), PostWomanError> {
let args = PostWomanArgs::parse();
let collection_raw = std::fs::read_to_string(&args.collection)?;
let collection: PostWomanCollection = toml::from_str(&collection_raw)?;
if args.multi_threaded {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("failed creating tokio multi-thread runtime")
.block_on(async { run_postwoman(args, collection).await })
} else {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("failed creating tokio current-thread runtime")
.block_on(async { run_postwoman(args, collection).await })
}
fn print_results(res: String, name: String, before: chrono::DateTime<chrono::Local>, suffix: String) {
let after = chrono::Local::now();
let elapsed = (after - before).num_milliseconds();
let timestamp = after.format(TIMESTAMP_FMT);
eprintln!(" + [{timestamp}] {name} {suffix}done in {elapsed}ms", );
print!("{}", res);
}
async fn run_postwoman(args: PostWomanArgs, collection: PostWomanCollection) -> Result<(), PostWomanError> {
let action = args.action.unwrap_or(PostWomanActions::List { verbose: false });
#[tokio::main]
async fn main() -> Result<(), PostWomanError> {
let args = PostWomanArgs::parse();
match action {
PostWomanActions::List { verbose } => {
let ua = collection.client.user_agent.unwrap_or(APP_USER_AGENT.to_string());
let collection = std::fs::read_to_string(args.collection)?;
let config: PostWomanConfig = toml::from_str(&collection)?;
match args.action.unwrap_or_default() {
PostWomanActions::List => {
let ua = config.client.user_agent.unwrap_or(APP_USER_AGENT.to_string());
println!("> {ua}");
for (key, value) in collection.env {
for (key, value) in config.env {
println!("+ {key}: {}", ext::stringify_toml(&value));
}
println!();
for (name, mut endpoint) in collection.route {
println!("- {name}: \t{} \t{}", endpoint.method.as_deref().unwrap_or("GET"), endpoint.url);
if verbose {
if let Some(ref query) = endpoint.query {
for query in query {
println!(" |? {query}");
}
}
if let Some(ref headers) = endpoint.headers {
for header in headers {
println!(" |: {header}");
}
}
if let Ok(body) = endpoint.body() {
println!(" |> {body}");
}
}
for (name, endpoint) in config.route {
println!("- {name}: \t{} \t{}", endpoint.method.unwrap_or("GET".into()), endpoint.url);
}
},
PostWomanActions::Run { query, parallel, repeat, debug } => {
let pattern = regex::Regex::new(&query)?;
let mut joinset = tokio::task::JoinSet::new();
let client = std::sync::Arc::new(collection.client);
let env = std::sync::Arc::new(collection.env);
for (name, mut endpoint) in collection.route {
let client = Arc::new(config.client);
let env = Arc::new(config.env);
for (name, mut endpoint) in config.route {
if pattern.find(&name).is_some() {
if debug { endpoint.extract = Some(ext::StringOr::T(model::ExtractorConfig::Debug)) };
if debug { endpoint.extract = Some(ext::StringOr::T(model::Extractor::Debug)) };
for i in 0..repeat {
let suffix = if repeat > 1 {
format!("#{} ", i+1)
@ -153,15 +136,11 @@ async fn run_postwoman(args: PostWomanArgs, collection: PostWomanCollection) ->
}
}
},
// PostWomanActions::Save { name, url, method, headers, body } => {
// todo!();
// },
}
Ok(())
}
fn print_results(res: String, name: String, before: chrono::DateTime<chrono::Local>, suffix: String) {
let after = chrono::Local::now();
let elapsed = (after - before).num_milliseconds();
let timestamp = after.format(TIMESTAMP_FMT);
eprintln!(" + [{timestamp}] {name} {suffix}done in {elapsed}ms", );
print!("{}", res);
}

View file

@ -1,12 +1,4 @@
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct ClientConfig {
/// user agent for requests, defaults to 'postwoman/<version>'
pub struct PostWomanClient {
pub user_agent: Option<String>,
/// max total duration of each request, in seconds. defaults to 30
pub timeout: Option<u64>,
/// max number of redirects to allow, defaults to 0
pub redirects: Option<usize>,
/// accept invalid SSL certificates, defaults to false (be careful: this is dangerous!)
pub accept_invalid_certs: Option<bool>,
}

View file

@ -1,20 +1,17 @@
use std::{collections::HashMap, str::FromStr};
use base64::{prelude::BASE64_STANDARD, Engine};
use http::header::{InvalidHeaderName, InvalidHeaderValue};
use http::method::InvalidMethod;
use http::{HeaderMap, HeaderName, HeaderValue};
use jaq_interpret::FilterT;
use crate::errors::InvalidHeaderError;
use crate::{PostWomanError, APP_USER_AGENT};
use crate::ext::{stringify_toml, stringify_json, StringOr};
use super::{ExtractorConfig, ClientConfig};
use super::{Extractor, PostWomanClient};
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct EndpointConfig {
pub struct Endpoint {
/// endpoint url, required
pub url: String,
/// http method for request, default GET
@ -28,45 +25,35 @@ pub struct EndpointConfig {
/// expected error code, will fail if different
pub expect: Option<u16>,
/// response extractor
pub extract: Option<StringOr<ExtractorConfig>>,
pub extract: Option<StringOr<Extractor>>,
}
impl EndpointConfig {
pub fn body(&mut self) -> Result<String, serde_json::Error> {
match self.body.take().unwrap_or_default() {
StringOr::Str(x) => Ok(x.clone()),
StringOr::T(json) => Ok(serde_json::to_string(&json)?),
}
}
pub fn method(&mut self) -> Result<reqwest::Method, InvalidMethod> {
match self.method {
Some(ref m) => Ok(reqwest::Method::from_str(m)?),
None => Ok(reqwest::Method::GET),
}
}
pub fn headers(&mut self) -> Result<HeaderMap, InvalidHeaderError> {
let mut headers = HeaderMap::default();
for header in self.headers.take().unwrap_or_default() {
let (k, v) = header.split_once(':')
.ok_or_else(|| InvalidHeaderError::Format(header.clone()))?;
headers.insert(
HeaderName::from_str(k)?,
HeaderValue::from_str(v)?
);
}
Ok(headers)
}
pub fn url(&mut self) -> String {
let mut url = self.url.clone();
if let Some(query) = self.query.take() {
url = format!("{url}?{}", query.join("&"));
}
url
fn replace_recursive(element: toml::Value, from: &str, to: &str) -> toml::Value {
match element {
toml::Value::Float(x) => toml::Value::Float(x),
toml::Value::Integer(x) => toml::Value::Integer(x),
toml::Value::Boolean(x) => toml::Value::Boolean(x),
toml::Value::Datetime(x) => toml::Value::Datetime(x),
toml::Value::String(x) => toml::Value::String(x.replace(from, to)),
toml::Value::Array(x) => toml::Value::Array(
x.into_iter().map(|x| replace_recursive(x, from, to)).collect()
),
toml::Value::Table(map) => {
let mut out = toml::map::Map::new();
for (k, v) in map {
let new_v = replace_recursive(v.clone(), from, to);
if k.contains(from) {
out.insert(k.replace(from, to), new_v);
} else {
out.insert(k.to_string(), new_v);
}
}
toml::Value::Table(out)
},
}
}
impl Endpoint {
pub fn fill(mut self, env: &toml::Table) -> Self {
let mut vars: HashMap<String, String> = HashMap::default();
@ -118,20 +105,34 @@ impl EndpointConfig {
self
}
pub async fn execute(mut self, opts: &ClientConfig) -> Result<String, PostWomanError> {
let url = self.url();
let body = self.body()?;
let method = self.method()?;
let headers = self.headers()?;
pub async fn execute(self, opts: &PostWomanClient) -> Result<String, PostWomanError> {
let method = match self.method {
Some(m) => reqwest::Method::from_str(&m)?,
None => reqwest::Method::GET,
};
let mut headers = HeaderMap::default();
for header in self.headers.unwrap_or_default() {
let (k, v) = header.split_once(':')
.ok_or_else(|| PostWomanError::InvalidHeader(header.clone()))?;
headers.insert(
HeaderName::from_str(k)?,
HeaderValue::from_str(v)?
);
}
let body = match self.body.unwrap_or_default() {
StringOr::Str(x) => x,
StringOr::T(json) => serde_json::to_string(&json)?,
};
let mut url = self.url;
if let Some(query) = self.query {
url = format!("{url}?{}", query.join("&"));
}
let client = reqwest::Client::builder()
.user_agent(opts.user_agent.as_deref().unwrap_or(APP_USER_AGENT))
.timeout(std::time::Duration::from_secs(opts.timeout.unwrap_or(30)))
.redirect(opts.redirects.map(reqwest::redirect::Policy::limited).unwrap_or(reqwest::redirect::Policy::none()))
.danger_accept_invalid_certs(opts.accept_invalid_certs.unwrap_or(false))
.build()?;
let res = client
.request(method, url)
.headers(headers)
@ -144,22 +145,22 @@ impl EndpointConfig {
}
Ok(match self.extract.unwrap_or_default() {
StringOr::T(ExtractorConfig::Discard) => "".to_string(),
StringOr::T(ExtractorConfig::Body) => format_body(res).await?,
StringOr::T(ExtractorConfig::Debug) => {
StringOr::T(Extractor::Discard) => "".to_string(),
StringOr::T(Extractor::Body) => format_body(res).await?,
StringOr::T(Extractor::Debug) => {
// TODO needless double format
let res_dbg = format!("{res:#?}");
let body = format_body(res).await?;
format!("{res_dbg}\nBody: {body}\n")
},
StringOr::T(ExtractorConfig::Header { key }) => res
StringOr::T(Extractor::Header { key }) => res
.headers()
.get(&key)
.ok_or(PostWomanError::HeaderNotFound(key))?
.to_str()?
.to_string()
+ "\n",
StringOr::T(ExtractorConfig::Regex { pattern }) => {
StringOr::T(Extractor::Regex { pattern }) => {
let pattern = regex::Regex::new(&pattern)?;
let body = format_body(res).await?;
pattern.find(&body)
@ -169,7 +170,7 @@ impl EndpointConfig {
+ "\n"
},
// bare string defaults to JQL query
StringOr::T(ExtractorConfig::JQ { query }) | StringOr::Str(query) => {
StringOr::T(Extractor::JQ { query }) | StringOr::Str(query) => {
let json: serde_json::Value = res.json().await?;
let selection = jq(&query, json)?;
if selection.len() == 1 {
@ -182,31 +183,6 @@ impl EndpointConfig {
}
}
fn replace_recursive(element: toml::Value, from: &str, to: &str) -> toml::Value {
match element {
toml::Value::Float(x) => toml::Value::Float(x),
toml::Value::Integer(x) => toml::Value::Integer(x),
toml::Value::Boolean(x) => toml::Value::Boolean(x),
toml::Value::Datetime(x) => toml::Value::Datetime(x),
toml::Value::String(x) => toml::Value::String(x.replace(from, to)),
toml::Value::Array(x) => toml::Value::Array(
x.into_iter().map(|x| replace_recursive(x, from, to)).collect()
),
toml::Value::Table(map) => {
let mut out = toml::map::Map::new();
for (k, v) in map {
let new_v = replace_recursive(v.clone(), from, to);
if k.contains(from) {
out.insert(k.replace(from, to), new_v);
} else {
out.insert(k.to_string(), new_v);
}
}
toml::Value::Table(out)
},
}
}
async fn format_body(res: reqwest::Response) -> Result<String, PostWomanError> {
match res.headers().get("Content-Type") {
None => Ok(res.text().await? + "\n"),

View file

@ -1,7 +1,7 @@
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ExtractorConfig {
pub enum Extractor {
#[default]
Body,
Debug,

View file

@ -2,14 +2,14 @@ mod client;
mod endpoint;
mod extractor;
pub use client::ClientConfig;
pub use endpoint::EndpointConfig;
pub use extractor::ExtractorConfig;
pub use client::PostWomanClient;
pub use endpoint::Endpoint;
pub use extractor::Extractor;
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct PostWomanCollection {
pub client: ClientConfig,
pub struct PostWomanConfig {
pub client: PostWomanClient,
pub env: toml::Table,
// it's weird to name it singular but makes more sense in config
pub route: indexmap::IndexMap<String, EndpointConfig>,
pub route: indexmap::IndexMap<String, Endpoint>,
}