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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use super::*;
use reqwest::Url;
lazy_static! {
static ref PUSHES_URL: String = format!("{}pushes", BASE_URL);
}
#[derive(Debug)]
pub enum Target {
Broadcast,
Device(String),
Email(String),
Channel(String),
Client(String)
}
#[derive(Debug)]
pub enum Request<'a> {
Note {
title: &'a str,
body: &'a str,
},
Link {
title: &'a str,
body: &'a str,
url: &'a str
}
}
#[derive(Deserialize, Debug)]
pub struct Response {
pub active: bool,
#[serde(default)]
pub body: String,
pub created: f64,
pub direction: String,
pub dismissed: bool,
pub iden: String,
pub modified: f64,
pub receiver_email: String,
pub receiver_email_normalized: String,
pub receiver_iden: String,
pub sender_email: String,
pub sender_email_normalized: String,
pub sender_iden: String,
pub sender_name: String,
#[serde(default)]
pub title: String,
#[serde(default)]
pub url: String,
#[serde(rename = "type")]
pub push_type: String,
}
impl Response {
pub fn created_time(&self) -> DateTime<UTC> {
float_unix_time2date_time(self.created)
}
pub fn modified_time(&self) -> DateTime<UTC> {
float_unix_time2date_time(self.modified)
}
}
#[derive(Deserialize, Debug)]
struct ResponseVec {
pub pushes: Vec<Response>
}
pub type Result = ::std::result::Result<(Response, ResponseHeaders), Box<Error>>;
#[derive(Serialize, Debug)]
pub struct ListCondition {
pub active: bool,
pub limit: u32,
pub modified_after: Option<f64>,
pub cursor: Option<String>,
}
impl ListCondition {
pub fn new(limit: u32) -> ListCondition {
ListCondition {
active: true,
limit: limit,
modified_after: None,
cursor: None,
}
}
pub fn set_modified_after(&mut self, t: DateTime<UTC>) {
self.modified_after = Some(date_time2float_unix_time(t));
}
}
impl PushbulletClient {
pub fn create_push(&self, target: &Target, request: Request) -> Result {
debug!("target: {:?}, request: {:?}", target, request);
let mut json = match request {
Request::Note { title, body } => json!({
"type": "note",
"title": title,
"body": body
}),
Request::Link { title, body, url } => json!({
"type": "link",
"title": title,
"body": body,
"url": url
})
};
match target {
&Target::Broadcast => (),
&Target::Device(ref id) => json["device_iden"] = json!(id),
&Target::Email(ref id) => json["email"] = json!(id),
&Target::Channel(ref id) => json["channel_tag"] = json!(id),
&Target::Client(ref id) => json["client_iden"] = json!(id)
}
debug!("json: {}", json);
match self.post(&*PUSHES_URL, json) {
Ok((raw_response, headers)) => {
let r: Response = serde_json::from_reader(raw_response)?;
Ok((r, headers))
}
Err(e) => Err(e)
}
}
pub fn list_push(&self, condition: &ListCondition)
-> ::std::result::Result<(Vec<Response>, ResponseHeaders), Box<Error>> {
debug!("condition: {:?}", condition);
let mut params = vec![
("active", format!("{}", condition.active)),
("limit", format!("{}", condition.limit)),
];
condition.modified_after.map(|t| {
params.push(("modified_after", format!("{:e}", t)))
});
condition.cursor.as_ref().map(|s| {
params.push(("cursor", s.to_string()))
});
let url = Url::parse_with_params(&*PUSHES_URL, ¶ms).unwrap().into_string();
match self.get(&url) {
Ok((raw_response, headers)) => {
let r: ResponseVec = serde_json::from_reader(raw_response)?;
Ok((r.pushes, headers))
}
Err(e) => Err(e)
}
}
}
#[cfg(test)]
mod tests {
use serde_json;
use super::*;
#[test]
fn deserialize_test() {
let deserialized: ResponseVec = serde_json::from_str(PUSH_RESULT).unwrap();
assert_eq!(deserialized.pushes.len(), 1);
let r = &deserialized.pushes[0];
assert_eq!(r.push_type, "note");
assert_eq!(r.active, true);
assert_eq!(r.direction, "self");
assert_eq!(r.modified, 1.412047948579031e+09);
let diff = r.modified_time().signed_duration_since(UTC.ymd(2014, 9, 30).and_hms(3, 32, 28));
assert!((diff.num_milliseconds() - 579).abs() < 10);
assert_eq!(r.created, 1.412047948579029e+09);
let diff = r.created_time().signed_duration_since(UTC.ymd(2014, 9, 30).and_hms(3, 32, 28));
assert!((diff.num_milliseconds() - 579).abs() < 10);
}
const PUSH_RESULT: &str = r#"
{
"pushes": [
{
"active": true,
"body": "Space Elevator, Mars Hyperloop, Space Model S (Model Space?)",
"created": 1.412047948579029e+09,
"direction": "self",
"dismissed": false,
"iden": "ujpah72o0sjAoRtnM0jc",
"modified": 1.412047948579031e+09,
"receiver_email": "elon@teslamotors.com",
"receiver_email_normalized": "elon@teslamotors.com",
"receiver_iden": "ujpah72o0",
"sender_email": "elon@teslamotors.com",
"sender_email_normalized": "elon@teslamotors.com",
"sender_iden": "ujpah72o0",
"sender_name": "Elon Musk",
"title": "Space Travel Ideas",
"type": "note"
}
]
}
"#;
}