feat: why doing it yourself when there's a crate for that
This commit is contained in:
parent
7bf8609983
commit
97505bbd86
2 changed files with 56 additions and 46 deletions
|
@ -7,6 +7,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.3.0", features = ["derive"] }
|
clap = { version = "4.3.0", features = ["derive"] }
|
||||||
|
postman_collection = "0.2.0"
|
||||||
reqwest = { version = "0.11.18", features = ["json"] }
|
reqwest = { version = "0.11.18", features = ["json"] }
|
||||||
serde = { version = "1.0.163", features = ["derive"] }
|
serde = { version = "1.0.163", features = ["derive"] }
|
||||||
serde_json = "1.0.96"
|
serde_json = "1.0.96"
|
||||||
|
|
69
src/main.rs
69
src/main.rs
|
@ -1,11 +1,12 @@
|
||||||
mod proto;
|
// mod proto;
|
||||||
mod model;
|
mod model;
|
||||||
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use proto::Body;
|
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
|
|
||||||
use crate::proto::{Item, Request, Header};
|
use postman_collection::{PostmanCollection, v2_1_0::Spec};
|
||||||
|
|
||||||
|
// use crate::proto::{Item, Request, Header};
|
||||||
|
|
||||||
/// API tester and debugger from your CLI
|
/// API tester and debugger from your CLI
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
|
@ -43,10 +44,6 @@ pub enum PostWomanActions {
|
||||||
#[arg(short, long, default_value = "")]
|
#[arg(short, long, default_value = "")]
|
||||||
data: String,
|
data: String,
|
||||||
|
|
||||||
/// show request that is being sent
|
|
||||||
#[arg(long, default_value_t = false)]
|
|
||||||
debug: bool,
|
|
||||||
|
|
||||||
/// add action to collection items
|
/// add action to collection items
|
||||||
#[arg(short = 'S', long, default_value_t = false)]
|
#[arg(short = 'S', long, default_value_t = false)]
|
||||||
save: bool,
|
save: bool,
|
||||||
|
@ -62,26 +59,31 @@ pub enum PostWomanActions {
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let args = PostWomanArgs::parse();
|
let args = PostWomanArgs::parse();
|
||||||
|
|
||||||
let mut collection : proto::PostWomanCollection = {
|
let collection =
|
||||||
let file = std::fs::File::open(&args.collection)?;
|
match postman_collection::from_path(args.collection) {
|
||||||
serde_json::from_reader(file)?
|
Ok(PostmanCollection::V2_1_0(spec)) => spec,
|
||||||
|
_ => Spec::default(), // TODO log what is happening here!
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if args.verbose {
|
||||||
println!("╶┐ * {}", collection.info.name);
|
println!("╶┐ * {}", collection.info.name);
|
||||||
if let Some(descr) = collection.info.description {
|
if let Some(descr) = &collection.info.description {
|
||||||
println!(" │ {}", descr);
|
match descr {
|
||||||
|
postman_collection::v2_1_0::DescriptionUnion::Description(x) => {
|
||||||
|
if let Some(d) = x.content { println!(" │ {}", d) };
|
||||||
|
if let Some(v) = x.version { println!(" │ {}", v) };
|
||||||
|
},
|
||||||
|
postman_collection::v2_1_0::DescriptionUnion::String(x) => println!(" │ {}", x),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
println!(" │");
|
println!(" │");
|
||||||
|
}
|
||||||
|
|
||||||
match args.action {
|
match args.action {
|
||||||
PostWomanActions::Send {
|
PostWomanActions::Send {
|
||||||
url, headers, method, data, save, debug
|
url, headers, method, data, save
|
||||||
} => {
|
} => {
|
||||||
let item = Item {
|
let req = Request::Object {
|
||||||
name: "TODO!".into(),
|
|
||||||
event: None,
|
|
||||||
item: None,
|
|
||||||
request: Some(Request {
|
|
||||||
url: crate::proto::Url::String(url),
|
url: crate::proto::Url::String(url),
|
||||||
method: method.to_string(),
|
method: method.to_string(),
|
||||||
header: Some(
|
header: Some(
|
||||||
|
@ -93,39 +95,46 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
),
|
),
|
||||||
body: if data.len() > 0 { Some(Body::Text(data)) } else { None },
|
body: if data.len() > 0 { Some(Body::String(data)) } else { None },
|
||||||
description: None,
|
description: None,
|
||||||
}),
|
|
||||||
response: Some(vec![]),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if debug {
|
let res = req.clone().send().await?;
|
||||||
println!(" ├ {:?}", item);
|
|
||||||
}
|
|
||||||
|
|
||||||
let res = item.send().await?;
|
if args.verbose {
|
||||||
println!(" ├┐ {}", res.status());
|
println!(" ├┐ {}", res.status());
|
||||||
|
}
|
||||||
|
|
||||||
if args.verbose {
|
if args.verbose {
|
||||||
println!(" ││ {}", res.text().await?.replace("\n", "\n ││ "));
|
println!(" ││ {}", res.text().await?.replace("\n", "\n ││ "));
|
||||||
|
} else {
|
||||||
|
println!("{}", res.text().await?);
|
||||||
}
|
}
|
||||||
|
|
||||||
if save {
|
if save {
|
||||||
// TODO prompt for name and descr
|
// TODO prompt for name and descr
|
||||||
|
let item = Item {
|
||||||
|
name: "TODO!".into(),
|
||||||
|
event: None,
|
||||||
|
item: None,
|
||||||
|
request: Some(req),
|
||||||
|
response: Some(vec![]),
|
||||||
|
};
|
||||||
collection.item.push(item);
|
collection.item.push(item);
|
||||||
std::fs::write(&args.collection, serde_json::to_string(&collection)?)?;
|
std::fs::write(&args.collection, serde_json::to_string(&collection)?)?;
|
||||||
println!(" ││ * saved");
|
if args.verbose { println!(" ││ * saved") }
|
||||||
}
|
}
|
||||||
|
|
||||||
println!(" │╵");
|
if args.verbose { println!(" │╵") }
|
||||||
},
|
},
|
||||||
PostWomanActions::Test { } => {
|
PostWomanActions::Test { } => {
|
||||||
let mut tasks = Vec::new();
|
let mut tasks = Vec::new();
|
||||||
|
|
||||||
for item in collection.item {
|
for req in collection.collect() {
|
||||||
let t = tokio::spawn(async move {
|
let t = tokio::spawn(async move {
|
||||||
let r = item.send().await?;
|
let url = req.to_string();
|
||||||
println!(" ├ {} >> {}", item.name, r.status());
|
let r = req.send().await?;
|
||||||
|
println!(" ├ {} >> {}", url, r.status());
|
||||||
if args.verbose {
|
if args.verbose {
|
||||||
println!(" │ {}", r.text().await?.replace("\n", "\n │ "));
|
println!(" │ {}", r.text().await?.replace("\n", "\n │ "));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue