diff --git a/package.json b/package.json index 9b6b929..8f930fa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@symblai/symbl-js", - "version": "1.1.3a", + "version": "1.1.3c", "description": "Javascript SDK for Symbl.ai's Language Insights API Platform", "main": "build/app.bundle.js", "dependencies": { 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)); + } + + }); + + } - }); }