diff options
author | Guillaume Pasquet <dev@etenil.net> | 2022-02-10 23:49:58 +0000 |
---|---|---|
committer | Guillaume Pasquet <dev@etenil.net> | 2022-02-10 23:49:58 +0000 |
commit | 9101f32f7d258984fb8f8247de8bc661acd69d47 (patch) | |
tree | 4a8b755c1ae4f96f14f5f5f8f2cd5e23b65c2c58 /src/main.rs | |
parent | 9f4fec0dce1e931356c567fa11d9435f44fbbf50 (diff) |
Working requests
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 58 |
1 files changed, 34 insertions, 24 deletions
diff --git a/src/main.rs b/src/main.rs index 693567d..182376c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,44 +2,54 @@ mod barbfile; use jsonformat::{format_json, Indentation}; -use std::collections::HashMap; -use std::env; +// use std::collections::HashMap; +// use std::env; use std::fs; use std::str::FromStr; use ureq; -struct Context { - vars: HashMap<String, String>, -} +// struct Context { +// vars: HashMap<String, String>, +// } -impl Context { - pub fn new() -> Context { - Context { - vars: HashMap::new(), - } - } - - pub fn get_var(&self, name: String) -> Option<String> { - self.vars - .get(&name) - .map(|val| val.clone()) - .or_else(|| env::var(name).ok()) - } -} +// impl Context { +// pub fn new() -> Context { +// Context { +// vars: HashMap::new(), +// } +// } + +// pub fn get_var(&self, name: String) -> Option<String> { +// self.vars +// .get(&name) +// .map(|val| val.clone()) +// .or_else(|| env::var(name).ok()) +// } +// } fn main() { - let mut context = Context::new(); + // let mut context = Context::new(); let bfile = barbfile::BarbFile::from_str( - fs::read_to_string("test.barb") + fs::read_to_string("test-post.barb") .expect("Failed to read file") .as_str(), ) .expect("Failed to parse file"); - let result = ureq::request(bfile.method_as_string().as_str(), &bfile.url()) - .call() - .unwrap() + + let req = ureq::request(bfile.method_as_string().as_str(), &bfile.url()); + + let resp = match bfile.method().takes_body() { + true => match bfile.body() { + Some(body) => req.send_string(body.as_str()), + None => req.call() + }, + false => req.call(), + }; + + let result = resp.unwrap() .into_string() .unwrap(); + println!("{}", format_json(result.as_str(), Indentation::Default)); } |