aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Pasquet <dev@etenil.net>2022-02-12 23:15:22 +0000
committerGuillaume Pasquet <dev@etenil.net>2022-02-12 23:15:22 +0000
commitf2d6f5de0e71741326badb15d04773d5bb2b4329 (patch)
tree751891b3604cd57c4be4dbb7322d899d882c1007
parent411e83ef44c4fd9ea0d4489f29b35bb2d47c388b (diff)
Working arguments
-rw-r--r--src/main.rs53
1 files changed, 44 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 25749de..0c4abde 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -30,18 +30,17 @@ impl Context {
.or_else(|| env::var(name).ok())
}
- pub fn execute(&self, bfile: BarbFile) -> String {
+ pub fn execute(&self, bfile: BarbFile) -> Result<ureq::Response, String> {
let req = ureq::request(bfile.method_as_string().as_str(), &bfile.url());
- let resp = match bfile.method().takes_body() {
+ match bfile.method().takes_body() {
true => match bfile.body() {
Some(body) => req.send_string(body.as_str()),
None => req.call(),
},
false => req.call(),
- };
-
- resp.unwrap().into_string().unwrap()
+ }
+ .map_err(|_| String::from("Error"))
}
}
@@ -57,17 +56,53 @@ struct Args {
files: Vec<String>,
}
+impl Args {
+ pub fn print_headers(&self) -> bool {
+ self.headers || !self.body
+ }
+
+ pub fn print_body(&self) -> bool {
+ !self.headers || self.body
+ }
+
+ pub fn raw_body(&self) -> bool {
+ self.raw
+ }
+}
+
fn main() {
let args = Args::parse();
- let mut context = Context::new();
+ let context = Context::new();
let bfile = BarbFile::from_str(
- fs::read_to_string("test-post.barb")
+ fs::read_to_string("test.barb")
.expect("Failed to read file")
.as_str(),
)
.expect("Failed to parse file");
- let result = context.execute(bfile);
+ let response = context.execute(bfile).unwrap();
+
+ if args.print_headers() {
+ println!("{} {}", response.status(), response.status_text());
+ for header_name in response.headers_names() {
+ println!(
+ "{}: {}",
+ header_name,
+ response.header(header_name.as_str()).unwrap()
+ );
+ }
+ }
- println!("{}", format_json(result.as_str(), Indentation::Default));
+ if args.print_body() {
+ println!(
+ "{}",
+ match args.raw_body() {
+ true => String::from(response.into_string().unwrap().as_str()),
+ false => format_json(
+ response.into_string().unwrap().as_str(),
+ Indentation::Default
+ ),
+ }
+ );
+ }
}