postwoman/src/model/request.rs

64 lines
1.6 KiB
Rust
Raw Normal View History

2023-06-15 16:19:46 +02:00
use std::str::FromStr;
use postman_collection::{v1_0_0, v2_0_0, v2_1_0};
2023-06-15 15:42:07 +02:00
pub trait IntoRequest {
fn make_request(&self) -> reqwest::Request;
}
impl IntoRequest for v2_0_0::RequestClass {
fn make_request(&self) -> reqwest::Request {
todo!()
}
}
impl IntoRequest for v2_1_0::RequestClass {
fn make_request(&self) -> reqwest::Request {
2023-06-15 16:19:46 +02:00
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 mut url_str = match &self.url {
Some(v2_1_0::Url::String(x)) => x.clone(),
Some(v2_1_0::Url::UrlClass(v2_1_0::UrlClass { raw: Some(x), .. })) => x.clone(),
2023-06-15 16:19:46 +02:00
// TODO compose URL from UrlClass rather than only accepting those with raw set
_ => "http://localhost".into(),
2023-06-15 16:19:46 +02:00
};
for (k, v) in std::env::vars() {
let key = format!("{{{{{}}}}}", k);
if url_str.contains(&key) {
url_str = url_str.replace(&key, &v);
}
}
2023-06-15 16:19:46 +02:00
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?
2023-06-15 15:42:07 +02:00
}
}
2023-06-15 15:59:03 +02:00
impl IntoRequest for v1_0_0::Request {
2023-06-15 15:42:07 +02:00
fn make_request(&self) -> reqwest::Request {
todo!()
}
}