Skip to content

Commit

Permalink
Merge f666134 into 36a696e
Browse files Browse the repository at this point in the history
  • Loading branch information
brozeph committed Dec 30, 2014
2 parents 36a696e + f666134 commit 049fd81
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
5 changes: 5 additions & 0 deletions 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
Expand Down
19 changes: 17 additions & 2 deletions lib/index.js
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion 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": {
Expand Down
16 changes: 16 additions & 0 deletions readme.md
Expand Up @@ -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`
Expand Down
47 changes: 42 additions & 5 deletions test/lib/index.js
Expand Up @@ -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
Expand Down

0 comments on commit 049fd81

Please sign in to comment.