-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
143 lines (111 loc) · 3.25 KB
/
index.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
/**
@module PushClient
*/
//Dependencies
var Q = require('q'),
https = require('https');
//REST API Config Defaults
var defaultHost = 'cp.pushwoosh.com',
defaultApiVersion = '1.3';
var defaultConfig={
"application":"",
"auth":"",
"notifications":[]
};
/**
Pushwoosh API Client
@constructor
@param {string} appid - The application ID
@param {string} tkn - The auth token, as seen in the Pushwoosh Dashboard
@param {object} options (optional) - optional config for the REST client
*/
function PushClient(appid, tkn, options) {
//Required client config
if (!appid || !tkn) {
throw 'RestClient requires an APP ID and Auth Token set explicitly ';
}
else {
//if auth token/SID passed in manually, trim spaces
this.appid = appid.replace(/ /g,'');
this.apiToken=tkn;
}
//Optional client config
options = options || {};
this.host = options.host || defaultHost;
this.apiVersion = options.apiVersion || defaultApiVersion;
this.timeout = options.timeout || 31000; // request timeout in milliseconds
defaultConfig.application=this.appid;
defaultConfig.auth=this.apiToken;
this.sendMessage=function(options){
var client=this;
var deferred = Q.defer();
var sendOptions=defaultConfig;
sendOptions.notifications = [];
sendOptions.notifications=[options];
client.request(sendOptions,"createMessage").then(function(data){
console.log(data);
deferred.resolve(data);
},function(err){console.log(err)});
return deferred.promise;
};
}
/**
Deletes a push notification
@param {string} messageId - Message to Delete
*/
PushClient.prototype.deleteMessage=function(messageId){
var client=this;
var deferred = Q.defer();
var sendOptions={'auth':client.apiToken,'message':messageId};
console.log(sendOptions);
client.request(sendOptions,"deleteMessage").then(function(data){
console.log(data);
deferred.resolve(data);
},function(err){console.log(err)});
return deferred.promise;
};
/**
Make an authenticated request against the Pushwoosh backend.
@param {object} options - options for HTTP request
@param {string} url - url to call
- @param {object} error - an error object if there was a problem processing the request
- @param {object} data - the JSON-parsed data
*/
PushClient.prototype.request = function (options, url) {
var client = this,
deferred = Q.defer();
var json_request={"request":options};
var error = null;
var jsonString = JSON.stringify(json_request);
var headers = {
'Content-Type': 'application/json',
'Content-Length': jsonString.length
};
var httpOptions = {
host: this.host,
path: '/json/' + this.apiVersion+'/'+url,
method: 'POST',
headers: headers
};
var req = https.request(httpOptions, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
// console.log(responseString);
var resultObject = JSON.parse(responseString);
deferred.resolve(resultObject);
});
});
req.on('error', function(e) {
// TODO: handle error.
console.log(e);
deferred.reject("error");
});
req.write(jsonString);
req.end();
return deferred.promise;
};
module.exports = PushClient;