diff --git a/history.md b/history.md index 58dafee..ca1f40e 100644 --- a/history.md +++ b/history.md @@ -1,3 +1,8 @@ +# v0.1.5 - 2014/12/30 + +* Added support for request retry in the event of an error using constructor option: `maxRetryCount` +* Enhanced documentation for readme + # v0.1.4 - 2014/12/30 * Methods #poll and #search optionally accept retvals parameter as an array diff --git a/lib/index.js b/lib/index.js index 8f12a5e..8577d57 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,6 +6,7 @@ var threeTaps = (function (self) { var defaultOptions = { apikey : '', + maxRetryCount : 0, pollingUrl : 'https://polling.3taps.com', referenceUrl : 'https://reference.3taps.com', searchUrl : 'https://search.3taps.com', @@ -53,7 +54,12 @@ var threeTaps = (function (self) { return value; } - function makeRequest (requestOptions, callback) { + function makeRequest (requestOptions, tryCount, callback) { + if (typeof callback === 'undefined' && typeof tryCount === 'function') { + callback = tryCount; + tryCount = 1; + } + // make request request(requestOptions, function (err, res, body) { // attempt to parse response as JSON @@ -64,9 +70,18 @@ var threeTaps = (function (self) { }; } - // check for error + // check for error and retry if applicable err = err || checkResponseForError(requestOptions, res, json); if (err) { + var retry = + self.options.maxRetryCount && + tryCount < self.options.maxRetryCount; + + if (retry) { + tryCount++; + return makeRequest(requestOptions, tryCount, callback); + } + return callback(err); } diff --git a/package.json b/package.json index 52158d6..1380173 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "3taps", - "version": "0.1.4", + "version": "0.1.5", "description": "Node client for 3taps API", "main": "./lib", "scripts": { diff --git a/readme.md b/readme.md index a8d0201..6de5945 100644 --- a/readme.md +++ b/readme.md @@ -39,6 +39,22 @@ In the event that the 3taps API returns an unsupported HTTP status code (i.e. le Options are optional for every method call... the underlying 3taps API may return an error in the event that an expected parameter is missing, however. All parameters can be specified globally when initializing the client library and each initialized parameter value can be overridden in each module method call. +##### Base Constructor Options + +The following options are required when creating the client library: + +* `apikey` : the API key for access to the 3taps API + +The following options are optional when creating the client library: + +* `maxRetryCount` : _defaults to 0_ - the number of retries in the event that any error is returned when calling the API +* `pollingUrl` : _defaults to_ https://polling.3taps.com +* `referenceUrl` : _defaults to_ https://reference.3taps.com +* `searchUrl` : _defaults to_ https://search.3taps.com +* `strictSSL` : _defaults to true_ +* `timeout` : _defaults to 10000 (10 seconds)_ + + ### Polling API The polling endpoint supports two specific capabilities: `anchor` and `poll` diff --git a/test/lib/index.js b/test/lib/index.js index b947d5f..e5fc0cd 100644 --- a/test/lib/index.js +++ b/test/lib/index.js @@ -55,12 +55,49 @@ describe('3taps', function () { }; }); - // test constructor - it('should accept null or empty options', function() { - client = threeTaps(); + describe('constructor', function () { + // test constructor + it('should accept null or empty options', function() { + client = threeTaps(); - should.exist(client); - should.exist(client.options); + should.exist(client); + should.exist(client.options); + }); + + it('should default maxRetryCount to 3', function () { + client = threeTaps(); + + should.exist(client.options.maxRetryCount); + client.options.maxRetryCount.should.equal(0); + }); + + it('should properly retry on failure', function (done) { + requestScope = nock('https://polling.3taps.com') + .filteringPath(querystringFilter) + .get('/anchor') + .times(2) + .reply(409, { error : 'testing failure' }) + .get('/anchor') + .reply(200, defaultResponse); + + client = threeTaps({ maxRetryCount : 2 }); + + client.anchor(function (err, data) { + should.exist(err); + should.not.exist(data); + + should.exist(err.response); + should.exist(err.response.error); + err.response.error.should.equal('testing failure'); + + client.anchor(function (err, data) { + should.not.exist(err); + should.exist(data); + + return done(); + }); + }); + }); }); // tests for the polling API