Skip to content

Commit 08e9784

Browse files
authored
Merge pull request watson-developer-cloud#854 from watson-developer-cloud/v4.0.0-branch
V4.0.0 branch
2 parents be3ef04 + bd74547 commit 08e9784

File tree

89 files changed

+14849
-24062
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+14849
-24062
lines changed

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"tabWidth": 2,
55
"singleQuote": true,
66
"trailingComma": "es5",
7-
"parser": "babylon"
7+
"parser": "babel"
88
}

README.md

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Node.js client library to use the Watson APIs.
2020
* [IAM](#iam)
2121
* [Username and password](#username-and-password)
2222
* [API key](#api-key)
23+
* [Callbacks vs Promises](#callbacks-vs-promises)
2324
* [Sending request headers](#sending-request-headers)
2425
* [Parsing HTTP response](#parsing-http-response)
2526
* [Data collection opt-out](#data-collection-opt-out)
@@ -64,8 +65,9 @@ Credentials are checked for in the following order:
6465
1. Hard-coded or programatic credentials passed to the service constructor
6566

6667
2. Environment variables:
67-
- `SERVICE_NAME_USERNAME` and `SERVICE_NAME_PASSWORD` environment properties (or `SERVICE_NAME_API_KEY` when appropriate) and, optionally, `SERVICE_NAME_URL`
68+
- `SERVICE_NAME_USERNAME` and `SERVICE_NAME_PASSWORD` environment properties
6869
- If using IAM: `SERVICE_NAME_IAM_APIKEY` and optionally `SERVICE_NAME_IAM_URL`, or `SERVICE_NAME_IAM_ACCESS_TOKEN`
70+
- Optionally, `SERVICE_NAME_URL`
6971

7072
3. IBM-Cloud-supplied credentials (via the `VCAP_SERVICES` JSON-encoded environment property)
7173

@@ -187,6 +189,39 @@ var discovery = new DiscoveryV1({
187189
});
188190
```
189191

192+
### Callbacks vs Promises
193+
194+
All SDK methods are asynchronous, as they are making network requests to Watson services. To handle receiving the data from these requests, the SDK offers support for both Promises and Callback functions. A Promise will be returned by default unless a Callback function is provided.
195+
196+
```js
197+
const discovery = new watson.DiscoveryV1({
198+
/* iam_apikey, version, url, etc... */
199+
});
200+
201+
// using Promises
202+
discovery.listEnvironments()
203+
.then(body => {
204+
console.log(JSON.stringify(body, null, 2));
205+
})
206+
.catch(err => {
207+
console.log(err);
208+
});
209+
210+
// using Promises provides the ability to use async / await
211+
async function callDiscovery() { // note that callDiscovery also returns a Promise
212+
const body = await discovery.listEnvironments();
213+
}
214+
215+
// using a Callback function
216+
discovery.listEnvironments((err, res) => {
217+
if (err) {
218+
console.log(err);
219+
} else {
220+
console.log(JSON.stringify(res, null, 2));
221+
}
222+
});
223+
```
224+
190225
### Setting the service URL
191226

192227
You can set the service URL by calling the setServiceUrl() method.
@@ -231,6 +266,8 @@ assistant.message({
231266

232267
To retrieve the HTTP response, all methods can be called with a callback function with three parameters, with the third being the response. Users for example may retrieve the response headers with this usage pattern.
233268

269+
If using Promises, the parameter `return_response` must be added and set to `true`. Then, the result returned will be equivalent to the third argument in the callback function - the entire response.
270+
234271
Here is an example of how to access the response headers for Watson Assistant:
235272

236273
```js
@@ -245,6 +282,18 @@ assistant.message(params, function(err, result, response) {
245282
console.log(response.headers);
246283
});
247284

285+
// using Promises
286+
287+
params.return_response = true;
288+
289+
assistant.message(params)
290+
.then(response => {
291+
console.log(response.headers);
292+
})
293+
.catch(err => {
294+
console.log('error:', err);
295+
});
296+
248297
```
249298

250299
### Data collection opt-out
@@ -367,7 +416,7 @@ assistant.message(
367416

368417
### Assistant v1
369418

370-
Use the [Assistant][conversation] service to determine the intent of a message.
419+
Use the [Assistant][assistant] service to determine the intent of a message.
371420

372421
Note: You must first create a workspace via IBM Cloud. See [the documentation](https://console.bluemix.net/docs/services/conversation/index.html#about) for details.
373422

@@ -430,10 +479,6 @@ compareComply.compareDocuments(
430479

431480
```
432481

433-
### Conversation
434-
435-
This service has been renamed to Assistant.
436-
437482
### Discovery
438483

439484
Use the [Discovery Service][discovery] to search and analyze structured and unstructured data.
@@ -746,10 +791,10 @@ visualRecognition.classify(params, function(err, res) {
746791
## Composing services
747792
748793
### Integration of Tone Analyzer with Conversation
749-
Sample code for [integrating Tone Analyzer and Conversation][conversation_tone_analyzer_example] is provided in the [examples directory][examples].
794+
Sample code for [integrating Tone Analyzer and Assistant][assistant_tone_analyzer_example] is provided in the [examples directory][examples].
750795
751796
## Unauthenticated requests
752-
By default, the library tries to use Basic Auth and will ask for `api_key` or `username` and `password` and send an `Authorization: Basic XXXXXXX`. You can avoid this by using:
797+
By default, the library tries to authenticate and will ask for `iam_apikey`, `iam_access_token`, or `username` and `password` to send an `Authorization` header. You can avoid this by using:
753798
754799
`use_unauthenticated`.
755800
@@ -796,7 +841,6 @@ This library is licensed under Apache 2.0. Full license text is available in
796841
See [CONTRIBUTING](https://github.com/watson-developer-cloud/node-sdk/blob/master/.github/CONTRIBUTING.md).
797842
798843
[assistant]: https://www.ibm.com/watson/services/conversation/
799-
[conversation]: https://www.ibm.com/watson/services/conversation/
800844
[discovery]: https://www.ibm.com/watson/services/discovery/
801845
[personality_insights]: https://www.ibm.com/watson/services/personality-insights/
802846
[visual_recognition]: https://www.ibm.com/watson/services/visual-recognition/
@@ -808,10 +852,9 @@ See [CONTRIBUTING](https://github.com/watson-developer-cloud/node-sdk/blob/maste
808852
[watson-dashboard]: https://console.bluemix.net/dashboard/apps?category=watson
809853
[npm_link]: https://www.npmjs.com/package/watson-developer-cloud
810854
[request_github]: https://github.com/request/request
811-
[dialog_migration]: https://console.bluemix.net/docs/services/conversation/index.html
812855
[examples]: https://github.com/watson-developer-cloud/node-sdk/tree/master/examples
813856
[document_conversion_integration_example]: https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/document_conversion_integration.v1.js
814-
[conversation_tone_analyzer_example]: https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/conversation_tone_analyzer_integration
857+
[assistant_tone_analyzer_example]: https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/conversation_tone_analyzer_integration
815858
[license]: http://www.apache.org/licenses/LICENSE-2.0
816859
[vcap_services]: https://console.bluemix.net/docs/services/watson/getting-started-variables.html
817860
[ibm-cloud-onboarding]: http://console.bluemix.net/registration?target=/developer/watson&cm_sp=WatsonPlatform-WatsonServices-_-OnPageNavLink-IBMWatson_SDKs-_-Node

0 commit comments

Comments
 (0)