Skip to content

Commit

Permalink
Merge pull request #38 from Watson-Personal-Assistant/pre-release
Browse files Browse the repository at this point in the history
Merging pre-release with master for 14-jun release
  • Loading branch information
gaynell-gonsalves authored Jun 18, 2018
2 parents c870144 + 32fce0a commit 19a8596
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
33 changes: 31 additions & 2 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ let Handler = function () {
* Initializes the handler, sets up the connection to wcs
*/
Handler.prototype.initialize = function () {
let rawJson = fs.readFileSync(path.join(process.env.skillSDKResDir, nluFolder, wcsFileName));
this.wcsCredentials = JSON.parse(rawJson);
if(process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE
&& process.env.WCS_VERSION && process.env.WCS_WORKSPACE_ID && process.env.WCS_WORKSPACE_NAME &&
process.env.WCS_WORKSPACE_LANGUAGE) {
setupWCSCredentialsFromEnv(this);
} else {
let rawJson = fs.readFileSync(path.join(process.env.skillSDKResDir, nluFolder, wcsFileName));
this.wcsCredentials = JSON.parse(rawJson);
}
if (this.wcsCredentials) {
let credentials = this.wcsCredentials.credentials;
setupWcs(this, credentials.url, credentials.username, credentials.password, credentials.version_date)
Expand Down Expand Up @@ -349,4 +355,27 @@ let saveContext = function (self, request, context, response) {
session: sessionContext
}
});
};

/**
* Sets up the handler's wcs credentials from environment variables
* @param self
*/

let setupWCSCredentialsFromEnv = function (self) {
self.wcsCredentials = {
"workspace": {
[process.env.WCS_WORKSPACE_LANGUAGE]: {
"name": process.env.WCS_WORKSPACE_NAME,
"workspace_id": process.env.WCS_WORKSPACE_ID
}
},
"credentials": {
"url": process.env.WCS_URL,
"version": process.env.WCS_VERSION,
"version_date": process.env.WCS_VERSION_DATE,
"password": process.env.WCS_PASSWORD,
"username": process.env.WCS_USERNAME
}
}
};
11 changes: 4 additions & 7 deletions lib/nlu/bundles/engines/wcs/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ var Workspace = function (credentials) {
try {
this.wcs = watson.conversation(options);
} catch (error) {
logger.error("ERROR " +
"module = wcs/workspace.js, " +
"function = Workspace, " +
"credentials = " + JSON.stringify(credentials) + ", " +
"error = there was an error while trying to connect to WCS: " + error);
logger.error("Error setting up your wcs workspace, please check your credentials" +
" (either in env vars or wcs.json). \n" + error);

}
};

Expand Down Expand Up @@ -61,8 +59,7 @@ Workspace.prototype.bind = function (id, cb) {
cb(`Failed to load workspace ${name} or doesn't exist`);
}
}).catch(err => {
logger.error('Failed to list wcs workspaces (check username / password)');
cb('Failed to list wcs workspaces');
cb('Failed to list wcs workspaces (check credentials)');
});
};

Expand Down
26 changes: 25 additions & 1 deletion lib/nlu/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ module.exports = Factory;

function getNLU(type, name) {
return new Promise(function (resolve, reject) {
const nlu = require(`${resPath}/nlu/${type}`);
let nlu;
if(process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE
&& process.env.WCS_VERSION && process.env.WCS_WORKSPACE_ID && process.env.WCS_WORKSPACE_NAME &&
process.env.WCS_WORKSPACE_LANGUAGE) {
nlu = createNLUFromEnv();
} else {
nlu = require(`${resPath}/nlu/${type}`);
}
if (!nlu) {
reject(errors.couldNotReadNluType);
}
Expand All @@ -136,3 +143,20 @@ function getNLU(type, name) {
})
}

function createNLUFromEnv() {
return {
"workspace": {
[process.env.WCS_WORKSPACE_LANGUAGE]: {
"name": process.env.WCS_WORKSPACE_NAME,
"workspace_id": process.env.WCS_WORKSPACE_ID
}
},
"credentials": {
"url": process.env.WCS_URL,
"version": process.env.WCS_VERSION,
"version_date": process.env.WCS_VERSION_DATE,
"password": process.env.WCS_PASSWORD,
"username": process.env.WCS_USERNAME
}
};
}

0 comments on commit 19a8596

Please sign in to comment.