Skip to content

Commit

Permalink
Resolves #20 - Adding retry support with axon-retry
Browse files Browse the repository at this point in the history
* Adds retry support if retries configuration is provided
* Existing functionality kept as is if retries is not provided
* Manually tested with custom AWS API Gateway
** With both AWS_IAM auth and no auth
** With retries and retriesCondition, with retries only (new functionality)
** With no retries (existing functionality)
** With GET, POST and PUT HTTP methods
  • Loading branch information
dagnello committed May 31, 2017
1 parent dca7237 commit c7b63cc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ var apigClient = apigClientFactory.newClient({
sessionToken: 'SESSION_TOKEN', //OPTIONAL: If you are using temporary credentials you must include the session token
region: 'eu-west-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1
systemClockOffset: 0 // OPTIONAL: An offset value in milliseconds to apply to signing time
retries: 4, // OPTIONAL: Number of times to retry before failing. Uses axon-retry plugin.
retryCondition: (err) => { // OPTIONAL: Callback to further control if request should be retried. Uses axon-retry plugin.
return err.response.status === 500;
}
});
```

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
},
"dependencies": {
"axios": "^0.16.1",
"axios-retry": "^1.2.0",
"crypto-js": "^3.1.9-1",
"url": "^0.11.0",
"url-template": "^2.0.8"
Expand Down
4 changes: 4 additions & 0 deletions src/apigClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ apigClientFactory.newClient = (config) => {
defaultContentType: config.defaultContentType,
defaultAcceptType: config.defaultAcceptType,
systemClockOffset: config.systemClockOffset,
retries: config.retries,
retryCondition: config.retryCondition,
};

let authType = 'NONE';
Expand All @@ -97,6 +99,8 @@ apigClientFactory.newClient = (config) => {
endpoint: endpoint,
defaultContentType: config.defaultContentType,
defaultAcceptType: config.defaultAcceptType,
retries: config.retries,
retryCondition: config.retryCondition,
};

const apiGatewayClient = apiGatewayClientFactory.newClient(
Expand Down
16 changes: 14 additions & 2 deletions src/lib/apiGatewayCore/sigV4Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/* eslint max-len: ["error", 100]*/

import axios from 'axios';
import axiosRetry from 'axios-retry';
import SHA256 from 'crypto-js/sha256';
import encHex from 'crypto-js/enc-hex';
import HmacSHA256 from 'crypto-js/hmac-sha256';
Expand Down Expand Up @@ -146,6 +147,8 @@ sigV4ClientFactory.newClient = function(config) {
awsSigV4Client.serviceName = utils.assertDefined(config.serviceName, 'serviceName');
awsSigV4Client.region = utils.assertDefined(config.region, 'region');
awsSigV4Client.endpoint = utils.assertDefined(config.endpoint, 'endpoint');
awsSigV4Client.retries = config.retries;
awsSigV4Client.retryCondition = config.retryCondition;

awsSigV4Client.makeRequest = function(request) {
let verb = utils.assertDefined(request.verb, 'verb');
Expand Down Expand Up @@ -226,11 +229,20 @@ sigV4ClientFactory.newClient = function(config) {
}

let signedRequest = {
method: verb,
url: url,
headers: headers,
data: body,
};
if (config.retries !== undefined) {
signedRequest.baseURL = url;
let client = axios.create(signedRequest);
axiosRetry(client, {
retries: config.retries,
retryCondition: config.retryCondition,
});
return client.request({method: verb});
}
signedRequest.method = verb;
signedRequest.url = url;
return axios(signedRequest);
};

Expand Down
16 changes: 14 additions & 2 deletions src/lib/apiGatewayCore/simpleHttpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/* eslint max-len: ["error", 100]*/

import axios from 'axios';
import axiosRetry from 'axios-retry';
import utils from './utils';

const simpleHttpClientFactory = {};
Expand Down Expand Up @@ -71,14 +72,25 @@ simpleHttpClientFactory.newClient = (config) => {
if (queryString != '') {
url += '?' + queryString;
}

let simpleHttpRequest = {
method: verb,
url: url,
headers: headers,
data: body,
};
if (config.retries !== undefined) {
simpleHttpRequest.baseURL = url;
let client = axios.create(simpleHttpRequest);
axiosRetry(client, {
retries: config.retries,
retryCondition: config.retryCondition,
});
return client.request({method: verb});
}
simpleHttpRequest.method = verb;
simpleHttpRequest.url = url;
return axios(simpleHttpRequest);
};

return simpleHttpClient;
};

Expand Down
4 changes: 4 additions & 0 deletions test/apigClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const config = {
accessKey: '00000000000000000000',
secretKey: '0000000000000000000000000000000000000000',
apiKey: '0000000000000000000000000000000000000000',
retry: 4,
retryCondition: (err) => {
return err.response.status === 500;
},
};

test('apigClientFactory exists', t => {
Expand Down

0 comments on commit c7b63cc

Please sign in to comment.