Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ gradle codeCoverageReport # run tests and generate the code coverage report (bui

If you want to contribute to the repository, here's a quick guide:
1. Fork the repository
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).
1. Edit the [`config.properties`](../common/src/test/resources/config.properties) file to add your service credentials to the appropriate fields.
2. develop and test your code changes, gradle: `gradle test`.
* Run `checkstyle`: `gradle checkstyle`. 🏁
* 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.
Expand Down
19 changes: 5 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ cache:
- "$HOME/.m2"
- "$HOME/.gradle/caches/"
- "$HOME/.gradle/wrapper/"
env:
global:
- BINTRAY_REPO_OWNER=ibm-cloud-sdks
- BINTRAY_REPO_NAME=ibm-cloud-sdk-repo
- BINTRAY_PKG_NAME=com.ibm.watson:ibm-watson
before_install:
- |
if [ "$TRAVIS_JDK_VERSION" == "openjdk7" ]; then
Expand Down Expand Up @@ -47,11 +42,11 @@ script:
&& tar xvf secrets.tar
|| true'
- if [ "${TRAVIS_TAG}" = "${TRAVIS_BRANCH}" ]; then ./appscan/ASOC.sh; fi
#- "./gradlew install -x check"
#- "./gradlew checkstyleMain"
#- "./gradlew checkstyleTest"
#- "./gradlew codeCoverageReport --continue"
#- "./gradlew docs > /dev/null"
- "./gradlew install -x check"
- "./gradlew checkstyleMain"
- "./gradlew checkstyleTest"
- "./gradlew codeCoverageReport --continue"
- "./gradlew docs > /dev/null"
after_success:
- bash <(curl -s https://codecov.io/bash)
deploy:
Expand All @@ -74,9 +69,5 @@ deploy:
repo: watson-developer-cloud/java-sdk
jdk: openjdk7
tags: true
# Ask bintray to sync the just-deployed artifact to maven central
after_script:
- '[ "${TRAVIS_TAG}" != "" ]
&& .utility/sync-to-maven-central.sh $BINTRAY_USER $BINTRAY_APIKEY $BINTRAY_REPO_OWNER $BINTRAY_REPO_NAME $BINTRAY_PKG_NAME $TRAVIS_TAG'
notifications:
email: true
81 changes: 34 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ Java client library to use the [Watson APIs][wdc].
* [Username and password](#username-and-password)
* [API key](#api-key)
* [Using the SDK](#using-the-sdk)
* [Parsing responses](#parsing-responses)
* [Configuring the HTTP client](#configuring-the-http-client)
* [Making asynchronous API calls](#making-asynchronous-api-calls)
* [Default headers](#default-headers)
* [Sending request headers](#sending-request-headers)
* [Parsing HTTP response info](#parsing-http-response-info)
* [FAQ](#faq)
* IBM Watson Services
* [Assistant](assistant)
Expand Down Expand Up @@ -62,19 +62,19 @@ All the services:

```xml
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>java-sdk</artifactId>
<version>6.14.0</version>
<groupId>com.ibm.watson</groupId>
<artifactId>ibm-watson</artifactId>
<version>7.0.0</version>
</dependency>
```

Only Discovery:

```xml
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<groupId>com.ibm.watson</groupId>
<artifactId>discovery</artifactId>
<version>6.14.0</version>
<version>7.0.0</version>
</dependency>
```

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

```gradle
'com.ibm.watson.developer_cloud:java-sdk:6.14.0'
'com.ibm.watson:ibm-watson:7.0.0'
```

Only Assistant:

```gradle
'com.ibm.watson.developer_cloud:assistant:6.14.0'
'com.ibm.watson:assistant:7.0.0'
```

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

**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.

### Getting credentials

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:
Expand Down Expand Up @@ -231,6 +229,25 @@ service.setUsernameAndPassword("<username>", "<password>");

## Using the SDK

### Parsing responses

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.

Here's an example of how to parse that response and get additional information beyond the response model:

```java
// listing our workspaces with an instance of the Assistant v1 service
Response<WorkspaceCollection> response = service.listWorkspaces().execute();

// pulling out the specific API method response, which we can manipulate as usual
WorkspaceCollection collection = response.getResult();
System.out.println("My workspaces: " + collection.getWorkspaces());

// grabbing headers that came back with our API response
Headers responseHeaders = response.getHeaders();
System.out.println("Response header names: " + responseHeaders.names());
```

### Configuring the HTTP client

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:
Expand All @@ -256,12 +273,12 @@ service.configureClient(options);
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:
```java
// make API call
ListEnvironmentsResponse response = service.listEnvironments().execute();
Response<ListEnvironmentsResponse> response = service.listEnvironments().execute();

// continue execution
```

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

#### `enqueue()`

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

Expand All @@ -288,7 +305,8 @@ service.listEnvironments().enqueue(new ServiceCallback<ListEnvironmentsResponse>
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:
```java
// get stream with request
Single<ListEnvironmentsResponse> observableRequest = service.listEnvironments().reactiveRequest();
Single<Response<ListEnvironmentsResponse>> observableRequest
= service.listEnvironments().reactiveRequest();

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

Map<String, String> headers = new HashMap<String, String>();
headers.put(HttpHeaders.X_WATSON_LEARNING_OPT_OUT, "true");
headers.put(WatsonHttpHeaders.X_WATSON_LEARNING_OPT_OUT, "true");

service.setDefaultHeaders(headers);

Expand All @@ -320,42 +338,11 @@ service.setDefaultHeaders(headers);
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:

```java
WorkspaceCollection workspaces = service.listWorkspaces()
Response<WorkspaceCollection> workspaces = service.listWorkspaces()
.addHeader("Custom-Header", "custom_value")
.execute();
```

### Parsing HTTP response info

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.

Here is an example of calling the Watson Assistant `listWorkspaces()` method and parsing its response model as well as the response headers:

```java
Response<WorkspaceCollection> response = service.listWorkspaces().executeWithDetails();

// getting result equivalent to execute()
WorkspaceCollection workspaces = response.getResult();

// getting returned HTTP headers
Headers responseHeaders = response.getHeaders();
```

Note that when using `enqueueWithDetails()`, you must also implement the new `ServiceCallbackWithDetails` interface. For example:

```java
service.listWorkspaces().enqueueWithDetails(new ServiceCallbackWithDetails<WorkspaceCollection>() {
@Override
public void onResponse(Response<WorkspaceCollection> response) {
WorkspaceCollection workspaces = response.getResult();
Headers responseHeaders = response.getHeaders();
}

@Override
public void onFailure(Exception e) { }
});
```

## FAQ

### Does this SDK play well with Android?
Expand Down
22 changes: 14 additions & 8 deletions assistant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ Use the [Assistant][assistant] service to identify intents, entities, and conduc
import com.ibm.watson.developer_cloud.assistant.v1.Assistant;

Assistant service = new Assistant("2018-02-16");
service.setUsernameAndPassword("<username>", "<password>");
IamOptions options = new IamOptions.Builder()
.apiKey("<iam_api_key>")
.build();
service.setIamCredentials(options);

InputData input = new InputData.Builder("Hi").build();
MessageOptions options = new MessageOptions.Builder(workspaceId)
.input(input)
.build();
MessageResponse response = service.message(options).execute();
MessageResponse response = service.message(options).execute().getResult();
System.out.println(response);
```

Expand All @@ -49,7 +52,7 @@ MessageOptions newMessageOptions = new MessageOptions.Builder()
.context(context)
.build();

MessageResponse response = service.message(newMessageOptions).execute();
MessageResponse response = service.message(newMessageOptions).execute().getResult();
context = response.getContext();

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

response = service.message(newMessageOptions).execute();
response = service.message(newMessageOptions).execute().getResult();

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

Assistant service = new Assistant("2018-09-20");
service.setUsernameAndPassword("<username>", "<password>");
IamOptions options = new IamOptions.Builder()
.apiKey("<iam_api_key>")
.build();
service.setIamCredentials(options);

MessageInput input = new MessageInput.Builder()
.text("Hi")
Expand All @@ -82,7 +88,7 @@ MessageOptions messageOptions = new MessageOptions.Builder()
.sessionId("<session_id>")
.input(input)
.build();
MessageResponse messageResponse = service.message(messageOptions).execute();
MessageResponse messageResponse = service.message(messageOptions).execute().getResult();

System.out.println(messageResponse);
```
Expand All @@ -105,7 +111,7 @@ MessageOptions messageOptions = new MessageOptions.Builder()
.context(context)
.build();

MessageResponse messageResponse = service.message(messageOptions).execute();
MessageResponse messageResponse = service.message(messageOptions).execute().getResult();
context = messageResponse.getContext();

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

messageResponse = service.message(messageOptions).execute();
messageResponse = service.message(messageOptions).execute().getResult();

System.out.println(messageResponse);
```
Expand Down
2 changes: 1 addition & 1 deletion assistant/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ checkstyle {
dependencies {
compile project(':common')
testCompile project(':common').sourceSets.test.output
compile 'com.ibm.cloud:sdk-core:2.0.0'
compile 'com.ibm.cloud:sdk-core:3.0.2'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
Loading