-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
173 lines (140 loc) · 3.48 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
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
const got = require('got')
const PocketAPI = class {
constructor (consumer_key, redirect_uri = 'pocketapp1234:authorizationFinished') {
this.consumer_key = consumer_key
this.redirect_uri = redirect_uri
this.config = {
pocketUrl: {
request: 'https://getpocket.com/v3/oauth/request',
authorize: 'https://getpocket.com/v3/oauth/authorize',
get: 'https://getpocket.com/v3/get',
add: 'https://getpocket.com/v3/add',
modify: 'https://getpocket.com/v3/send'
},
headers: {
'content-type': 'application/json',
'X-Accept': 'application/json'
}
}
}
setRequestToken (requestToken) {
this.request_token = requestToken
}
setAccessToken (accessToken) {
this.access_token = accessToken
}
setOptions (values, endpoint, method = 'post') {
return {
headers: this.config.headers,
body: JSON.stringify(values),
url: endpoint,
method: method,
}
}
async getRequestToken (callback) {
if (this.request_token) {
return await this.request_token
}
let response
let token
const values = {
consumer_key: this.consumer_key,
redirect_uri: this.redirect_uri,
}
const options = this.setOptions(values, this.config.pocketUrl.request)
try {
response = await got(options)
} catch (e) {
console.error(e.name + ': ' + e.message)
throw new Error(e)
}
token = JSON.parse(response.body)
this.request_token = token.code;
if (callback) {
return callback(token.code)
}
return token.code
}
async getAccessToken (callback) {
if (this.access_token) {
return await this.access_token
}
let token
let response
const values = {
consumer_key: this.consumer_key,
code: this.request_token
}
const options = this.setOptions(values, this.config.pocketUrl.authorize)
try {
response = await got(options)
} catch (e) {
console.error(e.name + ': ' + e.message)
throw new Error(e)
}
token = JSON.parse(response.body)
this.access_token = token.access_token
if (callback) {
return callback(token)
}
return token
}
async getArticles (params, callback) {
let response
const access = {
consumer_key: this.consumer_key,
access_token: this.access_token,
}
const values = { ...access, ...params }
const options = this.setOptions(values, this.config.pocketUrl.get)
try {
response = await got(options)
} catch (e) {
console.error(e.name + ': ' + e.message)
throw new Error(e)
}
if (callback) {
return callback(JSON.parse(response.body))
}
return JSON.parse(response.body)
}
async addArticles (params, callback) {
let response
const access = {
consumer_key: this.consumer_key,
access_token: this.access_token
}
const values = { ...access, ...params }
const options = this.setOptions(values, this.config.pocketUrl.add)
try {
response = await got(options)
} catch (e) {
console.error(e.name + ': ' + e.message)
throw new Error(e)
}
if (callback) {
return callback(JSON.parse(response.body))
}
return JSON.parse(response.body)
}
async modifyArticles (actions, callback) {
let response
const values = {
actions: actions,
consumer_key: this.consumer_key,
access_token: this.access_token
}
const options = this.setOptions(values, this.config.pocketUrl.modify)
try {
response = await got(options)
} catch (e) {
console.error(e.name + ': ' + e.message)
throw new Error(e)
}
if (callback) {
return callback(JSON.parse(response.body))
}
return JSON.parse(response.body)
}
}
module.exports = PocketAPI