-
Notifications
You must be signed in to change notification settings - Fork 4
/
getpocket.js
244 lines (223 loc) · 7.47 KB
/
getpocket.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
/*jslint node: true, white: true, nomen: true, vars: true, unparam: true */
"use strict";
var request = require('request');
var ROOT_URL = 'https://getpocket.com';
var ADD_URL = '/v3/add';
var SEND_URL = '/v3/send';
var GET_URL = '/v3/get';
var OAUTH_REQUEST_URL = '/v3/oauth/request';
var OAUTH_TOKEN_URL = '/auth/authorize';
var OAUTH_ACCESS_URL = '/v3/oauth/authorize';
// var config = {
// consumer_key: 'my-consumer-key', (required)
// access_token: 'my-access-token' (required for non authorisation calls)
// };
function GetPocket(config, root_url) {
this.config = config;
this.headers = {
'Content-Type': 'application/json; charset=UTF-8',
'X-Accept': 'application/json'
};
if (root_url) {
ROOT_URL = root_url;
}
}
GetPocket.prototype = {
// var config = {
// consumer_key: 'my-consumer-key', (required)
// access_token: 'my-access-token' (required for non authorisation calls)
// };
refreshConfig: function(config) {
this.config = config;
},
// var params = {
// redirect_uri: 'http://localhost:8080/redirect'
// };
getRequestToken: function(params, callback) {
if (!params.redirect_uri) {
callback(new Error('400 Bad Request - missing params.redirect_uri'), null, null);
return false;
}
var options = params;
options.consumer_key = this.config.consumer_key;
var url = ROOT_URL + OAUTH_REQUEST_URL;
var opts = {
uri: url,
headers: this.headers,
body: JSON.stringify(options)
};
return request.post(opts, callback);
},
// var params = {
// request_token: 'token-from-getRequestToken',
// redirect_uri: 'http://localhost:8080/redirect'
// };
getAuthorizeURL: function(params) {
return ROOT_URL + OAUTH_TOKEN_URL + '?request_token=' + params.request_token + '&redirect_uri=' + params.redirect_uri;
},
// var params = {
// request_token: 'token-from-getRequestToken'
// };
getAccessToken: function(params, callback) {
if (!params.request_token) {
callback(new Error('400 Bad Request - missing params.request_token'), null, null);
return false;
}
var options = params;
options.consumer_key = this.config.consumer_key;
options.code = params.request_token;
var url = ROOT_URL + OAUTH_ACCESS_URL;
var opts = {
uri: url,
headers: this.headers,
body: JSON.stringify(options)
};
return request.post(opts, callback);
},
// var params = {
// url: 'url to add', (required),
// title: 'title', (optional),
// tags: 'tag,tag,...', (optional)
// tweet_id: 'id' (optional)
// };
add: function(params, callback) {
if (!params.url) {
callback(new Error('400 Bad Request - missing params.url'), null);
return false;
}
var options = params;
options.consumer_key = this.config.consumer_key;
options.access_token = this.config.access_token;
var url = ROOT_URL + ADD_URL;
var opts = {
uri: url,
headers: this.headers,
body: JSON.stringify(options)
};
var self = this;
request.post(opts, function(error, response, body) {
self._callbackHandler(error, response, body, callback);
});
},
send: function(params, callback) {
if (!params.actions) {
callback(new Error('400 Bad Request - missing params.actions'), null, null);
return false;
}
var options = params;
options.consumer_key = this.config.consumer_key;
options.access_token = this.config.access_token;
var url = ROOT_URL + SEND_URL;
var opts = {
uri: url,
headers: this.headers,
body: JSON.stringify(options)
};
var self = this;
request.post(opts, function(error, response, body) {
self._callbackHandler(error, response, body, callback);
});
},
modify: function(params, callback) {
// alias for send
return this.send(params, callback);
},
get: function(params, callback) {
var options = params || {};
options.consumer_key = this.config.consumer_key;
options.access_token = this.config.access_token;
var url = ROOT_URL + GET_URL;
var opts = {
uri: url,
headers: this.headers,
body: JSON.stringify(options)
};
var self = this;
request.post(opts, function(error, response, body) {
self._callbackHandler(error, response, body, callback);
});
},
retrieve: function(params, callback) {
// alias for get
return this.get(params, callback);
},
// var params = {
// item_id: 'item to archive'
// };
// OR
// var params = [{
// item_id: 'item to archive'
// }, {
// item_id: 'another item to archive'
// }, …];
archive: function(params, callback) {
var isSingleItem = !Array.isArray(params);
if (!params || (isSingleItem && !params.item_id)) {
callback(new Error('400 Bad Request - missing params.item_id'), null, null);
return false;
}
if (isSingleItem) params = [params];
var timestamp = new Date().getTime();
var actions = params.map(function (item) {
if (item.item_id) {
return {
action: 'archive',
item_id: item.item_id,
time: timestamp
}
}
});
if (params.length !== actions.length) {
callback(new Error('400 Bad Request - missing some params.item_id'), null, null);
return false;
}
var options = {
actions: actions
};
this.send(options, callback);
},
delete: function(params, callback) {
var isSingleItem = !Array.isArray(params);
if (!params || (isSingleItem && !params.item_id)) {
callback(new Error('400 Bad Request - missing params.item_id'), null, null);
return false;
}
if (isSingleItem) params = [params];
var timestamp = new Date().getTime();
var actions = params.map(function (item) {
if (item.item_id) {
return {
action: 'delete',
item_id: item.item_id,
time: timestamp
}
}
});
if (params.length !== actions.length) {
callback(new Error('400 Bad Request - missing some params.item_id'), null, null);
return false;
}
var options = {
actions: actions
};
this.send(options, callback);
},
_callbackHandler: function(error, response, body, callback) {
if (error) {
callback(error, undefined);
}
else if (response.statusCode !== 200) {
callback(new Error(response.statusCode), undefined);
}
else {
try {
var payload = JSON.parse(body);
callback(undefined, payload);
}
catch (e) {
callback(new Error('Could not interpret response from getpocket.com as JSON. Sorry ' + e), undefined);
}
}
}
};
module.exports = GetPocket;