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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use super::{
    encode_pairs, url_encode, Execute, Twilio, TwilioErr, TwilioJson, TwilioRequest, TwilioResp,
};
use async_trait::async_trait;
use hyper::{self, Method};
use serde::Deserialize;

#[derive(Default, Debug)]
pub struct Msg<'a> {
    from: &'a str,
    to: &'a str,
    body: &'a str,
    media_url: Option<&'a str>,
}

impl<'a> Msg<'a> {
    pub fn new(from: &'a str, to: &'a str, body: &'a str) -> Msg<'a> {
        Msg {
            from,
            to,
            body,
            ..Msg::default()
        }
    }
}

impl<'a> ToString for Msg<'a> {
    fn to_string(&self) -> String {
        match self.media_url {
            Some(m_url) => encode_pairs(&[
                ("To", self.to),
                ("From", self.from),
                ("Body", self.body),
                ("MediaUrl", m_url),
            ])
            .unwrap(),
            None => {
                encode_pairs(&[("To", self.to), ("From", self.from), ("Body", self.body)]).unwrap()
            }
        }
    }
}

#[derive(Debug, Deserialize)]
#[allow(non_camel_case_types)]
pub enum MsgStatus {
    queued,
    sending,
    sent,
    failed,
    delivered,
    undelivered,
    receiving,
    received,
}

#[derive(Debug, Deserialize)]
pub struct MsgResp {
    pub from: String,
    pub to: String,
    pub body: String,
    pub sid: String,
    pub status: MsgStatus,
    pub media_url: Option<String>,
    pub price: Option<String>,
    pub price_unit: String,
    pub uri: String,
    pub date_created: String,
    pub date_sent: Option<String>,
    pub date_updated: String,
}

// for outbound sms
#[derive(Debug)]
pub struct SendMsg<'a> {
    pub msg: Msg<'a>,
    pub client: &'a Twilio,
}

impl<'a> SendMsg<'a> {
    pub fn media(mut self, media_url: &'a str) -> SendMsg<'a> {
        self.msg.media_url = Some(media_url);
        self
    }
}

execute!(SendMsg);

#[async_trait]
impl<'a> TwilioRequest for SendMsg<'a> {
    type Resp = MsgResp;

    async fn run(&self) -> TwilioResp<TwilioJson<Self::Resp>> {
        let msg = self.msg.to_string();
        self.execute(Method::POST, "Messages.json", Some(msg)).await
    }
}

#[derive(Debug)]
pub struct GetMessage<'a> {
    pub message_sid: &'a str,
    pub client: &'a Twilio,
}

execute!(GetMessage);

#[async_trait]
impl<'a> TwilioRequest for GetMessage<'a> {
    type Resp = MsgResp;

    async fn run(&self) -> TwilioResp<TwilioJson<Self::Resp>> {
        let msg_sid = format!("Messages/{}.json", self.message_sid);
        self.execute(Method::GET, msg_sid, None).await
    }
}

impl<'a> GetMessage<'a> {
    pub async fn redact(&self) -> TwilioResp<TwilioJson<MsgResp>> {
        let msg_sid = format!("Messages/{}.json", self.message_sid);
        self.execute(Method::POST, msg_sid, Some("Body=".into()))
            .await
    }

    pub async fn delete(&self) -> TwilioResp<TwilioJson<MsgResp>> {
        let msg_sid = format!("Messages/{}.json", self.message_sid);
        self.execute(Method::DELETE, msg_sid, None).await
    }

    pub async fn media(&self) -> TwilioResp<TwilioJson<MediaResp>> {
        let msg_sid = format!("Messages/{}/Media.json", self.message_sid);
        self.execute(Method::GET, msg_sid, None).await
    }
}

#[derive(Debug, Deserialize)]
pub struct MediaResp {
    pub media_list: Vec<MediaItem>,
    pub num_pages: i32,
    pub page: i32,
    pub page_size: i32,
    pub start: i32,
    pub total: i32,
    pub uri: String,
    pub account_sid: String,
    pub message_sid: String,
}

#[derive(Debug, Deserialize)]
pub struct MediaItem {
    pub account_sid: String,
    pub content_type: String,
    pub sid: String,
    pub uri: String,
    pub message_sid: String,
    pub date_created: String,
    pub date_update: String,
}

#[derive(Debug)]
pub struct Messages<'a> {
    pub client: &'a Twilio,
}

impl<'a> Messages<'a> {
    pub fn between(self, from: &'a str, to: &'a str) -> MessagesDetails<'a> {
        MessagesDetails {
            client: &self.client,
            from: Some(from),
            to: Some(to),
            date_sent: None,
        }
    }

    pub fn on(self, date_sent: &'a str) -> MessagesDetails<'a> {
        MessagesDetails {
            client: &self.client,
            from: None,
            to: None,
            date_sent: Some(date_sent),
        }
    }
}

execute!(Messages);

#[async_trait]
impl<'a> TwilioRequest for Messages<'a> {
    type Resp = ListAllMsgs;

    async fn run(&self) -> TwilioResp<TwilioJson<Self::Resp>> {
        self.execute(Method::GET, "Messages.json", None).await
    }
}

#[derive(Debug)]
pub struct MessagesDetails<'a> {
    pub client: &'a Twilio,
    pub from: Option<&'a str>,
    pub to: Option<&'a str>,
    pub date_sent: Option<&'a str>,
}

impl<'a> ToString for MessagesDetails<'a> {
    fn to_string(&self) -> String {
        let mut pairs = Vec::new();
        pair!(self, from, "From", pairs);
        pair!(self, to, "To", pairs);
        pair!(self, date_sent, "DateSent", pairs);
        // does this have to be different? will the encode_pairs work here?
        url_encode(pairs)
    }
}

execute!(MessagesDetails);

#[async_trait]
impl<'a> TwilioRequest for MessagesDetails<'a> {
    type Resp = ListAllMsgs;

    async fn run(&self) -> TwilioResp<TwilioJson<Self::Resp>> {
        let url = format!("Messages.json?{}", self.to_string());
        self.execute(Method::GET, url, None).await
    }
}

#[derive(Debug, Deserialize)]
pub struct ListAllMsgs {
    pub messages: Vec<MsgResp>,
    pub page: usize,
    pub page_size: usize,
    pub uri: String,
    pub next_page_uri: Option<String>,
    pub previous_page_uri: Option<String>,
}