1use crate::client::Client;
2use ureq::{Error, Response};
3use serde::{Deserialize};
4
5#[derive(Deserialize)]
6pub struct VoiceJson {
7 pub balance: f64,
8 pub debug: bool,
9 pub messages: Vec<VoiceMessage>,
10 pub success: String,
11 pub total_price: f64,
12}
13
14#[derive(Deserialize)]
15pub struct VoiceMessage {
16 pub error: Option<String>,
17 pub error_text: Option<String>,
18 pub id: Option<String>,
19 pub price: f64,
20 pub recipient: String,
21 pub sender: String,
22 pub success: bool,
23 pub text: String,
24}
25
26pub struct VoiceParams {
27 pub from: Option<String>,
28 pub text: String,
29 pub to: String,
30 pub xml: Option<bool>,
31}
32
33pub struct Voice {
34 client: Client
35}
36
37impl Voice {
38 pub fn new(client: Client) -> Self {
39 Voice {
40 client,
41 }
42 }
43
44 fn post(&self, params: VoiceParams, json: bool) -> Result<Response, Error> {
45 Ok(self.client.request("POST", "voice")
46 .send_form(&[
47 ("from", &*params.from.unwrap_or_default()),
48 ("json", self.client.bool_to_string(json)),
49 ("text", &*params.text),
50 ("to", &*params.to),
51 ("xml", self.client.bool_to_string(params.xml.unwrap_or_default())),
52 ])?)
53 }
54
55 pub fn text(&self, params: VoiceParams) -> Result<String, Error> {
56 Ok(self.post(params, false).unwrap().into_string()?)
57 }
58
59 pub fn json(&self, params: VoiceParams) -> Result<VoiceJson, Error> {
60 Ok(self.post(params, true).unwrap().into_json::<VoiceJson>()?)
61 }
62}