221 lines
37 KiB
HTML
221 lines
37 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html lang="en">
|
||
|
<head>
|
||
|
<meta charset="utf-8">
|
||
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
|
<title>RetryRestClient.js - Documentation</title>
|
||
|
|
||
|
<script src="scripts/prettify/prettify.js"></script>
|
||
|
<script src="scripts/prettify/lang-css.js"></script>
|
||
|
<!--[if lt IE 9]>
|
||
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
||
|
<![endif]-->
|
||
|
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
|
||
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
||
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
|
||
|
<label for="nav-trigger" class="navicon-button x">
|
||
|
<div class="navicon"></div>
|
||
|
</label>
|
||
|
|
||
|
<label for="nav-trigger" class="overlay"></label>
|
||
|
|
||
|
<nav>
|
||
|
<li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Classes</li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html">AuthenticationClient</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#changePassword">changePassword</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#clientCredentialsGrant">clientCredentialsGrant</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#getClientInfo">getClientInfo</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#getDelegationToken">getDelegationToken</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#getProfile">getProfile</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#passwordGrant">passwordGrant</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#requestChangePasswordEmail">requestChangePasswordEmail</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#requestEmailCode">requestEmailCode</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#requestMagicLink">requestMagicLink</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#requestSMSCode">requestSMSCode</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.AuthenticationClient.html#verifySMSCode">verifySMSCode</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="module-auth.DatabaseAuthenticator.html">DatabaseAuthenticator</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.DatabaseAuthenticator.html#changePassword">changePassword</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.DatabaseAuthenticator.html#requestChangePasswordEmail">requestChangePasswordEmail</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.DatabaseAuthenticator.html#signIn">signIn</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.DatabaseAuthenticator.html#signUp">signUp</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="module-auth.OAuthAuthenticator.html">OAuthAuthenticator</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.OAuthAuthenticator.html#passwordGrant">passwordGrant</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.OAuthAuthenticator.html#signIn">signIn</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-auth.OAuthAuthenticator.html#socialSignIn">s
|
||
|
</nav>
|
||
|
|
||
|
<div id="main">
|
||
|
|
||
|
<h1 class="page-title">RetryRestClient.js</h1>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
<section>
|
||
|
<article>
|
||
|
<pre class="prettyprint source linenums"><code>
|
||
|
var Promise = require('bluebird');
|
||
|
var retry = require('retry');
|
||
|
var ArgumentError = require('rest-facade').ArgumentError;
|
||
|
var assign = Object.assign || require('object.assign');
|
||
|
|
||
|
var DEFAULT_OPTIONS = { maxRetries: 10, enabled: true };
|
||
|
|
||
|
/**
|
||
|
* @class RetryRestClient
|
||
|
* Wrapper Rest Client that adds Retry functionality when requests are failing due to rate limiting (status code 429).
|
||
|
* @constructor
|
||
|
* @memberOf module:management
|
||
|
* @param {Object} restClient RestClient.
|
||
|
* @param {Object} [options] Options for the RetryRestClient.
|
||
|
* @param {Object} [options.enabled:true] Enabled or Disable Retry Policy functionality.
|
||
|
* @param {Number} [options.maxRetries=10] The maximum amount of times to retry the operation. Default is 10.
|
||
|
*/
|
||
|
var RetryRestClient = function(restClient, options){
|
||
|
if (restClient === null || typeof restClient !== 'object') {
|
||
|
throw new ArgumentError('Must provide RestClient');
|
||
|
}
|
||
|
|
||
|
var params = assign({}, DEFAULT_OPTIONS, options);
|
||
|
|
||
|
if (typeof params.enabled !== 'boolean') {
|
||
|
throw new ArgumentError('Must provide enabled boolean value');
|
||
|
}
|
||
|
|
||
|
if (typeof params.maxRetries !== 'number' || params.maxRetries <= 0) {
|
||
|
throw new ArgumentError('Must provide maxRetries as a positive number');
|
||
|
}
|
||
|
|
||
|
this.restClient = restClient;
|
||
|
this.maxRetries = params.maxRetries;
|
||
|
this.enabled = params.enabled;
|
||
|
}
|
||
|
|
||
|
RetryRestClient.prototype.getAll = function ( /* [params], [callback] */ ) {
|
||
|
return this.invoke('getAll', arguments);
|
||
|
};
|
||
|
|
||
|
RetryRestClient.prototype.get = function ( /* [params], [callback] */ ) {
|
||
|
return this.invoke('get', arguments);
|
||
|
}
|
||
|
|
||
|
RetryRestClient.prototype.create = function ( /* [params], [callback] */ ) {
|
||
|
return this.invoke('create', arguments);
|
||
|
}
|
||
|
|
||
|
RetryRestClient.prototype.patch = function ( /* [params], [callback] */ ) {
|
||
|
return this.invoke('patch', arguments);
|
||
|
}
|
||
|
|
||
|
RetryRestClient.prototype.update = function ( /* [params], [callback] */ ) {
|
||
|
return this.invoke('update', arguments);
|
||
|
}
|
||
|
|
||
|
RetryRestClient.prototype.delete = function ( /* [params], [callback] */ ) {
|
||
|
return this.invoke('delete', arguments);
|
||
|
}
|
||
|
|
||
|
RetryRestClient.prototype.invoke = function(method, args){
|
||
|
var cb;
|
||
|
args = Array.prototype.slice.call(args); // convert array-like object to array.
|
||
|
if(args && args[args.length -1] instanceof Function){
|
||
|
cb = args[args.length -1];
|
||
|
args.pop(); // Remove the callback
|
||
|
}
|
||
|
|
||
|
var promise = this.handleRetry(method, args);
|
||
|
|
||
|
if (cb instanceof Function) {
|
||
|
promise
|
||
|
.then(cb.bind(null, null))
|
||
|
.catch(cb);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
return promise;
|
||
|
}
|
||
|
|
||
|
RetryRestClient.prototype.handleRetry = function(method, args){
|
||
|
if(!this.enabled){
|
||
|
return this.restClient[method].apply(this.restClient, args);
|
||
|
}
|
||
|
|
||
|
var retryOptions = {
|
||
|
retries: this.maxRetries,
|
||
|
factor: 1,
|
||
|
minTimeout: 1, // retry immediate, use custom logic to control this.
|
||
|
randomize: false
|
||
|
};
|
||
|
|
||
|
var self = this;
|
||
|
var promise = new Promise(function (resolve, reject) {
|
||
|
var operation = retry.operation(retryOptions);
|
||
|
|
||
|
operation.attempt(function(){
|
||
|
self.restClient[method].apply(self.restClient, args)
|
||
|
.then(function(body) {
|
||
|
resolve(body);
|
||
|
})
|
||
|
.catch(function(err) {
|
||
|
self.invokeRetry(err, operation, reject);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
return promise;
|
||
|
};
|
||
|
|
||
|
RetryRestClient.prototype.invokeRetry = function(err, operation , reject){
|
||
|
var ratelimits = this.extractRatelimits(err);
|
||
|
if(ratelimits){
|
||
|
var delay = ratelimits.reset * 1000 - new Date().getTime();
|
||
|
if(delay > 0){
|
||
|
this.retryWithDelay(delay, operation, err, reject);
|
||
|
}else{
|
||
|
this.retryWithImmediate(operation, err, reject);
|
||
|
}
|
||
|
}else{
|
||
|
reject(err);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
RetryRestClient.prototype.extractRatelimits = function(err){
|
||
|
if(err && err.statusCode === 429 && err.originalError && err.originalError.response){
|
||
|
var headers = err.originalError.response.header;
|
||
|
if(headers && headers['x-ratelimit-limit']){
|
||
|
return {
|
||
|
limit: headers['x-ratelimit-limit'],
|
||
|
remaining: headers['x-ratelimit-remaining'],
|
||
|
reset: headers['x-ratelimit-reset']
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
RetryRestClient.prototype.retryWithImmediate = function(operation, err, reject){
|
||
|
if(operation.retry(err)){
|
||
|
return;
|
||
|
}
|
||
|
reject(err);
|
||
|
}
|
||
|
|
||
|
RetryRestClient.prototype.retryWithDelay = function(delay, operation, err, reject){
|
||
|
setTimeout(() => {
|
||
|
if(operation.retry(err)){
|
||
|
return;
|
||
|
}
|
||
|
reject(err);
|
||
|
}, delay);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
module.exports = RetryRestClient;
|
||
|
</code></pre>
|
||
|
</article>
|
||
|
</section>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
</div>
|
||
|
|
||
|
<br class="clear">
|
||
|
|
||
|
<footer>
|
||
|
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Dec 08 2017 10:01:22 GMT-0300 (-03) using the Minami theme.
|
||
|
</footer>
|
||
|
|
||
|
<script>prettyPrint();</script>
|
||
|
<script src="scripts/linenumber.js"></script>
|
||
|
</body>
|
||
|
</html>
|