aboutsummaryrefslogtreecommitdiff
path: root/src/executor.rs
blob: f78fd2a28c57a649bbba5017f02de830a66385a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::barbfile::BarbFile;

use std::collections::HashMap;
use ureq;
use ureq::Error as UreqError;

pub struct Context {
    vars: HashMap<String, String>,
}

impl Context {
    pub fn new<I>(vars: I) -> Context
    where
        I: Iterator<Item = (String, String)>,
    {
        let mut toto = HashMap::new();
        toto.extend(vars);

        Context { vars: toto }
    }

    #[cfg(test)]
    pub fn empty() -> Context {
        Context {
            vars: HashMap::new(),
        }
    }

    // Preserved for future reference
    // pub fn get_var(&self, name: String) -> Option<String> {
    //     self.vars
    //         .get(&name)
    //         .map(|val| val.clone())
    //         .or_else(|| env::var(name).ok())
    // }

    fn key_str(&self, key: &String) -> String {
        format!("{{{}}}", key)
    }

    pub fn substitute(&self, string: &String) -> String {
        let mut buffer = string.clone();
        for (key, val) in self.vars.iter() {
            buffer = buffer.replace(self.key_str(key).as_str(), val);
        }

        buffer
    }
}

pub struct Executor {
    context: Context,
}

impl Executor {
    pub fn new(context: Context) -> Executor {
        Executor { context }
    }

    fn make_req(&self, bfile: &BarbFile, print_req: bool, print_headers: bool) -> ureq::Request {
        if print_req {
            println!(
                "{} {}",
                bfile.method_as_string().as_str(),
                self.context.substitute(&bfile.url()).as_str()
            );
        }
        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(),
            );
            if print_headers {
                println!(
                    "{} {}",
                    header.name(),
                    self.context.substitute(header.value())
                );
            }
        }
        req
    }

    fn run(&self, bfile: &BarbFile, req: ureq::Request) -> Result<ureq::Response, String> {
        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(),
        };

        match resp {
            Ok(resp) => Ok(resp),
            Err(UreqError::Status(_, resp)) => Ok(resp),
            Err(UreqError::Transport(transp)) => Err(String::from(transp.to_string())),
        }
    }

    pub fn execute(
        &mut self,
        bfile: &BarbFile,
        print_req: bool,
        print_headers: bool,
    ) -> Result<ureq::Response, String> {
        self.run(bfile, self.make_req(&bfile, print_req, print_headers))
    }
}

// TODO: tests
#[cfg(test)]
mod tests {
    use super::*;
    use std::str::FromStr;

    #[test]
    fn test_context_key_str() {
        let ctx = Context::empty();
        assert_eq!(ctx.key_str(&String::from("foo")), String::from("{foo}"));
    }

    #[test]
    fn test_context_substitute() {
        let vars: Vec<(String, String)> = vec![
            (String::from("foo"), String::from("bar")),
            (String::from("bar"), String::from("baz")),
        ];
        let ctx = Context::new(vars.into_iter());
        assert_eq!(
            ctx.substitute(&String::from("blah blah {foo} blah")),
            String::from("blah blah bar blah")
        );
        assert_eq!(
            ctx.substitute(&String::from("blah {foo} {bar} blah")),
            String::from("blah bar baz blah")
        );
    }

    #[test]
    fn test_make_req_simple_get() {
        let executor = Executor::new(Context::empty());
        let bfile = BarbFile::from_str("#GET^http://foo.bar\n\n").unwrap();
        let req = executor.make_req(&bfile, false);

        assert_eq!(req.url(), "http://foo.bar");
        assert_eq!(req.method(), "GET");
    }

    #[test]
    fn test_make_req_simple_headers() {
        let executor = Executor::new(Context::empty());
        let bfile = BarbFile::from_str("#GET^http://foo.bar\n#Foo: Bar\n#Bar: Baz\n\n").unwrap();
        let req = executor.make_req(&bfile, false);

        assert_eq!(
            req.header_names(),
            vec![String::from("foo"), String::from("bar")]
        );
        assert_eq!(req.header("foo"), Some("Bar"));
        assert_eq!(req.header("bar"), Some("Baz"));
    }
}