twilio_agnostic/
call.rs

1use crate::{Client, FromMap, TwilioError, POST};
2use serde::Deserialize;
3use std::collections::BTreeMap;
4
5pub struct OutboundCall<'a> {
6    pub from: &'a str,
7    pub to: &'a str,
8    pub url: &'a str,
9}
10
11impl<'a> OutboundCall<'a> {
12    pub fn new(from: &'a str, to: &'a str, url: &'a str) -> OutboundCall<'a> {
13        OutboundCall { from, to, url }
14    }
15}
16
17#[derive(Debug, Deserialize)]
18#[serde(rename_all = "lowercase")]
19pub enum CallStatus {
20    Queued,
21    Ringing,
22    InProgress,
23    Canceled,
24    Completed,
25    Failed,
26    Busy,
27    NoAnswer,
28}
29
30#[derive(Debug, Deserialize)]
31pub struct Call {
32    from: String,
33    to: String,
34    sid: String,
35    status: CallStatus,
36}
37
38impl Client {
39    pub async fn make_call(&self, call: OutboundCall<'_>) -> Result<Call, TwilioError> {
40        let opts = [
41            ("To", &*call.to),
42            ("From", &*call.from),
43            ("Url", &*call.url),
44        ];
45        self.send_request(POST, "Calls", &opts).await
46    }
47}
48
49impl FromMap for Call {
50    fn from_map(mut m: BTreeMap<String, String>) -> Result<Box<Call>, TwilioError> {
51        let from = match m.remove("From") {
52            Some(v) => v,
53            None => return Err(TwilioError::ParsingError),
54        };
55        let to = match m.remove("To") {
56            Some(v) => v,
57            None => return Err(TwilioError::ParsingError),
58        };
59        let sid = match m.remove("CallSid") {
60            Some(v) => v,
61            None => return Err(TwilioError::ParsingError),
62        };
63        let stat = match m.get("CallStatus").map(|s| s.as_str()) {
64            Some("queued") => CallStatus::Queued,
65            Some("ringing") => CallStatus::Ringing,
66            Some("in-progress") => CallStatus::InProgress,
67            Some("canceled") => CallStatus::Canceled,
68            Some("completed") => CallStatus::Completed,
69            Some("failed") => CallStatus::Failed,
70            Some("busy") => CallStatus::Busy,
71            Some("no-answer") => CallStatus::NoAnswer,
72            _ => return Err(TwilioError::ParsingError),
73        };
74        Ok(Box::new(Call {
75            from,
76            to,
77            sid,
78            status: stat,
79        }))
80    }
81}