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
use crate::one_api::{balance, message_status, send_messages};
use crate::one_api::{error::Error, result::Result};
use reqwest::{
header::HeaderMap, header::HeaderValue, header::ACCEPT, header::AUTHORIZATION,
header::CONTENT_TYPE, Client as HTTPClient,
};
use std::default::Default;
pub const HOSTNAME: &str = "https://platform.clickatell.com";
const MESSAGE_PATH: &str = "/v1/message";
const BALANCE_PATH: &str = "/v1/balance";
const APPLICATION_JSON: &str = "application/json";
pub struct Client {
hostname: String,
http_client: HTTPClient,
}
impl Client {
pub fn new(api_key: &str) -> Result<Self> {
Self::builder().api_key(api_key).build()
}
pub fn builder() -> ClientBuilder {
ClientBuilder::default()
}
pub async fn send_messages(
&self,
send_messages_request: send_messages::Request,
) -> Result<send_messages::Response> {
if send_messages_request.message_count() >= 100 {
return Err(Error::TooManyMessages);
}
let response = self
.http_client
.post(format!("{}{MESSAGE_PATH}", self.hostname))
.json(&send_messages_request)
.send()
.await?;
Ok(response.json::<send_messages::Response>().await?)
}
pub async fn message_status(
&self,
request: message_status::Request,
) -> Result<message_status::Response> {
let response = self
.http_client
.get(format!("{}{MESSAGE_PATH}/{}", self.hostname, request))
.send()
.await?;
Ok(response.json::<message_status::Response>().await?)
}
pub async fn balance(&self) -> Result<balance::Response> {
let response = self
.http_client
.get(format!("{}{BALANCE_PATH}", self.hostname))
.send()
.await?;
Ok(response.json::<balance::Response>().await?)
}
}
pub struct ClientBuilder {
hostname: Option<String>,
api_key: Option<String>,
}
impl Default for ClientBuilder {
fn default() -> Self {
ClientBuilder {
hostname: Some(HOSTNAME.to_string()),
api_key: None,
}
}
}
impl ClientBuilder {
pub fn api_key(mut self, api_key: &str) -> Self {
self.api_key = Some(api_key.to_string());
self
}
pub fn hostname(mut self, hostname: &str) -> Self {
self.hostname = Some(hostname.to_string());
self
}
pub fn build(self) -> Result<Client> {
match (self.api_key, self.hostname) {
(Some(api_key), Some(hostname)) => {
let mut headers = HeaderMap::new();
let mut auth_value = HeaderValue::from_str(&api_key)?;
auth_value.set_sensitive(true);
headers.insert(AUTHORIZATION, auth_value);
headers.insert(CONTENT_TYPE, HeaderValue::from_str(APPLICATION_JSON)?);
headers.insert(ACCEPT, HeaderValue::from_str(APPLICATION_JSON)?);
let http_client = HTTPClient::builder().default_headers(headers).build()?;
Ok(Client {
hostname,
http_client,
})
}
(None, _) => Err(Error::ApiKeyNotSet),
(Some(_), None) => Err(Error::HostnameNotSet),
}
}
}