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
macro_rules! execute {
    ($x:ident) => {
        impl<'a> Execute for $x<'a> {
            fn execute<U, D>(
                self,
                method: Method,
                url: U,
                body: Option<String>,
            ) -> Result<(hyper::Headers, hyper::StatusCode, Option<D>), TwilioErr>
            where
                U: AsRef<str>,
                D: for<'de> serde::Deserialize<'de>,
            {
                use {
                    futures::{future, Future, Stream}, hyper::{header, Request}, serde_json,
                };
                const BASE: &str = "https://api.twilio.com/2010-04-01/Accounts";

                let mut core_ref = self.client.core.try_borrow_mut()?;
                let url =
                    format!("{}/{}/{}", BASE, self.client.sid, url.as_ref()).parse::<hyper::Uri>()?;
                // println!("{:?}", url);
                let mut request = Request::new(method, url);

                if let Some(body) = body {
                    // println!("{:?}", body);
                    request.set_body(body);
                    request
                        .headers_mut()
                        .set(header::ContentType::form_url_encoded());
                }
                // println!("{:?}", request);

                request.headers_mut().set(self.client.auth.clone());
                let fut_req = self.client.client.request(request).and_then(|res| {
                    // println!("Response: {}", res.status());
                    // println!("Headers: \n{}", res.headers());

                    let header = res.headers().clone();
                    let status = res.status();

                    res.body()
                        .fold(Vec::new(), |mut v, chunk| {
                            v.extend(&chunk[..]);
                            future::ok::<_, hyper::Error>(v)
                        })
                        .map(move |chunks| {
                            if chunks.is_empty() {
                                Ok((header, status, None))
                            } else {
                                // println!("{:?}", String::from_utf8(chunks.clone()));
                                Ok((header, status, Some(serde_json::from_slice(&chunks)?)))
                            }
                        })
                });
                core_ref.run(fut_req)?
            }
        }
    };
}

macro_rules! from {
    ($x:ty, $variant:ident) => {
        impl From<$x> for TwilioErr {
            fn from(e: $x) -> Self {
                $variant(e)
            }
        }
    };
}

macro_rules! pair {
    ($x:ident, $field:ident, $name:tt, $vec:ident) => {
        if let Some($field) = $x.$field {
            $vec.push(($name, $field));
        }
    };
}