This repository has been archived by the owner on May 18, 2018. It is now read-only.
forked from alexschneider/pushbullet-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushbullet.js
executable file
·272 lines (252 loc) · 8.17 KB
/
pushbullet.js
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
var PushBullet = (function() {
var pb = {};
var pbURL = "https://api.pushbullet.com/v2/";
var pbPush = pbURL + "pushes";
var pbContact = pbURL + "contacts";
var pbDevice = pbURL + "devices";
var pbUser = pbURL + "users/me";
var pbUpReq = pbURL + "upload-request";
var httpReqDone = 4;
var httpResGood = 200;
var httpResNoCont = 204;
pb.APIKey = null;
pb.push = function(pushType, devId, email, data, callback) {
var parameters = {type: pushType.toLowerCase()};
if(email && devId) {
var err = new Error("Cannot push to both device and contact");
if(callback) {
return callback(err);
} else {
throw err;
}
} else if(email) {
parameters.email = email;
} else if(devId) {
parameters.device_iden = devId;
}
switch(pushType.toLowerCase()) {
case "note":
parameters.title = data.title;
parameters.body = data.body;
break;
case "link":
parameters.title = data.title;
parameters.url = data.url;
if(data.body) {
parameters.body = data.body;
}
break;
case "address":
parameters.name = data.name;
parameters.address = data.address;
break;
case "list":
parameters.title = data.title;
parameters.items = data.items;
break;
default:
var err = new Error("Invalid type");
if(callback) {
return callback(err);
} else {
throw err;
}
break;
}
var res = ajaxReq(pbPush, "POST", parameters, false, callback);
if(!callback) {
return res;
}
};
pb.pushFile = function(devId, email, fileHandle, body, callback) {
var type = "file_type=" + encodeURIComponent(fileHandle.type);
var name = "file_name=" + encodeURIComponent(fileHandle.name);
var upReqURL = pbUpReq + "?" + type + "&" + name;
var upReqFunc = !callback ? null : function(err, res) {
if(err) {
return callback(err);
} else {
try {
doPushFile(res, devId, email, fileHandle, body, callback);
} catch(err2) {
return callback(err2);
}
}
};
var res = ajaxReq(upReqURL, "GET", null, false, upReqFunc);
if(!callback) {
return doPushFile(res, devId, email, fileHandle, body);
}
};
var doPushFile = function(ajax, devId, email, fileHandle, body, callback) {
var fileInfo = new FormData();
fileInfo.append("awsaccesskeyid", ajax.data.awsaccesskeyid);
fileInfo.append("acl", ajax.data.acl);
fileInfo.append("key", ajax.data.key);
fileInfo.append("signature", ajax.data.signature);
fileInfo.append("policy", ajax.data.policy);
fileInfo.append("content-type", fileHandle.type);
fileInfo.append("file", fileHandle);
ajaxReq(ajax.upload_url, "POST", fileInfo, true, null);
var parameters = {
file_name: fileHandle.name,
file_type: fileHandle.type,
file_url: ajax.file_url,
type: "file"
};
if(body) {
parameters.body = body;
}
if(email && devId) {
var err = new Error("Cannot push to both device and contact");
if(callback) {
return callback(err);
} else {
throw err;
}
} else if(email) {
parameters.email = email;
} else if(devId) {
parameters.device_iden = devId;
} else {
var err2 = new Error("Must push to either device or contact");
if(callback) {
return callback(err2);
} else {
throw err2;
}
}
var res = ajaxReq(pbPush, "POST", parameters, false, callback);
if(!callback) {
return res;
}
};
pb.deletePush = function(pushId, callback) {
var res = ajaxReq(pbPush + "/" + pushId, "DELETE", null, false, callback);
if(!callback) {
return res;
}
};
pb.pushHistory = function(modifiedAfter, cursor, callback) {
if(typeof modifiedAfter === 'function') {
callback = modifiedAfter;
modifiedAfter = null;
} else if (typeof cursor === 'function') {
callback = cursor;
cursor = null;
}
var parameters = null;
if(modifiedAfter) {
parameters = {
modified_after: modifiedAfter
};
}
if(cursor) {
parameters = parameters || {};
parameters.cursor = cursor;
}
var res = ajaxReq(pbPush, "GET", parameters, false, callback);
if(!callback) {
return res;
}
};
pb.devices = function(callback) {
var res = ajaxReq(pbDevice, "GET", null, false, callback);
if(!callback) {
return res;
}
};
pb.deleteDevice = function(devId, callback) {
var res = ajaxReq(pbDevice + "/" + devId, "DELETE", null, false, callback);
if(!callback) {
return res;
}
};
pb.contacts = function(callback) {
var res = ajaxReq(pbContact, "GET", null, false, callback);
if(!callback) {
return res;
}
};
pb.deleteContact = function(contId, callback) {
var res = ajaxReq(pbContact + "/" + contId, null, false, callback);
if(!callback) {
return res;
}
};
pb.user = function(callback) {
var res = ajaxReq(pbUser, "GET", null, false, callback);
if(!callback) {
return res;
}
};
var ajaxReq = function(url, verb, parameters, fileUpload, callback) {
if(!pb.APIKey) {
var err = new Error("API Key for Pushbullet not set");
if(callback) {
return callback(err);
} else {
throw err;
}
} else {
var ajax = new XMLHttpRequest();
var async = false;
if(callback) {
async = true;
ajax.onreadystatechange = function() {
if(ajax.readyState === httpReqDone) {
var res = null;
try {
res = handleResponse(ajax);
} catch(err) {
return callback(err);
}
return callback(null, res);
}
};
}
if(verb === "GET") {
var queryParams = [];
for(var key in parameters) {
queryParams.push(key + '=' + parameters[key]);
}
var queryString = queryParams.join("&");
url += "?" + queryString;
parameters = null;
}
ajax.open(verb, url, async);
if(!fileUpload) {
ajax.setRequestHeader("Authorization", "Basic " + window.btoa(pb.APIKey + ":"));
ajax.setRequestHeader("Content-Type", "application/json");
parameters = JSON.stringify(parameters);
}
if(parameters) {
ajax.send(parameters);
} else {
ajax.send();
}
if(!async) {
return handleResponse(ajax);
}
}
};
var handleResponse = function(ajax) {
var response;
try {
response = JSON.parse(ajax.response);
}
catch (err) {
response = {
response: ajax.response
};
}
if(ajax.status !== httpResGood) {
response.httpStatus = ajax.status;
if (ajax.status !== httpResNoCont) {
throw response;
}
}
return response;
};
return pb;
}());