Skip to content
This repository has been archived by the owner on Jun 6, 2019. It is now read-only.

Commit

Permalink
2.0.5: add Client.prototype.publishersInfo (#33)
Browse files Browse the repository at this point in the history
* 2.0.5: add Client.prototype.publishersInfo

For future use of batched REST call

* Ensure that at most one _publishersInfo is active...

…regardless of the number of alls to `publishersInfo`

* Consistent style

* attempt at fixing race resulting in multiple in flight (#34)

LGTM.

* All new edits!

- Have up to 4 concurrent retrievals

- Handle 429 responses

- Extra check in `retrytrip` in case we never get a `response` object
  • Loading branch information
mrose17 authored Dec 19, 2017
1 parent 81795a8 commit c429ca9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ node_modules

# package lock
package-lock.json

# one-off tests
test.js
48 changes: 46 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,56 @@ Client.prototype.publisherInfo = function (publisher, callback) {
path = '/v3/publisher/identity?' + querystring.stringify({ publisher: publisher })
self._retryTrip(self, { path: path, method: 'GET', useProxy: true }, function (err, response, body) {
self._log('publisherInfo', { method: 'GET', path: path, errP: !!err })
if (err) return callback(err)
if (err) return callback(err, null, response)

callback(null, body)
})
}

// batched interface... note multiple invocations of callback!

Client.prototype.publishersInfo = function (publishers, callback) {
if (this.options.version === 'v1') return

// initial version is still serialized, future versions will use a new API call.

if (!Array.isArray(publishers)) publishers = [ publishers ]
if (publishers.length === 0) return

if (typeof this.batches === 'undefined') this.batches = 0
this.publishers = this.publishers ? this.publishers.concat(publishers) : underscore.clone(publishers)
this._publishersInfo(callback)
}

Client.prototype._publishersInfo = function (callback) {
const self = this

const publisher = underscore.first(self.publishers)

if ((!publisher) || (self.batches > 3)) return

self.publishers = underscore.rest(self.publishers)

self.batches++
self.publisherInfo(publisher, (err, result, response) => {
if ((err) && (response) && (response.statusCode === 429)) {
console.log('zzz...')
return setTimeout(() => {
console.log('...zzz')
self.batches--
self.publishersInfo(publisher, callback)
}, random.randomInt({ min: 1 * msecs.minute, max: 2 * msecs.minute }))
}

self.batches--
callback(err, result || { publisher: publisher })

self._publishersInfo.bind(self)(callback)
})

setTimeout(() => self._publishersInfo.bind(self)(callback), random.randomInt({ min: 250, max: 500 }))
}

Client.prototype.getPromotion = function (lang, forPaymentId, callback) {
const self = this

Expand Down Expand Up @@ -1188,7 +1232,7 @@ Client.prototype._retryTrip = (self, params, callback, retry) => {
method = method(retry.delay)

self.roundtrip(params, (err, response, payload) => {
const code = Math.floor(response.statusCode / 100)
const code = response && Math.floor(response.statusCode / 100)

if ((!err) || (code !== 5) || (retry.retries-- < 0)) return callback(err, response, payload)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bat-client",
"version": "2.0.4",
"version": "2.0.5",
"description": "An example of client code for the BAT.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit c429ca9

Please sign in to comment.