Skip to content

Commit 7a47b7c

Browse files
saas-1216 -- per request retry config + whoami disable retries (#258)
* per request retry config + whoami disable retries * add request options defaults * add statusCode to simulate StatusCodeError * update version
1 parent 284e484 commit 7a47b7c

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

lib/logic/api/helper.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ const retryOptions = {
1818
};
1919

2020
function _makeResponseError(response) {
21-
return new Error(JSON.stringify(response.body || response));
21+
return new CFError({
22+
statusCode: response.statusCode,
23+
message: JSON.stringify(response.body || response)
24+
});
2225
}
2326

24-
const sendHttpRequest = async (httpOptions, authContext, throwOnUnauthorized, timeout = 30000) => {
27+
const sendHttpRequest = async (httpOptions, { authContext, throwOnUnauthorized, timeout = 30000, maxRetries, retryDelay, retryStrategy } = {}) => {
2528
let finalAuthContext;
2629
if (!authContext) {
2730
const authManager = require('../auth').manager; // eslint-disable-line
@@ -30,6 +33,16 @@ const sendHttpRequest = async (httpOptions, authContext, throwOnUnauthorized, ti
3033
finalAuthContext = authContext;
3134
}
3235

36+
if (maxRetries) {
37+
retryOptions.maxAttempts = maxRetries;
38+
}
39+
if (retryDelay) {
40+
retryOptions.retryDelay = retryDelay;
41+
}
42+
if (retryStrategy) {
43+
retryOptions.retryStrategy = retryStrategy;
44+
}
45+
3346
const finalOptions = _.merge(
3447
httpOptions,
3548
finalAuthContext.prepareHttpOptions(),

lib/logic/api/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const getByAuthContext = async (authContext) => {
66
method: 'GET',
77
};
88

9-
return sendHttpRequest(options, authContext);
9+
return sendHttpRequest(options, { authContext });
1010
};
1111

1212

lib/logic/auth/contexts/whoami.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ const whoami = async (context) => {
88
url: '/api/user/',
99
method: 'GET',
1010
};
11-
const user = await sendHttpRequest(userOptions, context, true, 2000);
11+
const user = await sendHttpRequest(userOptions, {
12+
authContext: context,
13+
throwOnUnauthorized: true,
14+
timeout: 2000,
15+
retryStrategy: () => false,
16+
});
1217
const accounts = _.get(user, 'account', {});
1318
const accountInfo = _.chain(accounts)
1419
.filter(account => account.name === user.activeAccountName)

lib/logic/auth/contexts/whoami.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jest.mock('../../api/helper', () => { // eslint-disable-line
3131
if (response) {
3232
return response();
3333
} else { // eslint-disable-line
34-
return apiHelper.sendHttpRequest(userOptions, context, flag);
34+
return apiHelper.sendHttpRequest(userOptions, {authContext: context, throwOnUnauthorized: flag});
3535
}
3636
};
3737

@@ -62,13 +62,13 @@ describe('whoami tests', () => {
6262
token: 'test-token',
6363
});
6464

65-
apiHelper.__setValidateParams((userOptions, context, flag) => {
65+
apiHelper.__setValidateParams((userOptions, requestOptions) => {
6666
expect(userOptions)
6767
.toEqual({
6868
'method': 'GET',
6969
'url': '/api/user/',
7070
});
71-
expect(flag)
71+
expect(requestOptions.throwOnUnauthorized)
7272
.toEqual(true);
7373
});
7474

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.9.12",
3+
"version": "0.9.13",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)