Compare commits
No commits in common. "ac92c53799e1ac45056f2962b2bdf5ae80e636bb" and "8544d0d7202d367ab99c672d1522869244f0ac6c" have entirely different histories.
ac92c53799
...
8544d0d720
8 changed files with 80 additions and 121 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -815,7 +815,7 @@ checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
|
|||
|
||||
[[package]]
|
||||
name = "postwoman"
|
||||
version = "0.4.1"
|
||||
version = "0.4.0"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"chrono",
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
include = ["other.toml", "api/auth.toml"] # include other postwoman collections relative to this one
|
||||
|
||||
[env] # these will be replaced in fields and inherited by includes. environment vars overrule these
|
||||
PW_TOKEN = "set-me-as-and-environment-variable!"
|
||||
|
||||
[client] # HTTP client configuration
|
||||
user_agent = "postwoman@sample/0.4.1"
|
||||
timeout = 60 # max time for each request to complete, in seconds
|
||||
redirects = 5 # allow up to five redirects, defaults to none
|
||||
base = "https://api.alemi.dev" # all route urls will be appended to this base
|
||||
|
||||
[env] # these will be replaced in routes options. environment vars overrule these
|
||||
PW_TOKEN = "set-me-as-and-environment-variable!"
|
||||
|
||||
|
||||
|
||||
[route.healthcheck] # the simplest possible route: just name and path
|
||||
path = "/"
|
||||
|
|
20
src/ext.rs
20
src/ext.rs
|
@ -38,23 +38,3 @@ pub fn stringify_json(v: &serde_json::Value) -> String {
|
|||
pub fn full_name(namespace: &str, name: &str) -> String {
|
||||
format!("{namespace}:{name}")
|
||||
}
|
||||
|
||||
pub trait FillableFromEnvironment {
|
||||
fn fill(self, env: &toml::Table) -> Self;
|
||||
|
||||
fn default_vars(env: &toml::Table) -> std::collections::HashMap<String, String> {
|
||||
let mut vars: std::collections::HashMap<String, String> = std::collections::HashMap::default();
|
||||
|
||||
vars.insert("POSTWOMAN_TIMESTAMP".to_string(), chrono::Local::now().timestamp().to_string());
|
||||
|
||||
for (k, v) in env {
|
||||
vars.insert(k.to_string(), stringify_toml(v));
|
||||
}
|
||||
|
||||
for (k, v) in std::env::vars() {
|
||||
vars.insert(k, v);
|
||||
}
|
||||
|
||||
vars
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,12 +59,12 @@ impl PrintableResult for ListResult {
|
|||
for (namespace, collection) in collections {
|
||||
println!("-> {namespace}");
|
||||
|
||||
for (key, value) in collection.env {
|
||||
for (key, value) in collection.env.unwrap_or_default() {
|
||||
println!(" + {key}={}", crate::ext::stringify_toml(&value));
|
||||
}
|
||||
|
||||
for (name, endpoint) in collection.route {
|
||||
let url = endpoint.url(collection.client.base.as_deref())
|
||||
for (name, endpoint) in collection.route.unwrap_or_default() {
|
||||
let url = endpoint.url(collection.client.as_ref().and_then(|x| x.base.as_deref()))
|
||||
.split('?')
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -7,7 +7,6 @@ use std::str::FromStr;
|
|||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use ext::FillableFromEnvironment;
|
||||
use fmt::{PrintableResult, ReportableResult};
|
||||
use indexmap::IndexMap;
|
||||
pub use model::PostWomanCollection;
|
||||
|
@ -80,7 +79,7 @@ fn main() {
|
|||
|
||||
let mut collections = IndexMap::new();
|
||||
|
||||
if !load_collections(&mut collections, args.collection.clone(), &toml::Table::default()) {
|
||||
if !load_collections(&mut collections, args.collection.clone()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -148,10 +147,10 @@ async fn run_collection_endpoints(
|
|||
) {
|
||||
// this is always safe to compile because we tested it beforehand
|
||||
let pattern = regex::Regex::new(&query).expect("tested it before and still failed here???");
|
||||
let env = std::sync::Arc::new(collection.env);
|
||||
let client = std::sync::Arc::new(collection.client.fill(&env));
|
||||
let client = std::sync::Arc::new(collection.client.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() {
|
||||
let full_name = ext::full_name(&namespace, &name);
|
||||
if pattern.find(&full_name).is_none() { continue };
|
||||
|
||||
|
@ -196,7 +195,7 @@ async fn run_collection_endpoints(
|
|||
}
|
||||
}
|
||||
|
||||
fn load_collections(store: &mut IndexMap<String, PostWomanCollection>, mut path: std::path::PathBuf, parent_env: &toml::Table) -> bool {
|
||||
fn load_collections(store: &mut IndexMap<String, PostWomanCollection>, mut path: std::path::PathBuf) -> bool {
|
||||
let collection_raw = match std::fs::read_to_string(&path) {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
|
@ -205,7 +204,7 @@ fn load_collections(store: &mut IndexMap<String, PostWomanCollection>, mut path:
|
|||
},
|
||||
};
|
||||
|
||||
let mut collection: PostWomanCollection = match toml::from_str(&collection_raw) {
|
||||
let collection: PostWomanCollection = match toml::from_str(&collection_raw) {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
eprintln!("! error parsing collection {path:?}: {e}");
|
||||
|
@ -213,24 +212,23 @@ fn load_collections(store: &mut IndexMap<String, PostWomanCollection>, mut path:
|
|||
},
|
||||
};
|
||||
|
||||
collection.env.extend(parent_env.iter().map(|(k, v)| (k.clone(), v.clone())));
|
||||
|
||||
let name = path.to_string_lossy().replace(".toml", "");
|
||||
let mut to_include = Vec::new();
|
||||
|
||||
path.pop();
|
||||
for include in &collection.include {
|
||||
let mut base = path.clone();
|
||||
let new = std::path::PathBuf::from_str(include).expect("infallible");
|
||||
base.push(new);
|
||||
to_include.push(base);
|
||||
if let Some(ref includes) = collection.include {
|
||||
path.pop();
|
||||
for include in includes {
|
||||
let mut base = path.clone();
|
||||
let new = std::path::PathBuf::from_str(include).expect("infallible");
|
||||
base.push(new);
|
||||
to_include.push(base);
|
||||
}
|
||||
}
|
||||
|
||||
let parent_env = collection.env.clone();
|
||||
store.insert(name, collection);
|
||||
|
||||
for base in to_include {
|
||||
if !load_collections(store, base, &parent_env) {
|
||||
if !load_collections(store, base) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
use crate::ext::FillableFromEnvironment;
|
||||
|
||||
|
||||
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ClientConfig {
|
||||
|
@ -14,23 +12,3 @@ pub struct ClientConfig {
|
|||
/// accept invalid SSL certificates, defaults to false (be careful: this is dangerous!)
|
||||
pub accept_invalid_certs: Option<bool>,
|
||||
}
|
||||
|
||||
impl FillableFromEnvironment for ClientConfig {
|
||||
fn fill(mut self, env: &toml::Table) -> Self {
|
||||
let vars = Self::default_vars(env);
|
||||
|
||||
for (k, v) in vars {
|
||||
let k_var = format!("${{{k}}}");
|
||||
|
||||
if let Some(base) = self.base {
|
||||
self.base = Some(base.replace(&k_var, &v));
|
||||
}
|
||||
|
||||
if let Some(user_agent) = self.user_agent {
|
||||
self.user_agent = Some(user_agent.replace(&k_var, &v));
|
||||
}
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::str::FromStr;
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
|
||||
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||
use http::method::InvalidMethod;
|
||||
|
@ -8,7 +8,7 @@ use jaq_interpret::FilterT;
|
|||
use crate::errors::InvalidHeaderError;
|
||||
use crate::{PostWomanError, APP_USER_AGENT};
|
||||
|
||||
use crate::ext::{stringify_json, FillableFromEnvironment, StringOr};
|
||||
use crate::ext::{stringify_toml, stringify_json, StringOr};
|
||||
use super::{ExtractorConfig, ClientConfig};
|
||||
|
||||
|
||||
|
@ -75,6 +75,57 @@ impl EndpointConfig {
|
|||
url
|
||||
}
|
||||
|
||||
pub fn fill(mut self, env: &toml::Table) -> Self {
|
||||
let mut vars: HashMap<String, String> = HashMap::default();
|
||||
|
||||
vars.insert("POSTWOMAN_TIMESTAMP".to_string(), chrono::Local::now().timestamp().to_string());
|
||||
|
||||
for (k, v) in env {
|
||||
vars.insert(k.to_string(), stringify_toml(v));
|
||||
}
|
||||
|
||||
for (k, v) in std::env::vars() {
|
||||
vars.insert(k, v);
|
||||
}
|
||||
|
||||
for (k, v) in vars {
|
||||
let k_var = format!("${{{k}}}");
|
||||
self.path = self.path.replace(&k_var, &v);
|
||||
if let Some(method) = self.method {
|
||||
self.method = Some(method.replace(&k_var, &v));
|
||||
}
|
||||
if let Some(b) = self.body {
|
||||
match b {
|
||||
StringOr::Str(body) => {
|
||||
self.body = Some(StringOr::Str(body.replace(&k_var, &v)));
|
||||
},
|
||||
StringOr::T(json) => {
|
||||
let wrap = toml::Value::Table(json.clone());
|
||||
let toml::Value::Table(out) = replace_recursive(wrap, &k_var, &v)
|
||||
else { unreachable!("we put in a table, we get out a table") };
|
||||
self.body = Some(StringOr::T(out));
|
||||
},
|
||||
}
|
||||
}
|
||||
if let Some(query) = self.query {
|
||||
self.query = Some(
|
||||
query.into_iter()
|
||||
.map(|x| x.replace(&k_var, &v))
|
||||
.collect()
|
||||
);
|
||||
}
|
||||
if let Some(headers) = self.headers {
|
||||
self.headers = Some(
|
||||
headers.into_iter()
|
||||
.map(|x| x.replace(&k_var, &v))
|
||||
.collect()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn execute(self, opts: &ClientConfig) -> Result<String, PostWomanError> {
|
||||
let body = self.body()?;
|
||||
let method = self.method()?;
|
||||
|
@ -145,49 +196,6 @@ impl EndpointConfig {
|
|||
}
|
||||
}
|
||||
|
||||
impl FillableFromEnvironment for EndpointConfig {
|
||||
fn fill(mut self, env: &toml::Table) -> Self {
|
||||
let vars = Self::default_vars(env);
|
||||
|
||||
for (k, v) in vars {
|
||||
let k_var = format!("${{{k}}}");
|
||||
self.path = self.path.replace(&k_var, &v);
|
||||
if let Some(method) = self.method {
|
||||
self.method = Some(method.replace(&k_var, &v));
|
||||
}
|
||||
if let Some(b) = self.body {
|
||||
match b {
|
||||
StringOr::Str(body) => {
|
||||
self.body = Some(StringOr::Str(body.replace(&k_var, &v)));
|
||||
},
|
||||
StringOr::T(json) => {
|
||||
let wrap = toml::Value::Table(json.clone());
|
||||
let toml::Value::Table(out) = replace_recursive(wrap, &k_var, &v)
|
||||
else { unreachable!("we put in a table, we get out a table") };
|
||||
self.body = Some(StringOr::T(out));
|
||||
},
|
||||
}
|
||||
}
|
||||
if let Some(query) = self.query {
|
||||
self.query = Some(
|
||||
query.into_iter()
|
||||
.map(|x| x.replace(&k_var, &v))
|
||||
.collect()
|
||||
);
|
||||
}
|
||||
if let Some(headers) = self.headers {
|
||||
self.headers = Some(
|
||||
headers.into_iter()
|
||||
.map(|x| x.replace(&k_var, &v))
|
||||
.collect()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_recursive(element: toml::Value, from: &str, to: &str) -> toml::Value {
|
||||
match element {
|
||||
toml::Value::Float(x) => toml::Value::Float(x),
|
||||
|
|
|
@ -8,13 +8,9 @@ pub use extractor::ExtractorConfig;
|
|||
|
||||
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PostWomanCollection {
|
||||
#[serde(default)]
|
||||
pub client: ClientConfig,
|
||||
#[serde(default)]
|
||||
pub include: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub env: toml::Table,
|
||||
#[serde(default)]
|
||||
pub route: indexmap::IndexMap<String, EndpointConfig>,
|
||||
pub client: Option<ClientConfig>,
|
||||
pub env: Option<toml::Table>,
|
||||
pub include: Option<Vec<String>>,
|
||||
// it's weird to name it singular but makes more sense in config
|
||||
pub route: Option<indexmap::IndexMap<String, EndpointConfig>>,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue