Skip to content

Commit 60c8139

Browse files
fix: update examples to use apikey
Related to: #1007
1 parent 71864eb commit 60c8139

21 files changed

Lines changed: 162 additions & 212 deletions

examples/.eslintrc.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module.exports = {
2-
"parserOptions": { "ecmaVersion": 6 },
3-
"rules": {
4-
"no-console": "off",
5-
"node/no-missing-require": "off",
6-
"require-jsdoc": "off",
7-
"valid-jsdoc": "off",
8-
"no-process-exit": "off",
9-
"prefer-const": "off",
10-
"no-var": "off"
11-
}
2+
parserOptions: { ecmaVersion: 6 },
3+
rules: {
4+
'no-console': 'off',
5+
'node/no-missing-require': 'off',
6+
'require-jsdoc': 'off',
7+
'valid-jsdoc': 'off',
8+
'no-process-exit': 'off',
9+
'prefer-const': 'off',
10+
'no-var': 'off',
11+
},
1212
};

examples/assistant.v1.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@ var AssistantV1 = require('ibm-watson/assistant/v1');
66
* Instantiate the Watson Assistant Service
77
*/
88
var assistant = new AssistantV1({
9-
username: process.env.ASSISTANT_USERNAME || '<assistant_username>',
10-
password: process.env.ASSISTANT_PASSWORD || '<assistant_password>',
11-
version: '2018-02-16'
9+
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
10+
// iam_apikey: 'INSERT YOUR IAM API KEY HERE',
11+
version: '2020-04-30',
1212
});
1313

