Skip to content

Commit fe142ed

Browse files
committed
docs: Update all READMEs with auth changes and new core features
1 parent 5a70ef6 commit fe142ed

File tree

12 files changed

+96
-109
lines changed

12 files changed

+96
-109
lines changed

README.md

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ Java client library to use the [Watson APIs][wdc].
2020
* [Authentication](#authentication)
2121
* [IAM](#iam)
2222
* [Username and password](#username-and-password)
23-
* [ICP](#icp)
23+
* [Cloud Pak for Data](#cloud-pak-for-data)
2424
* [Using the SDK](#using-the-sdk)
2525
* [Parsing responses](#parsing-responses)
2626
* [Configuring the HTTP client](#configuring-the-http-client)
2727
* [Making asynchronous API calls](#making-asynchronous-api-calls)
2828
* [Default headers](#default-headers)
2929
* [Sending request headers](#sending-request-headers)
30+
* [Canceling requests](#canceling-requests)
3031
* [FAQ](#faq)
3132
* IBM Watson Services
3233
* [Assistant](assistant)
@@ -114,7 +115,7 @@ Watson services are migrating to token-based Identity and Access Management (IAM
114115

115116
- With some service instances, you authenticate to the API by using **[IAM](#iam)**.
116117
- In other instances, you authenticate by providing the **[username and password](#username-and-password)** for the service instance.
117-
- If you're using a Watson service on ICP, you'll need to authenticate in a [specific way](#icp).
118+
- If you're using a Watson service on Cloud Pak for Data, you'll need to authenticate in a [specific way](#cloud-pak-for-data).
118119

119120
### Getting credentials
120121

@@ -170,68 +171,56 @@ Some services use token-based Identity and Access Management (IAM) authenticatio
170171
You supply either an IAM service **API key** or an **access token**:
171172

172173
- Use the API key to have the SDK manage the lifecycle of the access token. The SDK requests an access token, ensures that the access token is valid, and refreshes it if necessary.
173-
- Use the access token if you want to manage the lifecycle yourself. For details, see [Authenticating with IAM tokens](https://cloud.ibm.com/docs/services/watson/getting-started-iam.html). If you want to switch to API key, override your stored IAM credentials with an IAM API key. Then call the `setIamCredentials()` method again.
174+
- Use the access token if you want to manage the lifecycle yourself. For details, see [Authenticating with IAM tokens](https://cloud.ibm.com/docs/services/watson/getting-started-iam.html).
174175

175176

176177
Supplying the IAM API key:
177178

178179
```java
179180
// letting the SDK manage the IAM token
180-
IamOptions options = new IamOptions.Builder()
181-
.apiKey("<iam_api_key>")
182-
.url("<iam_url>") // optional - the default value is https://iam.cloud.ibm.com/identity/token
183-
.build();
184-
Discovery service = new Discovery("2017-11-07", options);
181+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
182+
Discovery service = new Discovery("2017-11-07", authenticator);
185183
```
186184

187185
Supplying the access token:
188186

189187
```java
190188
// assuming control of managing IAM token
191-
IamOptions options = new IamOptions.Builder()
192-
.accessToken("<access_token>")
193-
.build();
194-
Discovery service = new Discovery("2017-11-07", options);
189+
Authenticator authenticator = new BearerTokenAuthenticator("<access_token>");
190+
Discovery service = new Discovery("2017-11-07", authenticator);
195191
```
196192

197193
#### Username and password
198194

199195
```java
200-
// in the constructor
201-
BasicAuthConfig config = new BasicAuthConfig.Builder()
202-
.username("<username>")
203-
.password("<password")
204-
.build();
205-
Discovery service = new Discovery("2017-11-07", config);
196+
Authenticator authenticator = new BasicAuthenticator("<username>", "<password>");
197+
Discovery service = new Discovery("2017-11-07", authenticator);
206198
```
207199

208-
#### ICP
200+
#### Cloud Pak for Data
209201
Like IAM, you can pass in credentials to let the SDK manage an access token for you or directly supply an access token to do it yourself.
210202

211203
```java
212204
// letting the SDK manage the token
213-
ICP4DConfig config = new ICP4DConfig.Builder()
214-
.url("<ICP token exchange base URL>")
215-
.username("<username>")
216-
.password("<password>")
217-
.disableSSLVerification(true)
218-
.build();
219-
Discovery service = new Discovery("2017-11-07", config);
220-
service.setEndPoint("<service ICP URL>");
205+
Authenticator authenticator = new CloudPakForDataAuthenticator(
206+
"<CP4D token exchange base URL>",
207+
"<username>",
208+
"<password>",
209+
true, // disabling SSL verification
210+
null,
211+
);
212+
Discovery service = new Discovery("2017-11-07", authenticator);
213+
service.setEndPoint("<service CP4D URL>");
221214
```
222215

223216
```java
224217
// assuming control of managing the access token
225-
ICP4DConfig config = new ICP4DConfig.Builder()
226-
.url("<ICP token exchange base URL>")
227-
.userManagedAccessToken("<access token>")
228-
.disableSSLVerification(true)
229-
.build();
230-
Discovery service = new Discovery("2017-11-07", config);
231-
service.setEndPoint("<service ICP URL>");
218+
Authenticator authenticator = new BearerTokenAuthenticator("<access_token>");
219+
Discovery service = new Discovery("2017-11-07", authenticator);
220+
service.setEndPoint("<service CP4D URL>");
232221
```
233222

234-
Be sure to both disable SSL verification when authenticating and set the endpoint explicitly to the URL given in ICP.
223+
Be sure to both disable SSL verification when authenticating and set the endpoint explicitly to the URL given in Cloud Pak for Data.
235224

236225
## Using the SDK
237226

@@ -259,8 +248,9 @@ System.out.println("Response header names: " + responseHeaders.names());
259248
The HTTP client can be configured by using the `configureClient()` method on your service object, passing in an `HttpConfigOptions` object. Currently, the following options are supported:
260249
- Disabling SSL verification (only do this if you really mean to!) ⚠️
261250
- Using a proxy (more info here: [OkHTTPClient Proxy authentication how to?](https://stackoverflow.com/a/35567936/456564))
251+
- Setting HTTP logging verbosity
262252

263-
Here's an example of setting both of the above:
253+
Here's an example of setting the above:
264254

265255
```java
266256
Discovery service = new Discovery("2017-11-07");
@@ -269,6 +259,7 @@ Discovery service = new Discovery("2017-11-07");
269259
HttpConfigOptions options = new HttpConfigOptions.Builder()
270260
.disableSslVerification(true)
271261
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyHost", 8080)))
262+
.loggingLevel(HttpConfigOptions.LoggingLevel.BASIC)
272263
.build();
273264

274265
service.configureClient(options);
@@ -329,7 +320,7 @@ Default headers can be specified at any time by using the `setDefaultHeaders(Map
329320
The example below sends the `X-Watson-Learning-Opt-Out` header in every request preventing Watson from using the payload to improve the service.
330321

331322
```java
332-
PersonalityInsights service = new PersonalityInsights("2016-10-19");
323+
PersonalityInsights service = new PersonalityInsights("2016-10-19", new NoAuthAuthenticator());
333324

334325
Map<String, String> headers = new HashMap<String, String>();
335326
headers.put(WatsonHttpHeaders.X_WATSON_LEARNING_OPT_OUT, "true");
@@ -349,6 +340,44 @@ Response<WorkspaceCollection> workspaces = service.listWorkspaces()
349340
.execute();
350341
```
351342

343+
### Canceling requests
344+
345+
It's possible that you may want to cancel a request you make to a service. For example, you may set some timeout threshold and just want to cancel an asynchronous if it doesn't respond in time. You can do that by calling the `cancel()` method on your `ServiceCall` object. For example:
346+
347+
```java
348+
// time to consider timeout (in ms)
349+
long timeoutThreshold = 3000;
350+
351+
// storing ServiceCall object we'll use to list our Assistant v1 workspaces
352+
ServiceCall<WorkspaceCollection> call = service.listWorkspaces();
353+
354+
long startTime = System.currentTimeMillis();
355+
call.enqueue(new ServiceCallback<WorkspaceCollection>() {
356+
@Override
357+
public void onResponse(Response<WorkspaceCollection> response) {
358+
// store the result somewhere
359+
fakeDb.store("my-key", response.getResult());
360+
}
361+
362+
@Override
363+
public void onFailure(Exception e) {
364+
System.out.println("The request failed :(");
365+
}
366+
});
367+
368+
// keep waiting for the call to complete while we're within the timeout bounds
369+
while ((fakeDb.retrieve("my-key") == null) && (System.currentTimeMillis() - startTime < timeoutThreshold)) {
370+
Thread.sleep(500);
371+
}
372+
373+
// if we timed out and it's STILL not complete, we'll just cancel the call
374+
if (fakeDb.retrieve("my-key") == null) {
375+
call.cancel();
376+
}
377+
```
378+
379+
Doing so will call your `onFailure()` implementation.
380+
352381
## FAQ
353382

354383
### Does this SDK play well with Android?

assistant/README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ Use the [Assistant][assistant] service to identify intents, entities, and conduc
2424
// make sure to use the Assistant v1 import!
2525
import com.ibm.watson.assistant.v1.Assistant;
2626

27-
Assistant service = new Assistant("2018-02-16");
28-
IamOptions options = new IamOptions.Builder()
29-
.apiKey("<iam_api_key>")
30-
.build();
31-
service.setIamCredentials(options);
27+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
28+
Assistant service = new Assistant("2018-02-16", authenticator);
3229

3330
MessageInput input = new MessageInput();
3431
input.setText("Hi");
@@ -78,11 +75,8 @@ System.out.println(response);
7875
// make sure to use the Assistant v2 import!
7976
import com.ibm.watson.assistant.v2.Assistant;
8077

81-
Assistant service = new Assistant("2018-09-20");
82-
IamOptions options = new IamOptions.Builder()
83-
.apiKey("<iam_api_key>")
84-
.build();
85-
service.setIamCredentials(options);
78+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
79+
Assistant service = new Assistant("2018-09-20", authenticator);
8680

8781
MessageInput input = new MessageInput.Builder()
8882
.text("Hi")

compare-comply/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
## Usage
2020
Use the [Compare and Comply](https://cloud.ibm.com/docs/services/compare-comply/index.html#about) service to enable better and faster document understanding. Below is an example of converting a PDF file into HTML:
2121
```java
22-
CompareComply service = new CompareComply("2018-10-15");
23-
IamOptions options = new IamOptions.Builder()
24-
.apiKey("<iam_api_key>")
25-
.build();
26-
service.setIamCredentials(options);
22+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
23+
CompareComply service = new CompareComply("2018-10-15", authenticator);
2724

2825
ConvertToHtmlOptions convertToHtmlOptions = new ConvertToHtmlOptions.Builder()
2926
.file("~/path/to/file.pdf")

discovery/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
The [Discovery][discovery] wraps the environment, collection, configuration, document, and query operations of the Discovery service.
2121

2222
```java
23-
Discovery discovery = new Discovery("2017-11-07");
24-
IamOptions options = new IamOptions.Builder()
25-
.apiKey("<iam_api_key>")
26-
.build();
27-
service.setIamCredentials(options);
23+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
24+
Discovery discovery = new Discovery("2017-11-07", authenticator);
2825

2926
//Build an empty query on an existing environment/collection
3027
String environmentId = "<environmentId>";

language-translator/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ Select a domain, then identify or select the language of text, and then translat
2121
Example: Translate 'hello' from English to Spanish using the [Language Translator][language_translator] service.
2222

2323
```java
24-
LanguageTranslator service = new LanguageTranslator();
25-
IamOptions iamOptions = new IamOptions.Builder()
26-
.apiKey("<iam_api_key>")
27-
.build();
28-
service.setIamCredentials(iamOptions);
24+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
25+
LanguageTranslator service = new LanguageTranslator(authenticator);
2926

3027
TranslateOptions translateOptions = new TranslateOptions.Builder()
3128
.addText("hello")

natural-language-classifier/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
Use [Natural Language Classifier](https://cloud.ibm.com/docs/services/natural-language-classifier?topic=natural-language-classifier-natural-language-classifier) service to create a classifier instance by providing a set of representative strings and a set of one or more correct classes for each as training. Then use the trained classifier to classify your new question for best matching answers or to retrieve next actions for your application.
2121

2222
```java
23-
NaturalLanguageClassifier service = new NaturalLanguageClassifier();
24-
IamOptions options = new IamOptions.Builder()
25-
.apiKey("<iam_api_key>")
26-
.build();
27-
service.setIamCredentials(options);
23+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
24+
NaturalLanguageClassifier service = new NaturalLanguageClassifier(authenticator);
2825

2926
ClassifyOptions classifyOptions = new ClassifyOptions.Builder()
3027
.classifierId("<classifier-id>")

natural-language-understanding/README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@ Language Understanding will give you results for the features you request. The s
2323
analysis by default, so the results can ignore most advertisements and other unwanted content.
2424

2525
```java
26-
NaturalLanguageUnderstanding service = new NaturalLanguageUnderstanding("2017-02-27");
27-
IamOptions options = new IamOptions.Builder()
28-
.apiKey("<iam_api_key>")
29-
.build();
30-
service.setIamCredentials(options);
26+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
27+
NaturalLanguageUnderstanding service = new NaturalLanguageUnderstanding("2017-02-27", authenticator);
3128

3229
EntitiesOptions entities = new EntitiesOptions.Builder()
3330
.sentiment(true)
34-
.limit(1)
31+
.limit(1L)
3532
.build();
3633
Features features = new Features.Builder()
3734
.entities(entities)

personality-insights/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ Use linguistic analytics to infer personality and social characteristics, includ
2121
Example: Analyze text and get a personality profile using the [Personality Insights][personality_insights] service.
2222

2323
```java
24-
PersonalityInsights service = new PersonalityInsights("2016-10-19");
25-
IamOptions options = new IamOptions.Builder()
26-
.apiKey("<iam_api_key>")
27-
.build();
28-
service.setIamCredentials(options);
24+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
25+
PersonalityInsights service = new PersonalityInsights("2016-10-19", authenticator);
2926

3027
// Demo content from Moby Dick by Hermann Melville (Chapter 1)
3128
String text = "Call me Ishmael. Some years ago-never mind how long precisely-having "

speech-to-text/README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
Use the [Speech to Text][speech_to_text] service to recognize the text from a .wav file.
2121

2222
```java
23-
SpeechToText service = new SpeechToText();
24-
IamOptions options = new IamOptions.Builder()
25-
.apiKey("<iam_api_key>")
26-
.build();
27-
service.setIamCredentials(options);
23+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
24+
SpeechToText service = new SpeechToText(authenticator);
2825

2926
File audio = new File("src/test/resources/sample1.wav");
3027

@@ -42,11 +39,8 @@ System.out.println(transcript);
4239
Speech to Text supports WebSocket, the url is: `wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize`
4340

4441
```java
45-
SpeechToText service = new SpeechToText();
46-
IamOptions options = new IamOptions.Builder()
47-
.apiKey("<iam_api_key>")
48-
.build();
49-
service.setIamCredentials(options);
42+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
43+
SpeechToText service = new SpeechToText(authenticator);
5044

5145
InputStream audio = new FileInputStream("src/test/resources/sample1.wav");
5246

text-to-speech/README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
Use the [Text to Speech][text_to_speech] service to get the available voices to synthesize.
2121

2222
```java
23-
TextToSpeech service = new TextToSpeech();
24-
IamOptions options = new IamOptions.Builder()
25-
.apiKey("<iam_api_key>")
26-
.build();
27-
service.setIamCredentials(options);
23+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
24+
TextToSpeech service = new TextToSpeech(authenticator);
2825

2926
Voices voices = service.listVoices().execute().getResult();
3027
System.out.println(voices);
@@ -33,11 +30,8 @@ System.out.println(voices);
3330
## Usage with WebSockets
3431
The Watson Text to Speech service supports the use of WebSockets as an alternative to the `synthesize()` method, which converts text to speech. Here is an example of using the WebSocket version of the method to get an audio file:
3532
```java
36-
TextToSpeech service = new TextToSpeech();
37-
IamOptions options = new IamOptions.Builder()
38-
.apiKey("<iam_api_key>")
39-
.build();
40-
service.setIamCredentials(options);
33+
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
34+
TextToSpeech service = new TextToSpeech(authenticator);
4135

4236
String text = "It's beginning to look a lot like Christmas";
4337
SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder()

0 commit comments

Comments
 (0)