Skip to content

Commit b6a410a

Browse files
authored
Merge pull request #1047 from watson-developer-cloud/update-tests-and-examples
Update tests and examples
2 parents 3ea3509 + 43cdb76 commit b6a410a

86 files changed

Lines changed: 1242 additions & 1429 deletions

File tree

Some content is hidden

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

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ gradle codeCoverageReport # run tests and generate the code coverage report (bui
1818

1919
If you want to contribute to the repository, here's a quick guide:
2020
1. Fork the repository
21-
1. Create a `.config.properties` similar to [`config.properties`](https://github.com/watson-developer-cloud/java-sdk/blob/master/core/src/test/resources/config.properties).
21+
1. Edit the [`config.properties`](../common/src/test/resources/config.properties) file to add your service credentials to the appropriate fields.
2222
2. develop and test your code changes, gradle: `gradle test`.
2323
* Run `checkstyle`: `gradle checkstyle`. 🏁
2424
* Create minimal diffs - disable on save actions like reformat source code or organize imports. If you feel the source code should be reformatted create a separate PR for this change.

.travis.yml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ cache:
1515
- "$HOME/.m2"
1616
- "$HOME/.gradle/caches/"
1717
- "$HOME/.gradle/wrapper/"
18-
env:
19-
global:
20-
- BINTRAY_REPO_OWNER=ibm-cloud-sdks
21-
- BINTRAY_REPO_NAME=ibm-cloud-sdk-repo
22-
- BINTRAY_PKG_NAME=com.ibm.watson:ibm-watson
2318
before_install:
2419
- |
2520
if [ "$TRAVIS_JDK_VERSION" == "openjdk7" ]; then
@@ -47,11 +42,11 @@ script:
4742
&& tar xvf secrets.tar
4843
|| true'
4944
- if [ "${TRAVIS_TAG}" = "${TRAVIS_BRANCH}" ]; then ./appscan/ASOC.sh; fi
50-
#- "./gradlew install -x check"
51-
#- "./gradlew checkstyleMain"
52-
#- "./gradlew checkstyleTest"
53-
#- "./gradlew codeCoverageReport --continue"
54-
#- "./gradlew docs > /dev/null"
45+
- "./gradlew install -x check"
46+
- "./gradlew checkstyleMain"
47+
- "./gradlew checkstyleTest"
48+
- "./gradlew codeCoverageReport --continue"
49+
- "./gradlew docs > /dev/null"
5550
after_success:
5651
- bash <(curl -s https://codecov.io/bash)
5752
deploy:
@@ -74,9 +69,5 @@ deploy:
7469
repo: watson-developer-cloud/java-sdk
7570
jdk: openjdk7
7671
tags: true
77-
# Ask bintray to sync the just-deployed artifact to maven central
78-
after_script:
79-
- '[ "${TRAVIS_TAG}" != "" ]
80-
&& .utility/sync-to-maven-central.sh $BINTRAY_USER $BINTRAY_APIKEY $BINTRAY_REPO_OWNER $BINTRAY_REPO_NAME $BINTRAY_PKG_NAME $TRAVIS_TAG'
8172
notifications:
8273
email: true

README.md

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ Java client library to use the [Watson APIs][wdc].
2121
* [Username and password](#username-and-password)
2222
* [API key](#api-key)
2323
* [Using the SDK](#using-the-sdk)
24+
* [Parsing responses](#parsing-responses)
2425
* [Configuring the HTTP client](#configuring-the-http-client)
2526
* [Making asynchronous API calls](#making-asynchronous-api-calls)
2627
* [Default headers](#default-headers)
2728
* [Sending request headers](#sending-request-headers)
28-
* [Parsing HTTP response info](#parsing-http-response-info)
2929
* [FAQ](#faq)
3030
* IBM Watson Services
3131
* [Assistant](assistant)
@@ -62,19 +62,19 @@ All the services:
6262

6363
```xml
6464
<dependency>
65-
<groupId>com.ibm.watson.developer_cloud</groupId>
66-
<artifactId>java-sdk</artifactId>
67-
<version>6.14.0</version>
65+
<groupId>com.ibm.watson</groupId>
66+
<artifactId>ibm-watson</artifactId>
67+
<version>7.0.0</version>
6868
</dependency>
6969
```
7070

7171
Only Discovery:
7272

7373
```xml
7474
<dependency>
75-
<groupId>com.ibm.watson.developer_cloud</groupId>
75+
<groupId>com.ibm.watson</groupId>
7676
<artifactId>discovery</artifactId>
77-
<version>6.14.0</version>
77+
<version>7.0.0</version>
7878
</dependency>
7979
```
8080

@@ -83,13 +83,13 @@ Only Discovery:
8383
All the services:
8484

8585
```gradle
86-
'com.ibm.watson.developer_cloud:java-sdk:6.14.0'
86+
'com.ibm.watson:ibm-watson:7.0.0'
8787
```
8888

8989
Only Assistant:
9090

9191
```gradle
92-
'com.ibm.watson.developer_cloud:assistant:6.14.0'
92+
'com.ibm.watson:assistant:7.0.0'
9393
```
9494

9595
##### JAR
@@ -118,8 +118,6 @@ Watson services are migrating to token-based Identity and Access Management (IAM
118118
- With some service instances, you authenticate to the API by using **[IAM](#iam)**.
119119
- In other instances, you authenticate by providing the **[username and password](#username-and-password)** for the service instance.
120120

121-
**Note:** Previously, it was possible to authenticate using a token in a header called `X-Watson-Authorization-Token`. This method is deprecated. The token continues to work with Cloud Foundry services, but is not supported for services that use Identity and Access Management (IAM) authentication. See [here](#iam) for details.
122-
123121
### Getting credentials
124122

125123
To find out which authentication to use, view the service credentials. You find the service credentials for authentication the same way for all Watson services:
@@ -231,6 +229,25 @@ service.setUsernameAndPassword("<username>", "<password>");
231229

232230
## Using the SDK
233231

232+
### Parsing responses
233+
234+
No matter which method you use to make an API request (`execute()`, `enqueue()`, or `reactiveRequest()`), you'll get back an object of form `Response<T>`, where `T` is the model representing the specific response model.
235+
236+
Here's an example of how to parse that response and get additional information beyond the response model:
237+
238+
```java
239+
// listing our workspaces with an instance of the Assistant v1 service
240+
Response<WorkspaceCollection> response = service.listWorkspaces().execute();
241+
242+
// pulling out the specific API method response, which we can manipulate as usual
243+
WorkspaceCollection collection = response.getResult();
244+
System.out.println("My workspaces: " + collection.getWorkspaces());
245+
246+
// grabbing headers that came back with our API response
247+
Headers responseHeaders = response.getHeaders();
248+
System.out.println("Response header names: " + responseHeaders.names());
249+
```
250+
234251
### Configuring the HTTP client
235252

236253
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:
@@ -256,12 +273,12 @@ service.configureClient(options);
256273
The basic, synchronous way to make API calls with this SDK is through the use of the `execute()` method. Using this method looks something like this:
257274
```java
258275
// make API call
259-
ListEnvironmentsResponse response = service.listEnvironments().execute();
276+
Response<ListEnvironmentsResponse> response = service.listEnvironments().execute();
260277

261278
// continue execution
262279
```
263280

264-
However, if you need to perform these calls in the background, there are two other main methods to do this asynchronously: `enqueue()` and `reactiveRequest()`.
281+
However, if you need to perform these calls in the background, there are two other methods to do this asynchronously: `enqueue()` and `reactiveRequest()`.
265282

266283
#### `enqueue()`
267284

@@ -270,7 +287,7 @@ This method allows you to set a callback for the service response through the us
270287
// make API call in the background
271288
service.listEnvironments().enqueue(new ServiceCallback<ListEnvironmentsResponse>() {
272289
@Override
273-
public void onResponse(ListEnvironmentsResponse response) {
290+
public void onResponse(Response<ListEnvironmentsResponse> response) {
274291
System.out.println("We did it! " + response);
275292
}
276293

@@ -288,7 +305,8 @@ service.listEnvironments().enqueue(new ServiceCallback<ListEnvironmentsResponse>
288305
If you're a fan of the [RxJava](https://github.com/ReactiveX/RxJava) library, this method lets you leverage that to allow for "reactive" programming. The method will return a `Single<T>` which you can manipulate how you please. Example:
289306
```java
290307
// get stream with request
291-
Single<ListEnvironmentsResponse> observableRequest = service.listEnvironments().reactiveRequest();
308+
Single<Response<ListEnvironmentsResponse>> observableRequest
309+
= service.listEnvironments().reactiveRequest();
292310

293311
// make API call in the background
294312
observableRequest
@@ -308,7 +326,7 @@ The example below sends the `X-Watson-Learning-Opt-Out` header in every request
308326
PersonalityInsights service = new PersonalityInsights("2016-10-19");
309327

310328
Map<String, String> headers = new HashMap<String, String>();
311-
headers.put(HttpHeaders.X_WATSON_LEARNING_OPT_OUT, "true");
329+
headers.put(WatsonHttpHeaders.X_WATSON_LEARNING_OPT_OUT, "true");
312330

313331
service.setDefaultHeaders(headers);
314332

@@ -320,42 +338,11 @@ service.setDefaultHeaders(headers);
320338
Custom headers can be passed with any request. To do so, add the header to the `ServiceCall` object before executing the request. For example, this is what it looks like to send the header `Custom-Header` along with a call to the Watson Assistant service:
321339

322340
```java
323-
WorkspaceCollection workspaces = service.listWorkspaces()
341+
Response<WorkspaceCollection> workspaces = service.listWorkspaces()
324342
.addHeader("Custom-Header", "custom_value")
325343
.execute();
326344
```
327345

328-
### Parsing HTTP response info
329-
330-
The basic `execute()`, `enqueue()`, and `rx()` methods make HTTP requests to your Watson service and return models based on the requested endpoint. If you would like access to some HTTP response information along with the response model, you can use the more detailed versions of those three methods: `executeWithDetails()`, `enqueueWithDetails()`, and `rxWithDetails()`. To capture the responses, use the new `Response<T>` class, with `T` being the expected response model.
331-
332-
Here is an example of calling the Watson Assistant `listWorkspaces()` method and parsing its response model as well as the response headers:
333-
334-
```java
335-
Response<WorkspaceCollection> response = service.listWorkspaces().executeWithDetails();
336-
337-
// getting result equivalent to execute()
338-
WorkspaceCollection workspaces = response.getResult();
339-
340-
// getting returned HTTP headers
341-
Headers responseHeaders = response.getHeaders();
342-
```
343-
344-
Note that when using `enqueueWithDetails()`, you must also implement the new `ServiceCallbackWithDetails` interface. For example:
345-
346-
```java
347-
service.listWorkspaces().enqueueWithDetails(new ServiceCallbackWithDetails<WorkspaceCollection>() {
348-
@Override
349-
public void onResponse(Response<WorkspaceCollection> response) {
350-
WorkspaceCollection workspaces = response.getResult();
351-
Headers responseHeaders = response.getHeaders();
352-
}
353-
354-
@Override
355-
public void onFailure(Exception e) { }
356-
});
357-
```
358-
359346
## FAQ
360347

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

assistant/README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ Use the [Assistant][assistant] service to identify intents, entities, and conduc
2525
import com.ibm.watson.developer_cloud.assistant.v1.Assistant;
2626

2727
Assistant service = new Assistant("2018-02-16");
28-
service.setUsernameAndPassword("<username>", "<password>");
28+
IamOptions options = new IamOptions.Builder()
29+
.apiKey("<iam_api_key>")
30+
.build();
31+
service.setIamCredentials(options);
2932

3033
InputData input = new InputData.Builder("Hi").build();
3134
MessageOptions options = new MessageOptions.Builder(workspaceId)
3235
.input(input)
3336
.build();
34-
MessageResponse response = service.message(options).execute();
37+
MessageResponse response = service.message(options).execute().getResult();
3538
System.out.println(response);
3639
```
3740

@@ -49,7 +52,7 @@ MessageOptions newMessageOptions = new MessageOptions.Builder()
4952
.context(context)
5053
.build();
5154

52-
MessageResponse response = service.message(newMessageOptions).execute();
55+
MessageResponse response = service.message(newMessageOptions).execute().getResult();
5356
context = response.getContext();
5457

5558
// second message
@@ -59,7 +62,7 @@ newMessageOptions = new MessageOptions.Builder()
5962
.context(context) // using context from the first message
6063
.build();
6164

62-
response = service.message(newMessageOptions).execute();
65+
response = service.message(newMessageOptions).execute().getResult();
6366

6467
System.out.println(response);
6568
```
@@ -72,7 +75,10 @@ System.out.println(response);
7275
import com.ibm.watson.developer_cloud.assistant.v2.Assistant;
7376

7477
Assistant service = new Assistant("2018-09-20");
75-
service.setUsernameAndPassword("<username>", "<password>");
78+
IamOptions options = new IamOptions.Builder()
79+
.apiKey("<iam_api_key>")
80+
.build();
81+
service.setIamCredentials(options);
7682

7783
MessageInput input = new MessageInput.Builder()
7884
.text("Hi")
@@ -82,7 +88,7 @@ MessageOptions messageOptions = new MessageOptions.Builder()
8288
.sessionId("<session_id>")
8389
.input(input)
8490
.build();
85-
MessageResponse messageResponse = service.message(messageOptions).execute();
91+
MessageResponse messageResponse = service.message(messageOptions).execute().getResult();
8692

8793
System.out.println(messageResponse);
8894
```
@@ -105,7 +111,7 @@ MessageOptions messageOptions = new MessageOptions.Builder()
105111
.context(context)
106112
.build();
107113

108-
MessageResponse messageResponse = service.message(messageOptions).execute();
114+
MessageResponse messageResponse = service.message(messageOptions).execute().getResult();
109115
context = messageResponse.getContext();
110116

111117
// second message
@@ -119,7 +125,7 @@ messageOptions = new MessageOptions.Builder()
119125
.context(context) // using context from first message
120126
.build();
121127

122-
messageResponse = service.message(messageOptions).execute();
128+
messageResponse = service.message(messageOptions).execute().getResult();
123129

124130
System.out.println(messageResponse);
125131
```

assistant/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ checkstyle {
5757
dependencies {
5858
compile project(':common')
5959
testCompile project(':common').sourceSets.test.output
60-
compile 'com.ibm.cloud:sdk-core:2.0.0'
60+
compile 'com.ibm.cloud:sdk-core:3.0.2'
6161
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
6262
}
6363

0 commit comments

Comments
 (0)