1414
/**
1515
* Calls the assistant message api.
1616
* returns a promise
1717
*/
18-
var message = function(text, context) {
18+
var message = function (text, context) {
1919
var payload = {
2020
workspace_id: process.env.WORKSPACE_ID || '<workspace_id>',
2121
input: {
22-
text: text
22+
text: text,
2323
},
24-
context: context
24+
context: context,
2525
};
2626
return new Promise((resolve, reject) =>
27-
assistant.message(payload, function(err, data) {
27+
assistant.message(payload, function (err, data) {
2828
if (err) {
2929
reject(err);
3030
} else {
@@ -49,9 +49,7 @@ message('first message', undefined)
4949
})
5050
.then(response2 => {
5151
console.log(JSON.stringify(response2, null, 2), '\n--------');
52-
console.log(
53-
'Note that the two reponses should have the same context.conversation_id'
54-
);
52+
console.log('Note that the two reponses should have the same context.conversation_id');
5553
})
5654
.catch(err => {
5755
// APPLICATION-SPECIFIC CODE TO PROCESS THE ERROR

examples/assistant_tone_analyzer_integration/tone_assistant_integration.v1.js

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,6 @@
88
* Service credentials can be provided directly in this file, or can be saved to a .env file located in the
99
* same directory.
1010
*
11-
* Requirements:
12-
* 1. Tone Analyzer Service instance:
13-
* - https://cloud.ibm.com/catalog/services/tone-analyzer
14-
* - credentials for this service to be provided below in tone_analyzer variable
15-
* - replace <tone_analyzer_username> and <tone_analyzer_password>
16-
* 2. Assistant Service instance:
17-
* - https://cloud.ibm.com/catalog/services/watson-assistant
18-
* - credentials for this service to be provided below in the assistant variable
19-
* - replace <assistant_username> and <assistant_password>
20-
* 3. Workspace id:
21-
* - a workspace containing intents, entities and dialog nodes must be created using the tool
22-
* available through the IBM Cloud Assistant Service. Details are available at
23-
* https://github.com/watson-developer-cloud/assistant-simple#workspace
24-
* - replace <workspace_id> in the payload variable
25-
*
26-
* Run the code using the command:
27-
* node tone_assistant_integration.v1.js
2811
*/
2912

3013
'use strict';
@@ -39,18 +22,18 @@ require('dotenv').config({ silent: true });
3922
* Instantiate the Watson Assistant Service
4023
*/
4124
var assistant = new AssistantV1({
42-
username: process.env.ASSISTANT_USERNAME || '<assistant_username>',
43-
password: process.env.ASSISTANT_PASSWORD || '<assistant_password>',
44-
version: '2017-05-26'
25+
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
26+
// iam_apikey: 'INSERT YOUR IAM API KEY HERE',
27+
version: '2020-04-30',
4528
});
4629

4730
/**
4831
* Instantiate the Watson Tone Analyzer Service
4932
*/
5033
var toneAnalyzer = new ToneAnalyzerV3({
51-
username: process.env.TONE_ANALYZER_USERNAME || '<tone_analyzer_username>',
52-
password: process.env.TONE_ANALYZER_PASSWORD || '<tone_analyzer_password>',
53-
version: '2017-09-21'
34+
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
35+
// iam_apikey: 'INSERT YOUR IAM API KEY HERE',
36+
version: '2017-09-21',
5437
});
5538

5639
/**
@@ -66,8 +49,8 @@ var maintainToneHistoryInContext = true;
6649
var payload = {
6750
workspace_id: process.env.WORKSPACE_ID || '<workspace_id>',
6851
input: {
69-
text: 'I am not happy today :('
70-
}
52+
text: 'I am not happy today :(',
53+
},
7154
};
7255

7356
/**
@@ -84,12 +67,8 @@ function invokeToneAssistant(payload, maintainToneHistoryInContext) {
8467
tone_detection
8568
.invokeToneAsync(payload, toneAnalyzer)
8669
.then(tone => {
87-
tone_detection.updateUserTone(
88-
payload,
89-
tone,
90-
maintainToneHistoryInContext
91-
);
92-
assistant.message(payload, function(err, data) {
70+
tone_detection.updateUserTone(payload, tone, maintainToneHistoryInContext);
71+
assistant.message(payload, function (err, data) {
9372
if (err) {
9473
// APPLICATION-SPECIFIC CODE TO PROCESS THE ERROR
9574
// FROM ASSISTANT SERVICE
@@ -101,7 +80,7 @@ function invokeToneAssistant(payload, maintainToneHistoryInContext) {
10180
}
10281
});
10382
})
104-
.catch(function(err) {
83+
.catch(function (err) {
10584
console.log(JSON.stringify(err, null, 2));
10685
});
10786
}

examples/browserify/server.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ app.get(
1111
'/bundle.js',
1212
expressBrowserify('public/client.js', {
1313
watch: isDev,
14-
debug: isDev
14+
debug: isDev,
1515
})
1616
);
1717

@@ -22,13 +22,12 @@ dotenv.load({ silent: true });
2222

2323
// For local development, specify the username and password or set env properties
2424
var ltAuthService = new AuthorizationV1({
25-
username: process.env.TONE_ANALYZER_USERNAME || '<username>',
26-
password: process.env.TONE_ANALYZER_PASSWORD || '<password>',
27-
url: ToneAnalyzerV3.URL
25+
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
26+
// iam_apikey: 'INSERT YOUR IAM API KEY HERE',
2827
});
2928

30-
app.get('/api/token/tone_analyzer', function(req, res) {
31-
ltAuthService.getToken(function(err, token) {
29+
app.get('/api/token/tone_analyzer', function (req, res) {
30+
ltAuthService.getToken(function (err, token) {
3231
if (err) {
3332
console.log('Error retrieving token: ', err);
3433
return res.status(500).send('Error retrieving token');
@@ -38,6 +37,6 @@ app.get('/api/token/tone_analyzer', function(req, res) {
3837
});
3938

4039
var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;
41-
app.listen(port, function() {
40+
app.listen(port, function () {
4241
console.log('Watson browserify example server running at http://localhost:%s/', port);
4342
});

examples/compare-comply.v1.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ const fs = require('fs');
44
const CompareComplyV1 = require('ibm-watson/compare-comply/v1');
55

66
const compareComply = new CompareComplyV1({
7-
// if left unspecified here, the SDK will fall back to the COMPARE_COMPLY_IAM_APIKEY
8-
// environment property, and then IBM Cloud's VCAP_SERVICES environment property
9-
iam_apikey: 'YOUR APIKEY',
10-
url: 'https://gateway.watsonplatform.net/compare-comply/api',
7+
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
8+
// iam_apikey: 'INSERT YOUR IAM API KEY HERE',
119
version: '2018-12-06',
1210
});
1311

examples/discovery.v1.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ var DiscoveryV1 = require('ibm-watson/discovery/v1');
44
var fs = require('fs');
55

66
var discovery = new DiscoveryV1({
7-
// if left unspecified here, the SDK will fall back to the DISCOVERY_USERNAME and DISCOVERY_PASSWORD
8-
// environment properties, and then IBM Cloud's VCAP_SERVICES environment property
9-
// username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
10-
// password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE'
11-
// url: 'INSERT YOUR URL FOR THE SERVICE HERE'
12-
username: 'YOUR USERNAME',
13-
password: 'YOUR PASSWORD',
14-
version: '2018-03-05',
15-
url: 'https://gateway.watsonplatform.net/discovery/api/'
7+
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
8+
// iam_apikey: 'INSERT YOUR IAM API KEY HERE',
9+
version: '2020-04-30',
1610
});
1711

18-
discovery.getEnvironments({}, function(error, data) {
12+
discovery.getEnvironments({}, function (error, data) {
1913
console.log(JSON.stringify(data, null, 2));
2014
});
2115

@@ -26,9 +20,9 @@ discovery.addDocument(
2620
{
2721
environment_id: 'YOUR ENVIRONMENT ID',
2822
collection_id: 'YOUR COLLECTION ID',
29-
file: file
23+
file: file,
3024
},
31-
function(error, data) {
25+
function (error, data) {
3226
if (error) {
3327
console.log(error);
3428
} else {

examples/language_translator.v3.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ const LanguageTranslatorV3 = require('ibm-watson/language-translator/v3');
77
* Instantiate the Watson Language Translator Service
88
*/
99
const languageTranslator = new LanguageTranslatorV3({
10-
username: process.env.LANGUAGE_TRANSLATOR_USERNAME || '<language_translator_username>',
11-
password: process.env.LANGUAGE_TRANSLATOR_PASSWORD || '<language_translator_password>',
12-
version: '2019-01-10'
10+
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
11+
// iam_apikey: 'INSERT YOUR IAM API KEY HERE',
12+
version: '2020-04-30',
1313
});
1414

1515
const params = {
16-
text: "Hello, this is a example of translating language with Watson.",
16+
text: 'Hello, this is a example of translating language with Watson.',
1717
source: 'en',
1818
target: 'es',
19-
}
19+
};
2020

2121
// return the body - primary use case
2222

23-
languageTranslator.translate(params)
23+
languageTranslator
24+
.translate(params)
2425
.then(body => {
2526
console.log(JSON.stringify(body, null, 2));
2627
console.log('\n');
@@ -34,7 +35,8 @@ languageTranslator.translate(params)
3435

3536
params.return_response = true;
3637

37-
languageTranslator.translate(params)
38+
languageTranslator
39+
.translate(params)
3840
.then(res => {
3941
console.log(JSON.stringify(res, null, 2));
4042
})

examples/natural_language_classifier.v1.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ var NaturalLanguageClassifierV1 = require('ibm-watson/natural-language-classifie
44
var fs = require('fs');
55

66
var classifier = new NaturalLanguageClassifierV1({
7-
username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
8-
password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE',
9-
version: 'v1',
10-
url: 'https://gateway.watsonplatform.net/natural-language-classifier/api/'
7+
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
8+
// iam_apikey: 'INSERT YOUR IAM API KEY HERE',
9+
version: '2020-04-30',
1110
});
1211

1312
// Optionally for IAM authentication
@@ -20,16 +19,11 @@ var classifier = new NaturalLanguageClassifierV1({
2019

2120
// Creating a classifier
2221
var params = {
23-
training_data: fs.createReadStream(
24-
'../test/resources/weather_data_train.csv'
25-
),
26-
metadata: Buffer.from(
27-
JSON.stringify({ language: 'en', name: 'my-classifier' }),
28-
'utf8'
29-
)
22+
training_data: fs.createReadStream('../test/resources/weather_data_train.csv'),
23+
metadata: Buffer.from(JSON.stringify({ language: 'en', name: 'my-classifier' }), 'utf8'),
3024
};
3125

32-
classifier.createClassifier(params, function(err, response) {
26+
classifier.createClassifier(params, function (err, response) {
3327
if (err) {
3428
console.log(err);
3529
} else {
@@ -42,9 +36,9 @@ classifier.createClassifier(params, function(err, response) {
4236
classifier.classify(
4337
{
4438
text: 'Is it sunny?',
45-
classifier_id: '<classifier-id>'
39+
classifier_id: '<classifier-id>',
4640
}, // from the previous command
47-
function(err, response) {
41+
function (err, response) {
4842
if (err) {
4943
console.log('error:', err);
5044
} else {

examples/natural_language_understanding.v1.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,24 @@ var NaturalLanguageUnderstandingV1 = require('ibm-watson/natural-language-unders
55
require('dotenv').config({ silent: true }); // optional
66

77
var nlu = new NaturalLanguageUnderstandingV1({
8-
// note: if unspecified here, credentials are pulled from environment properties:
9-
// NATURAL_LANGUAGE_UNDERSTANDING_USERNAME & NATURAL_LANGUAGE_UNDERSTANDING_PASSWORD
10-
// username: '<username>'.
11-
// password: '<password>',
12-
version: '2018-04-05',
13-
url: 'https://gateway.watsonplatform.net/natural-language-understanding/api/'
8+
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
9+
// iam_apikey: 'INSERT YOUR IAM API KEY HERE',
10+
version: '2020-04-30',
1411
});
1512

1613
var filename = '../test/resources/natural_language_understanding/energy-policy.html';
17-
fs.readFile(filename, 'utf-8', function(file_error, file_data) {
14+
fs.readFile(filename, 'utf-8', function (file_error, file_data) {
1815
if (file_error) {
1916
console.log(file_error);
2017
} else {
2118
var options = {
2219
html: file_data,
2320
features: {
2421
concepts: {},
25-
keywords: {}
26-
}
22+
keywords: {},
23+
},
2724
};
28-
nlu.analyze(options, function(err, res) {
25+
nlu.analyze(options, function (err, res) {
2926
if (err) {
3027
console.log(err);
3128
return;

0 commit comments

Comments
 (0)