aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Pasquet <dev@etenil.net>2022-02-10 23:49:58 +0000
committerGuillaume Pasquet <dev@etenil.net>2022-02-10 23:49:58 +0000
commit9101f32f7d258984fb8f8247de8bc661acd69d47 (patch)
tree4a8b755c1ae4f96f14f5f5f8f2cd5e23b65c2c58
parent9f4fec0dce1e931356c567fa11d9435f44fbbf50 (diff)
Working requests
-rw-r--r--src/barbfile.rs17
-rw-r--r--src/main.rs58
-rw-r--r--test-post.barb6
3 files changed, 55 insertions, 26 deletions
diff --git a/src/barbfile.rs b/src/barbfile.rs
index 26ab583..4d304e2 100644
--- a/src/barbfile.rs
+++ b/src/barbfile.rs
@@ -1,4 +1,3 @@
-use std::matches;
use std::str::FromStr;
use std::string::ToString;
use std::{error::Error, fmt};
@@ -14,7 +13,7 @@ impl fmt::Display for BarbParseError {
}
}
-enum Method {
+pub enum Method {
GET,
PUT,
POST,
@@ -49,6 +48,16 @@ impl ToString for Method {
}
}
+impl Method {
+ pub fn takes_body(&self) -> bool {
+ match self {
+ Method::GET => false,
+ Method::DELETE => false,
+ _ => true
+ }
+ }
+}
+
#[derive(Debug)]
struct Header {
name: String,
@@ -100,6 +109,10 @@ impl BarbFile {
pub fn url(&self) -> &String {
&self.header.url
}
+
+ pub fn body(&self) -> &Option<String> {
+ &self.body
+ }
}
fn decode_url_line(line: &str) -> Result<(Method, String), BarbParseError> {
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));
}
diff --git a/test-post.barb b/test-post.barb
new file mode 100644
index 0000000..5bbc7df
--- /dev/null
+++ b/test-post.barb
@@ -0,0 +1,6 @@
+#POST^https://jsonplaceholder.typicode.com/posts
+
+{
+ "title": "bar",
+ "body": "This is the bar that goes together with foo."
+} \ No newline at end of file