Skip to content

Commit

Permalink
API extract reusable retry and shouldRetry functions
Browse files Browse the repository at this point in the history
handleExpiredAuthToken does not need a then and a catch block
that do the same thing
We don't need to call `addDefaultValuesToParameters` - `Network.post` already does that
  • Loading branch information
kidroca committed Dec 2, 2021
1 parent 3974014 commit 1371639
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ function isAuthTokenRequired(command) {
], command);
}

/**
* Determines are we supposed to retry the given request
* @param {Object} request
* @returns {Boolean}
*/
function shouldRetry(request) {
return lodashGet(request, 'data.shouldRetry', false);
}

/**
* When the request should be retried add it back to the queue
* It will either get retried now, or later when we're back online
* @param {Object} request
*/
function retry(request) {
Network.post(request.command, request.data, request.type, request.shouldUseSecure)
.then(request.resolve)
.catch(request.reject);
}

/**
* Adds default values to our request data
*
Expand Down Expand Up @@ -92,35 +112,23 @@ Network.registerParameterEnhancer(addDefaultValuesToParameters);
* Function used to handle expired auth tokens. It re-authenticates with the API and
* then replays the original request
*
* @param {String} originalCommand
* @param {Object} [originalParameters]
* @param {String} [originalType]
* @returns {Promise}
* @param {Object} originalRequest
*/
function handleExpiredAuthToken(originalCommand, originalParameters, originalType) {
function handleExpiredAuthToken(originalRequest) {
// When the authentication process is running, and more API requests will be requeued and they will
// be performed after authentication is done.
if (isAuthenticating) {
return Network.post(originalCommand, originalParameters, originalType);
retry(originalRequest);
return;
}

// Prevent any more requests from being processed while authentication happens
Network.pauseRequestQueue();
isAuthenticating = true;

// eslint-disable-next-line no-use-before-define
return reauthenticate(originalCommand)
.then(() => {
// Now that the API is authenticated, make the original request again with the new authToken
const params = addDefaultValuesToParameters(originalCommand, originalParameters);
return Network.post(originalCommand, params, originalType);
})
.catch(() => (

// If the request did not succeed because of a networking issue or the server did not respond requeue the
// original request.
Network.post(originalCommand, originalParameters, originalType)
));
reauthenticate(originalRequest.command)
.finally(() => retry(originalRequest));
}

Network.registerRequestHandler((queuedRequest, finalParameters) => {
Expand Down Expand Up @@ -159,15 +167,12 @@ Network.registerResponseHandler((queuedRequest, response) => {
// There are some API requests that should not be retried when there is an auth failure like
// creating and deleting logins. In those cases, they should handle the original response instead
// of the new response created by handleExpiredAuthToken.
const shouldRetry = lodashGet(queuedRequest, 'data.shouldRetry');
if (!shouldRetry || unableToReauthenticate) {
if (!shouldRetry(queuedRequest) || unableToReauthenticate) {
queuedRequest.resolve(response);
return;
}

handleExpiredAuthToken(queuedRequest.command, queuedRequest.data, queuedRequest.type)
.then(queuedRequest.resolve)
.catch(queuedRequest.reject);
handleExpiredAuthToken(queuedRequest);
return;
}

Expand All @@ -193,11 +198,8 @@ Network.registerErrorHandler((queuedRequest, error) => {

// When the request should be retried add it back to the queue
// It will either get retried now, or later when we're back online
if (lodashGet(queuedRequest, 'data.shouldRetry')) {
Network.post(queuedRequest.command, queuedRequest.data, queuedRequest.type, queuedRequest.shouldUseSecure)
.then(queuedRequest.resolve)
.catch(queuedRequest.reject);

if (shouldRetry(queuedRequest)) {
retry(queuedRequest);
return;
}

Expand Down

0 comments on commit 1371639

Please sign in to comment.