From 76f8d262c76d7f559dc8500df694e398aff9532b Mon Sep 17 00:00:00 2001 From: Andrey Kostyuchenko Date: Fri, 22 Jan 2021 15:55:45 +0300 Subject: [PATCH] PDKIO-1761: timeout is increased and handle 429 error --- package-lock.json | 2 +- package.json | 2 +- src/authenticators/client.js | 18 +++++++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 415af5c..210ec7c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@pdk/client", - "version": "0.7.1", + "version": "0.7.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8c3b660..37b04e7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@pdk/client", - "version": "0.7.1", + "version": "0.7.2", "description": "ProdataKey pdk.io API Client Library for Javascript", "scripts": { "test": "mocha -r src/test/setup.js -r esm \"src/**/*.spec.js\"", diff --git a/src/authenticators/client.js b/src/authenticators/client.js index 90e0ba8..c555fba 100644 --- a/src/authenticators/client.js +++ b/src/authenticators/client.js @@ -22,7 +22,7 @@ export const clientauth = ({ }) => async () => { // merge provided default http options with PDK defaults and set it to the Issuer object - Issuer.defaultHttpOptions = {timeout: 10000, retries: 1, ...default_http_options} + Issuer.defaultHttpOptions = {timeout: 60000, retries: 1, ...default_http_options} debug(`Authenticating as client_id: ${client_id}`); @@ -54,8 +54,16 @@ async () => { outstanding = client.grant({ grant_type: 'client_credentials' }) - token_set = await outstanding - outstanding = undefined + try { + token_set = await outstanding + outstanding = undefined + } catch(err) { + outstanding = undefined + if (err && err.statusCode === 429) { + _sleep(4000); + await oauthtoken_set.refresh(); + } + } debug(`Got fresh token: ${JSON.stringify(token_set)}`); } else { @@ -74,3 +82,7 @@ async () => { await oauthtoken_set.refresh(); return oauthtoken_set; } + +function _sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +}