forked from ceejbot/recurring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecurly-data.js
369 lines (311 loc) · 9.84 KB
/
recurly-data.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
'use strict'
const handleRecurlyError = require('./util').handleRecurlyError
const RecurlyError = require('./recurly-error')
const _ = require('lodash')
const rparser = require('./parser.js')
const debugR = require('debug')('recurring:request')
const debug = require('debug')('recurring')
const iterators = require('async-iterators')
const pkg = require('../package.json')
const querystring = require('querystring')
const parser = rparser.createParser()
class RecurlyData {
constructor(options) {
// Store a reference to the Recurring instance.
this._recurring = options.recurring
this.idField = options.idField
this.enumerable = options.enumerable
this.properties = { }
this._resources = { }
for (var i = 0; i < options.properties.length; i++) {
RecurlyData.addProperty(this, options.properties[i])
}
this.proplist = options.properties
this.__defineGetter__('id', function() {
return this.properties[options.idField]
})
var idSetter = function() {
var newval = arguments['0']
this.properties[options.idField] = newval
this.href = this.constructor.ENDPOINT + '/' + newval
}
this.__defineSetter__('id', idSetter)
this.__defineSetter__(options.idField, idSetter)
}
static get ENDPOINT() {
return 'https://api.recurly.com/v2/'
}
static addProperty(instance, propname) {
var getterFunc = function() {
return this.properties[propname]
}
var setterFunc = function() {
var newval = arguments['0']
this.properties[propname] = newval
}
instance.__defineGetter__(propname, getterFunc)
instance.__defineSetter__(propname, setterFunc)
}
baseOptions() {
return {
headers: {
'Accept': 'application/xml',
'Authorization': this._recurring.AUTH_BASIC,
'User-Agent': `${pkg.name}/${pkg.version}`,
'X-Api-Version': '2.29'
}
}
}
go(method, uri, args, opts, callback) {
if (typeof callback === 'undefined' && typeof opts === 'function') {
callback = opts
opts = { }
}
var options = _.merge(this.baseOptions(), opts)
options.uri = uri
if (method === 'GET') {
options.qs = args
}
else {
options.body = args
}
options.method = method
this.execute(options, callback)
}
get(uri, args, opts, callback) {
this.go('GET', uri, args, opts, callback)
}
put(uri, args, opts, callback) {
this.go('PUT', uri, args, opts, callback)
}
post(uri, args, opts, callback) {
this.go('POST', uri, args, opts, callback)
}
head(uri, args, opts, callback) {
this.go('HEAD', uri, args, opts, callback)
}
all(filter, callback) {
if (typeof callback === 'undefined' && typeof filter === 'function') {
callback = filter
filter = {}
}
filter = filter || {}
if (!this.enumerable) {
return callback(new Error(`${this.constructor.name} does not support .all()`))
}
this.fetchAll(this.constructor.name, this.constructor.ENDPOINT, filter, (err, results) => {
if (err) {
return callback(err)
}
const data = { }
_.each(results, item => {
data[item[this.idField]] = item
})
callback(null, data)
})
}
fetchAll(modelName, uri, filter, callback) {
if (typeof callback === 'undefined' && typeof filter === 'function') {
callback = filter
filter = {}
}
filter = filter || {}
var model = this._recurring[modelName]()
var iterator = model.iterator(filter, uri)
var result = [ ]
iterators.forEachAsync(iterator, function(err, item, next) {
if (err) {
return callback(err)
}
result.push(item)
return next()
}, () => callback(null, result))
}
fetch(callback) {
if (!this.href) {
throw (new Error('cannot fetch a record without an href'))
}
this.get(this.href, { }, (err, response, payload) => {
if (err) {
return callback(err)
}
if (response.statusCode === 404) {
return callback(new Error('not_found'))
}
this.inflate(payload)
callback(null, this)
})
}
destroy(callback) {
const options = this.baseOptions()
options.uri = this.href
options.method = 'DELETE'
const handleResponse = (err, response, payload) => {
var error = handleRecurlyError(err, response, payload, [ 204 ])
if (error) {
return callback(error)
}
this.deleted = true
callback(null, this.deleted)
}
this._recurring.request(options, function(err, response, body) {
if (body) {
parser.parseXML(body, function(xmlerr, result) {
if (xmlerr) {
return handleResponse(err, response, body)
}
return handleResponse(err, response, result)
})
}
else {
handleResponse(err, response)
}
})
}
inflate(json) {
if (typeof json !== 'object') {
// TODO throw an error
console.error(json)
return
}
var keys = Object.keys(json)
for (var i = 0; i < keys.length; i++) {
var prop = keys[i]
var value = json[prop]
if (prop === 'a') {
// Hackery. 'a' is a list of named anchors. We treat them specially.
this.a = { }
var anchors = Object.keys(value)
for (var j = 0; j < anchors.length; j++) {
this.a[value.name] = value
}
}
// if (prop === 'a') {
// // Hackery. 'a' is a list of named anchors. We treat them specially.
// this.a = { }
// var anchors = Object.keys(value)
// for (var j = 0; j < anchors.length; j++) {
// this.a[value[anchors[j]].name] = value[j]
// }
// }
else if (value.hasOwnProperty('href') && (Object.keys(value).length === 1)) {
if (!this._resources) {
this._resources = { }
}
this._resources[prop] = value.href
}
else {
this[prop] = value
}
}
}
execute(options, callback) {
console.log(`#recurring`, options);
debug('execute called with options: %o', options)
this._recurring.request(options, function(err, response, body) {
debug('execute got response with status code: %s', _.get(response, 'statusCode'))
debugR('execute got response with body: %O', body)
if (err) {
console.error('recurly.' + options.method, 'error ' + JSON.stringify(err))
return callback(err, response, { })
}
if (response.statusCode === 404) {
return parser.parseXML(body, function(err, result) {
if (err) {
console.error('recurly.get', 'xml parsing error:', JSON.stringify(err), 'from body:', body)
return callback(err, response, { })
}
var error = new RecurlyError(result)
callback(error, response, { })
})
}
if (response.statusCode === 401) {
return callback(new Error('Your API key is missing or invalid'), response, {})
}
if (options.noParse || response.statusCode === 204) {
return callback(err, response, body)
}
parser.parseXML(body, function(err, result) {
if (err) {
console.error('recurly.get', 'xml parsing error:', JSON.stringify(err), 'from body:', body)
return callback(err, response, { })
}
callback(null, response, result)
})
})
}
iterator(filter, endpoint) {
filter = filter || {}
endpoint = endpoint || this.constructor.ENDPOINT
let uri = filter ? `${endpoint}?${querystring.stringify(filter)}` : endpoint
const result = [ ]
let total = -1
let current = 0
return {
next: function(cb) {
// If we are already at the end, return nothing,
if (total > -1 && current >= total) {
return cb(null)
}
// If this is the first hit, fetch the total count, then the first set of results.
if (total === -1) {
return this.getTotalCount((err, res) => {
if (err) {
return cb(err)
}
total = res
return this.getNextValue(cb)
})
}
// Otherwise, just return the next result.
return this.getNextValue(cb)
},
getTotalCount: done => {
debug(`fetching result count.`)
this.head(uri, null, { noParse: true }, (err, response, records) => {
var error = handleRecurlyError(err, response, records, [ 200 ])
if (error) {
return done(error)
}
total = parseInt(response.headers['x-records'], 10)
debug(`result count: ${total}`)
done(null, total)
})
},
getNextValue: done => {
// If we already have some results, return the next one.
if (result.length) {
current++
done(null, result.shift())
}
// Otherwise fetch the next page of results if there are some.
else {
debug(`fetching iterator results: ${current}/${total}`)
this.get(uri, { per_page: 200 }, (err, response, records) => {
var error = handleRecurlyError(err, response, records, [ 200 ])
if (error) {
return done(error)
}
// Ensure next attempt to fetch results pulls from next page.
if (response.headers.link) {
uri = response.headers.link.split('; rel="next"')[0].split(',').pop().trim().slice(1, -1)
}
// Process the results.
_.each(records, record => {
const item = this._recurring[this.constructor.name]()
item.inflate(record)
result.push(item)
})
// Grab the first item.
var item = result.shift()
// Increment the current counter.
current++
// Return it.
done(err, item)
})
}
}
}
}
}
module.exports = RecurlyData