diff --git a/src/builder.rs b/src/builder.rs deleted file mode 100644 index 0a9a1b2..0000000 --- a/src/builder.rs +++ /dev/null @@ -1,11 +0,0 @@ -use clap::Subcommand; - -use crate::proto::{Header, Item, Request}; - - -impl PostWomanActions { - pub async fn build(&self) -> Item { - match self { - } - } -} diff --git a/src/collector.rs b/src/collector.rs deleted file mode 100644 index 908e16b..0000000 --- a/src/collector.rs +++ /dev/null @@ -1,86 +0,0 @@ -use std::str::FromStr; -use postman_collection::v2_1_0::{Spec, RequestClass, Items, Url, UrlClass, HeaderUnion, Body}; - -pub fn collect(collection: Spec) -> Vec { - let mut reqs = Vec::new(); - for item in collection.item { - reqs.append(&mut requests(item)); // TODO creating all these vectors is a waste! - } - reqs -} - -pub fn requests(root: Items) -> Vec { - let mut reqs = Vec::new(); - - if let Some(r) = root.request { - match r { - postman_collection::v2_1_0::RequestUnion::RequestClass(x) => reqs.push(x), - postman_collection::v2_1_0::RequestUnion::String(url) => reqs.push( - RequestClass { - method: Some("GET".into()), - url: Some(Url::String(url)), - auth: None, - body: None, - certificate: None, - description: None, - header: None, - proxy: None, - } - ), - } - } - - if let Some(items) = root.item { - for item in items { - reqs.append(&mut requests(item)); - } - } - - reqs -} - -pub fn url(req: &RequestClass) -> String { - let mut base_url = match &req.url { - Some(Url::String(x)) => x.clone(), - Some(Url::UrlClass(UrlClass { raw: Some(raw) , .. })) => raw.clone(), - // TODO compose UrlClass - _ => "".into(), - }; - - for (k, v) in std::env::vars() { - let key = format!("{{{{{}}}}}", k); - if base_url.contains(&key) { - base_url = base_url.replace(&key, &v); - } - } - - base_url -} - -pub async fn send(req: RequestClass) -> reqwest::Result { - let method = reqwest::Method::from_bytes( - &req.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 = reqwest::Url::from_str(&url(&req)).unwrap(); - - let mut out = reqwest::Client::new().request(method, url); - - match req.header { - Some(HeaderUnion::HeaderArray(x)) => { - for h in x { - out = out.header(h.key, h.value); - } - }, - _ => {}, - } - - match req.body { - Some(Body { raw: Some(x), .. }) => { - out = out.body(x) - }, - _ => {}, - } - - out.send().await -} diff --git a/src/proto.rs b/src/proto.rs deleted file mode 100644 index f055890..0000000 --- a/src/proto.rs +++ /dev/null @@ -1,177 +0,0 @@ -use serde::{Serialize, Deserialize}; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct PostWomanCollection { - pub variables: Vec, // TODO these sure aren't just strings for sure... - pub info: CollectionInfo, - pub item: Vec, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct CollectionInfo { - pub name: String, - pub description: Option, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Item { - pub name: String, - pub event: Option>, - pub request: Option, - pub response: Option>, - pub item: Option>, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Response { - pub name: String, - pub code: u16, - pub header: String, - // pub cookie: Vec, - pub body: Body, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Event { - pub listen: String, - pub script: Script, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Script { - pub r#type: String, - pub exec: Vec, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -#[serde(untagged)] -pub enum Request { - Object { - url: Url, - method: String, - header: Option>, - body: Option, - description: Option, - }, - String(String), -} - -impl ToString for Request { - fn to_string(&self) -> String { - match self { - Self::String(x) => x.clone(), - Self::Object { - url, - method: _, header: _, body: _, description: _ - } => url.to_string(), - } - } -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Header { - pub key: String, - pub value: String, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Query { - pub key: String, - pub value: String, - pub equals: bool, - pub description: Option, -} - -impl ToString for Query { - fn to_string(&self) -> String { - format!("{}={}", self.key, self.value) - } -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -#[serde(untagged)] -pub enum Body { - Object(serde_json::Value), - String(String), -} - -impl ToString for Body { - fn to_string(&self) -> String { - match self { - Body::Object(v) => serde_json::to_string(v).unwrap(), - Body::String(s) => s.clone(), - } - } -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -#[serde(untagged)] -pub enum Url { - Object { - raw: Option, - protocol: String, - host: Vec, - path: Vec, - query: Option>, - variable: Option>, // TODO surely aren't just strings - }, - String(String), -} - -impl ToString for Url { - fn to_string(&self) -> String { - match self { - Url::String(s) => s.clone(), - Url::Object { - raw, protocol, - host,path, query, - variable: _ - } => { - match &raw { - Some(s) => s.clone(), - None => { - let mut url = String::new(); - url.push_str(&protocol); - url.push_str("://"); - url.push_str(&host.join(".")); - url.push_str("/"); - url.push_str(&path.join("/")); - - if let Some(query) = &query { - url.push_str("?"); - let q : Vec = query.iter().map(|x| x.to_string()).collect(); - url.push_str(&q.join("&")); - } - - url - } - } - } - } - } -} - -// barebones custom error - -// #[derive(Debug, Clone)] -// pub struct PostWomanError { -// msg : String, -// } -// -// impl PostWomanError { -// pub fn throw(msg: impl ToString) -> Box { -// Box::new( -// PostWomanError { -// msg: msg.to_string(), -// } -// ) -// } -// } -// -// impl std::fmt::Display for PostWomanError { -// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -// write!(f, "PostWomanError({})", self.msg) -// } -// } -// -// impl std::error::Error for PostWomanError {}