From d992be0efe729c905b41fa8e75b4430c24b8d7c4 Mon Sep 17 00:00:00 2001 From: Adam Voliva Date: Tue, 31 Aug 2021 17:00:48 -0700 Subject: [PATCH 1/3] Add ability to pass access token. --- src/ClientSDK.js | 19 ++++---- src/api/EndpointApi.js | 1 - src/api/OAuth2.js | 100 +++++++++++++++++++++++------------------ 3 files changed, 68 insertions(+), 52 deletions(-) diff --git a/src/ClientSDK.js b/src/ClientSDK.js index 37241b9..1f884f9 100644 --- a/src/ClientSDK.js +++ b/src/ClientSDK.js @@ -23,13 +23,13 @@ export default class ClientSDK { throw new Error('options with appId and appSecret must be provided.'); } - const {appId, appSecret, logLevel, tlsAuth, basePath} = await options; + const {appId, appSecret, logLevel, tlsAuth, basePath, accessToken} = await options; - if (!appId) { + if (!appId && !accessToken) { throw new Error('appId is required.'); } - if (!appSecret) { + if (!appSecret && !accessToken) { throw new Error('appSecret is required.'); } @@ -44,9 +44,8 @@ export default class ClientSDK { this.basePath = basePath; logger.trace('Initializing SDK with options: ', options); - - return new Promise((resolve, reject) => { - this.oauth2.init(options.appId, options.appSecret) + const onInitPromise = (resolve, reject) => { + this.oauth2.init(options.appId, options.appSecret, options.accessToken) .then(() => { const apiClient = new ApiClient(); if (basePath || (basePath && basePath !== this.oauth2.apiClient.basePath)) { @@ -58,7 +57,12 @@ export default class ClientSDK { this.endpointClient = new EndpointApi({}, apiClient); resolve(); }).catch(reason => reject(reason)); - }); + }; + if (!accessToken) { + return new Promise(onInitPromise); + } else { + return new Promise(onInitPromise); + } } async startRealtimeRequest(options = {}) { @@ -67,7 +71,6 @@ export default class ClientSDK { } options.basePath = options.basePath || this.basePath; - const realtimeClient = new RealtimeApi(options, this.oauth2); const startRequest = (resolve, reject) => { diff --git a/src/api/EndpointApi.js b/src/api/EndpointApi.js index f79f822..8fadd89 100644 --- a/src/api/EndpointApi.js +++ b/src/api/EndpointApi.js @@ -157,7 +157,6 @@ export default class EndpointApi { } const endpointConnectRequest = EndpointConnectRequest.constructFromObject(request); - return new Promise((resolve, reject) => { try { this.connectionToEndpointApi.connectToEndpoint(endpointConnectRequest, (error, data, response) => { diff --git a/src/api/OAuth2.js b/src/api/OAuth2.js index 0f50825..ca95aa5 100644 --- a/src/api/OAuth2.js +++ b/src/api/OAuth2.js @@ -66,59 +66,73 @@ export default class OAuth2 { } } - init(appId, appSecret) { + init(appId, appSecret, appToken) { if (arguments.length < 2) { throw new Error(`Expected number of arguments 2, detected: ${arguments.length}`); } - if (!appId) { - throw new Error('appId is required.'); - } + if (appToken) { + return new Promise((resolve, reject) => { + this.activeToken = appToken; + logger.trace('Token received.'); + this.apiClient.authentications.jwt.apiKey = this.activeToken; + resolve({ + accessToken: appToken + }) + }); + } else { - if (!appSecret) { - throw new Error('appSecret is required.'); - } + if (!appId) { + throw new Error('appId is required.'); + } + + if (!appSecret) { + throw new Error('appSecret is required.'); + } - this.appId = appId; - this.appSecret = appSecret; - - return new Promise((resolve, reject) => { - logger.trace(`Initializing app with appId and appSecret`, appId, appSecret); - try { - const grant = Grant.constructFromObject({ - type: 'application', - appId, - appSecret - }); - this.authenticationApi.generateToken(grant, (err, data) => { - if (err) { - if (err.status && err.status === 401) { - const message = 'Combination of appId and appSecret is not valid.'; - logger.info(message); - reject({ - message, - internalError: err + this.appId = appId; + this.appSecret = appSecret; + + return new Promise((resolve, reject) => { + logger.trace(`Initializing app with appId and appSecret`, appId, appSecret); + try { + const grant = Grant.constructFromObject({ + type: 'application', + appId, + appSecret + }); + this.authenticationApi.generateToken(grant, (err, data) => { + if (err) { + if (err.status && err.status === 401) { + const message = 'Combination of appId and appSecret is not valid.'; + logger.info(message); + reject({ + message, + internalError: err + }); + } else { + reject(ErrorHandler.getError(err)); + } + } else if (data) { + this.processTokenResult(data); + const {accessToken, expiresIn} = data; + resolve({ + accessToken: accessToken, + expiresIn: expiresIn }); + } else { - reject(ErrorHandler.getError(err)); + reject(ErrorHandler.getError()); } - } else if (data) { - this.processTokenResult(data); - const {accessToken, expiresIn} = data; - resolve({ - accessToken: accessToken, - expiresIn: expiresIn - }); - - } else { - reject(ErrorHandler.getError()); - } - }); - } catch (e) { - reject(ErrorHandler.getError(e)); - } + }); + } catch (e) { + reject(ErrorHandler.getError(e)); + } + + }); + + } - }); } From b94f81f896c1cc9c869e08f8922f02da3b7df849 Mon Sep 17 00:00:00 2001 From: Adam Voliva Date: Tue, 31 Aug 2021 17:02:11 -0700 Subject: [PATCH 2/3] Up alpha version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9b6b929..4bf2d58 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@symblai/symbl-js", - "version": "1.1.3a", + "version": "1.1.3b", "description": "Javascript SDK for Symbl.ai's Language Insights API Platform", "main": "build/app.bundle.js", "dependencies": { From 7abd07dc3b0fd41b95dde91b809a7f69478d3721 Mon Sep 17 00:00:00 2001 From: Adam Voliva Date: Tue, 31 Aug 2021 17:06:47 -0700 Subject: [PATCH 3/3] Up version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4bf2d58..8f930fa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@symblai/symbl-js", - "version": "1.1.3b", + "version": "1.1.3c", "description": "Javascript SDK for Symbl.ai's Language Insights API Platform", "main": "build/app.bundle.js", "dependencies": {