diff options
author | Guillaume Pasquet <dev@etenil.net> | 2022-02-15 14:01:15 +0000 |
---|---|---|
committer | Guillaume Pasquet <dev@etenil.net> | 2022-02-15 14:01:15 +0000 |
commit | 15e40bf3d028b383afbf7251910a121807e0d1b1 (patch) | |
tree | f756b69731198d24addf6ea77f34241b57b61e82 | |
parent | 77f9fa4ddb2032309c19508a11de0caa3c155af1 (diff) |
Add headers support
-rw-r--r-- | src/barbfile.rs | 12 | ||||
-rw-r--r-- | src/executor.rs | 14 | ||||
-rw-r--r-- | src/main.rs | 8 | ||||
-rw-r--r-- | test.barb | 2 |
4 files changed, 31 insertions, 5 deletions
diff --git a/src/barbfile.rs b/src/barbfile.rs index 4d304e2..39e4485 100644 --- a/src/barbfile.rs +++ b/src/barbfile.rs @@ -59,7 +59,7 @@ impl Method { } #[derive(Debug)] -struct Header { +pub struct Header { name: String, value: String, } @@ -70,6 +70,16 @@ impl fmt::Display for Header { } } +impl Header { + pub fn name(&self) -> &String { + &self.name + } + + pub fn value(&self) -> &String { + &self.value + } +} + struct BarbHeader { pub method: Method, pub url: String, diff --git a/src/executor.rs b/src/executor.rs index ee17bbf..d9bfaee 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -1,4 +1,4 @@ -use crate::barbfile::BarbFile; +use crate::barbfile::{BarbFile, Header}; use std::collections::HashMap; use std::env; @@ -18,6 +18,7 @@ impl Context { } } + // Preserved for future reference // pub fn get_var(&self, name: String) -> Option<String> { // self.vars // .get(&name) @@ -49,13 +50,18 @@ impl Executor { context: Context::new() } } - + pub fn execute(&mut self, bfile: BarbFile) -> Result<ureq::Response, String> { - let req = ureq::request( + let mut req = ureq::request( bfile.method_as_string().as_str(), self.context.substitute(&bfile.url()).as_str() ); + for header in bfile.headers() { + req = req.set(header.name(), self.context.substitute(header.value()).as_str()); + println!("{} {}", header.name(), self.context.substitute(header.value())); + } + match bfile.method().takes_body() { true => match bfile.body() { Some(body) => req.send_string(body.as_str()), @@ -66,3 +72,5 @@ impl Executor { .map_err(|_| String::from("Error")) } } + +// TODO: tests diff --git a/src/main.rs b/src/main.rs index c1fe2ab..67c6541 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,8 @@ struct Args { #[clap(short, long)] headers: bool, #[clap(short, long)] + all_headers: bool, + #[clap(short, long)] body: bool, #[clap(short, long)] raw: bool, @@ -26,7 +28,7 @@ struct Args { impl Args { pub fn print_headers(&self) -> bool { - self.headers || !self.body + self.headers || self.all_headers || !self.body } pub fn print_body(&self) -> bool { @@ -36,6 +38,10 @@ impl Args { pub fn raw_body(&self) -> bool { self.raw } + + pub fn req_headers(&self) -> bool { + self.all_headers + } } fn main() { @@ -1,2 +1,4 @@ #GET^https://catfact.ninja/fact +#Foo: Bar +#Baz: {TERM} |