Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to retry requests #484

Merged
merged 1 commit into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/helper/request-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ RequestObj.prototype.end = function(cb) {
function RequestBuilder(options) {
this._sendTelemetry = options._sendTelemetry === false ? options._sendTelemetry : true;
this._telemetryInfo = options._telemetryInfo || null;
this._timesToRetryFailedRequests = options._timesToRetryFailedRequests;
this.headers = options.headers || {};
}

Expand All @@ -86,6 +87,9 @@ RequestBuilder.prototype.setCommonConfiguration = function(ongoingRequest, optio
if (this._sendTelemetry) {
ongoingRequest = ongoingRequest.set('Auth0-Client', this.getTelemetryData());
}
if (this._timesToRetryFailedRequests > 0) {
ongoingRequest = ongoingRequest.retry(this._timesToRetryFailedRequests);
}
return ongoingRequest;
};

Expand Down
10 changes: 10 additions & 0 deletions src/web-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var CrossOriginAuthentication = require('./cross-origin-authentication');
* @param {String} [options.scope] scopes to be requested during Auth. e.g. `openid email`
* @param {String} [options.audience] identifier of the resource server who will consume the access token issued after Auth
* @param {Array} [options.plugins]
* @param {Number} [options._timesToRetryFailedRequests] Number of times to retry a failed request, according to {@link https://github.com/visionmedia/superagent/blob/master/lib/should-retry.js}
* @see {@link https://auth0.com/docs/api/authentication}
*/
function WebAuth(options) {
Expand Down Expand Up @@ -55,6 +56,11 @@ function WebAuth(options) {
optional: true,
type: 'object',
message: '_telemetryInfo option is not valid'
},
_timesToRetryFailedRequests: {
optional: true,
type: 'number',
message: '_timesToRetryFailedRequests option is not valid'
}
}
);
Expand All @@ -78,6 +84,10 @@ function WebAuth(options) {
? this.baseOptions._sendTelemetry
: true;

this.baseOptions._timesToRetryFailedRequests = options._timesToRetryFailedRequests
? parseInt(options._timesToRetryFailedRequests, 0)
: 0;

this.baseOptions.tenant =
(this.baseOptions.overrides && this.baseOptions.overrides.__tenant) ||
this.baseOptions.domain.split('.')[0];
Expand Down
10 changes: 10 additions & 0 deletions test/helper/request-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ describe('helpers requestBuilder', function() {
});
});

it('should retry request', function() {
var retryTimes = 2;
var req = new RequestBuilder({
_timesToRetryFailedRequests: retryTimes
});
var handler = req.get('https://test.com').withCredentials().end(function(err, data) {});

expect(handler.request.willRetry).to.eql(retryTimes);
});

it('should post stuff', function() {
var req = new RequestBuilder({});
var handler = req
Expand Down
5 changes: 5 additions & 0 deletions test/mock/request-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ RequestMock.prototype.end = function(cb) {
return this;
};

RequestMock.prototype.retry = function(times) {
this.willRetry = times;
return this;
};

module.exports = RequestMock;
1 change: 1 addition & 0 deletions test/web-auth/web-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('auth0.WebAuth', function() {
scope: 'openid name read:blog',
audience: 'urn:site:demo:blog',
_sendTelemetry: false,
_timesToRetryFailedRequests: 2,
overrides: {
__tenant: 'tenant1',
__token_issuer: 'issuer1'
Expand Down