fix: made it work (kinda)

This commit is contained in:
əlemi 2023-06-15 16:19:46 +02:00
parent 2effe880dc
commit d704610357
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 80 additions and 32 deletions

View file

@ -1,9 +1,8 @@
mod model;
mod collector;
use clap::{Parser, Subcommand};
use crate::{collector::{collect, url, send}, model::PostWomanCollection};
use crate::model::PostWomanCollection;
/// API tester and debugger from your CLI
#[derive(Parser, Debug)]
@ -120,24 +119,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
PostWomanActions::Test { } => {
let reqs = collection.requests();
// let mut tasks = Vec::new();
let mut tasks = Vec::new();
// for req in reqs {
// let t = tokio::spawn(async move {
// let url = url(&req);
// let r = send(req).await?;
// println!(" ├ {} >> {}", url, r.status());
// if args.verbose {
// println!(" │ {}", r.text().await?.replace("\n", "\n │ "));
// }
// Ok::<(), reqwest::Error>(())
// });
// tasks.push(t);
// }
for req in reqs {
let t = tokio::spawn(async move {
let c = reqwest::Client::default(); // TODO maybe make just 1 client for everyone?
let url = req.url().as_str().to_string();
let r = c.execute(req).await?;
println!("{} >> {}", url, r.status());
if args.verbose {
println!("{}", r.text().await?.replace("\n", "\n"));
}
Ok::<(), reqwest::Error>(())
});
tasks.push(t);
}
// for t in tasks {
// t.await??;
// }
for t in tasks {
t.await??;
}
},
PostWomanActions::Show { } => {
println!("{:?}", collection);

View file

@ -1,4 +1,4 @@
use postman_collection::{PostmanCollection, v1_0_0, v2_0_0, v2_1_0};
use postman_collection::{v1_0_0, v2_0_0, v2_1_0};
use super::request::IntoRequest;
@ -44,14 +44,8 @@ impl CollectRequests for v2_0_0::Items {
if let Some(r) = &self.request {
let clazz = match r {
v2_0_0::RequestUnion::String(url) => v2_0_0::RequestClass {
auth: None,
body: None,
certificate: None,
description: None,
header: None,
method: None,
proxy: None,
url: Some(v2_0_0::Url::String(url.clone())),
.. Default::default()
},
v2_0_0::RequestUnion::RequestClass(r) => r.clone(),
};
@ -61,7 +55,6 @@ impl CollectRequests for v2_0_0::Items {
for item in sub {
requests.append(&mut item.collect_requests());
}
}
requests
}
@ -69,6 +62,28 @@ impl CollectRequests for v2_0_0::Items {
impl CollectRequests for v2_1_0::Items {
fn collect_requests(&self) -> Vec<reqwest::Request> {
todo!()
let mut requests = Vec::new();
if let Some(r) = &self.request {
let clazz = match r {
v2_1_0::RequestUnion::String(url) => v2_1_0::RequestClass {
auth: None,
body: None,
certificate: None,
description: None,
header: None,
method: None,
proxy: None,
url: Some(v2_1_0::Url::String(url.clone())),
},
v2_1_0::RequestUnion::RequestClass(r) => r.clone(),
};
requests.push(clazz.make_request());
}
if let Some(sub) = &self.item {
for item in sub {
requests.append(&mut item.collect_requests());
}
}
requests
}
}

View file

@ -1,7 +1,7 @@
mod request;
mod collector;
use postman_collection::{PostmanCollection, v1_0_0, v2_0_0, v2_1_0};
use postman_collection::{PostmanCollection, v2_0_0, v2_1_0};
use self::collector::CollectRequests;
@ -24,7 +24,7 @@ impl PostWomanCollection {
pub fn name(&self) -> &String {
match &self.collection {
PostmanCollection::V1_0_0(spec) => todo!(),
PostmanCollection::V1_0_0(_spec) => todo!(),
PostmanCollection::V2_0_0(spec) => &spec.info.name,
PostmanCollection::V2_1_0(spec) => &spec.info.name,
}

View file

@ -1,4 +1,6 @@
use postman_collection::{PostmanCollection, v1_0_0, v2_0_0, v2_1_0};
use std::str::FromStr;
use postman_collection::{v1_0_0, v2_0_0, v2_1_0};
pub trait IntoRequest {
fn make_request(&self) -> reqwest::Request;
@ -12,7 +14,38 @@ impl IntoRequest for v2_0_0::RequestClass {
impl IntoRequest for v2_1_0::RequestClass {
fn make_request(&self) -> reqwest::Request {
todo!()
let method = reqwest::Method::from_bytes(
&self.method.as_ref().unwrap_or(&"GET".into()).as_bytes() // TODO lol?
).unwrap_or(reqwest::Method::GET); // TODO throw an error rather than replacing it silently
let url_str = match &self.url {
Some(v2_1_0::Url::String(x)) => x,
Some(v2_1_0::Url::UrlClass(v2_1_0::UrlClass { raw: Some(x), .. })) => x,
// TODO compose URL from UrlClass rather than only accepting those with raw set
_ => "http://localhost",
};
let url = reqwest::Url::from_str(&url_str).unwrap();
let mut out = reqwest::Client::new().request(method, url);
match &self.header {
Some(v2_1_0::HeaderUnion::HeaderArray(x)) => {
for h in x {
out = out.header(h.key.clone(), h.value.clone()); // TODO avoid cloning
}
},
_ => {},
}
match &self.body {
Some(v2_1_0::Body { raw: Some(x), .. }) => {
out = out.body(x.clone()) // TODO try to avoid cloning?
},
_ => {},
}
out.build().unwrap() // TODO what about this?
}
}