aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Pasquet <dev@etenil.net>2022-02-15 14:01:15 +0000
committerGuillaume Pasquet <dev@etenil.net>2022-02-15 14:01:15 +0000
commit15e40bf3d028b383afbf7251910a121807e0d1b1 (patch)
treef756b69731198d24addf6ea77f34241b57b61e82
parent77f9fa4ddb2032309c19508a11de0caa3c155af1 (diff)
Add headers support
-rw-r--r--src/barbfile.rs12
-rw-r--r--src/executor.rs14
-rw-r--r--src/main.rs8
-rw-r--r--test.barb2
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() {
diff --git a/test.barb b/test.barb
index b49f5c8..f90d499 100644
--- a/test.barb
+++ b/test.barb
@@ -1,2 +1,4 @@
#GET^https://catfact.ninja/fact
+#Foo: Bar
+#Baz: {TERM}