Skip to content

Commit fca9947

Browse files
Merge pull request #918 from watson-developer-cloud/develop
Add changes for v5.3.0 release
2 parents b1513c7 + bbf16cc commit fca9947

34 files changed

+1475
-169
lines changed

README.md

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Java client library to use the [Watson APIs][wdc].
1414
* [Maven](#maven)
1515
* [Gradle](#gradle)
1616
* [Usage](#usage)
17+
* [Running in IBM Cloud](#running-in-ibm-cloud)
1718
* [Getting the Service Credentials](#getting-the-service-credentials)
1819
* IBM Watson Services
1920
* [Assistant](assistant)
@@ -27,11 +28,14 @@ Java client library to use the [Watson APIs][wdc].
2728
* [Tone Analyzer](tone-analyzer)
2829
* [Tradeoff Analytics](tradeoff-analytics)
2930
* [Visual Recognition](visual-recognition)
30-
* [Changes for v4.0](#changes-for-v40)
31-
* [Using a Proxy](#using-a-proxy)
3231
* [Android](#android)
33-
* [Running in IBM Cloud](#running-in-ibm-cloud)
32+
* [Using a Proxy](#using-a-proxy)
3433
* [Default Headers](#default-headers)
34+
* [Sending Request Headers](#sending-request-headers)
35+
* [Parsing HTTP Response Info](#parsing-http-response-info)
36+
* [Specifying a Service URL](#specifying-a-service-url)
37+
* [401 Unauthorized Error](#401-unauthorized-error)
38+
* [Changes for v4.0](#changes-for-v40)
3539
* [Debug](#debug)
3640
* [Eclipse and Intellij](#working-with-eclipse-and-intellij-idea)
3741
* [License](#license)
@@ -114,6 +118,11 @@ you will have to create a service in [IBM Cloud][ibm_cloud].
114118
If you are running your application in IBM Cloud (or other platforms based on Cloud Foundry), you don't need to specify the
115119
credentials; the library will get them for you by looking at the [`VCAP_SERVICES`][vcap_services] environment variable.
116120

121+
## Running in IBM Cloud
122+
123+
When running in IBM Cloud (or other platforms based on Cloud Foundry), the library will automatically get the credentials from [`VCAP_SERVICES`][vcap_services].
124+
If you have more than one plan, you can use `CredentialUtils` to get the service credentials for an specific plan.
125+
117126
## Getting the Service Credentials
118127

119128
You will need the `username` and `password` (`api_key` for Visual Recognition) credentials, and the API endpoint for each service. Service credentials are different from your IBM Cloud account username and password.
@@ -126,12 +135,6 @@ To get your service credentials, follow these steps:
126135
1. On the left side of the page, click **Service Credentials**, and then **View credentials** to view your service credentials.
127136
1. Copy `url`, `username` and `password`(`api_key` for AlchemyAPI or Visual Recognition).
128137

129-
## Changes for v4.0
130-
Version 4.0 focuses on the move to programmatically-generated code for many of the services. See the [changelog](https://github.com/watson-developer-cloud/java-sdk/wiki/Changelog) for the details.
131-
132-
## Migration
133-
This version includes many breaking changes as a result of standardizing behavior across the new generated services. Full details on migration from previous versions can be found [here](https://github.com/watson-developer-cloud/java-sdk/wiki/Migration).
134-
135138
## Android
136139

137140
The Android SDK utilizes the Java SDK while making some Android-specific additions. This repository can be found [here](https://github.com/watson-developer-cloud/android-sdk). It depends on [OkHttp][] and [gson][].
@@ -159,17 +162,53 @@ System.out.println(workspaces);
159162

160163
For more information see: [OkHTTPClient Proxy authentication how to?](https://stackoverflow.com/a/35567936/456564)
161164

162-
## Running in IBM Cloud
163-
164-
When running in IBM Cloud (or other platforms based on Cloud Foundry), the library will automatically get the credentials from [`VCAP_SERVICES`][vcap_services].
165-
If you have more than one plan, you can use `CredentialUtils` to get the service credentials for an specific plan.
166-
167165
```java
168166
PersonalityInsights service = new PersonalityInsights("2016-10-19");
169167
String apiKey = CredentialUtils.getAPIKey(service.getName(), CredentialUtils.PLAN_STANDARD);
170168
service.setApiKey(apiKey);
171169
```
172170

171+
## Sending Request Headers
172+
173+
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:
174+
175+
```java
176+
WorkspaceCollection workspaces = service.listWorkspaces()
177+
.addHeader("Custom-Header", "custom_value")
178+
.execute();
179+
```
180+
181+
## Parsing HTTP Response Info
182+
183+
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.
184+
185+
Here is an example of calling the Watson Assistant `listWorkspaces()` method and parsing its response model as well as the response headers:
186+
187+
```java
188+
Response<WorkspaceCollection> response = service.listWorkspaces().executeWithDetails();
189+
190+
// getting result equivalent to execute()
191+
WorkspaceCollection workspaces = response.getResult();
192+
193+
// getting returned HTTP headers
194+
Headers responseHeaders = response.getHeaders();
195+
```
196+
197+
Note that when using `enqueueWithDetails()`, you must also implement the new `ServiceCallbackWithDetails` interface. For example:
198+
199+
```java
200+
service.listWorkspaces().enqueueWithDetails(new ServiceCallbackWithDetails<WorkspaceCollection>() {
201+
@Override
202+
public void onResponse(Response<WorkspaceCollection> response) {
203+
WorkspaceCollection workspaces = response.getResult();
204+
Headers responseHeaders = response.getHeaders();
205+
}
206+
207+
@Override
208+
public void onFailure(Exception e) { }
209+
});
210+
```
211+
173212
## Default Headers
174213

175214
Default headers can be specified at any time by using the `setDefaultHeaders(Map<String, String> headers)` method.
@@ -187,27 +226,30 @@ service.setDefaultHeaders(headers);
187226
// All the api calls from now on will send the default headers
188227
```
189228

190-
## Specifying a service URL
229+
## Specifying a Service URL
191230

192231
You can set the correct API Endpoint for your service calling `setEndPoint()`.
193232

194-
For example, if you have the conversation service in Germany, the Endpoint may be `https://gateway-fra.watsonplatform.net/conversation/api`.
233+
For example, if you have the Discovery service in Germany, the Endpoint may be `https://gateway-fra.watsonplatform.net/discovery/api`.
195234

196235
You will need to call
197236

198237
```java
199-
Assistant service = new Assistant("2018-02-16");
200-
service.sentEndPoint("https://gateway-fra.watsonplatform.net/conversation/api")
238+
Discovery service = new Discovery("2017-11-07");
239+
service.sentEndPoint("https://gateway-fra.watsonplatform.net/discovery/api")
201240
```
202241

203-
## 401 Unauthorized error
242+
## 401 Unauthorized Error
204243

205244
Make sure you are using the service credentials and not your IBM Cloud account/password.
206245
Check the API Endpoint, you may need to update the default using `setEndPoint()`.
207246

247+
## Changes for v4.0
248+
Version 4.0 focuses on the move to programmatically-generated code for many of the services. See the [changelog](https://github.com/watson-developer-cloud/java-sdk/wiki/Changelog) for the details. This version also includes many breaking changes as a result of standardizing behavior across the new generated services. Full details on migration from previous versions can be found [here](https://github.com/watson-developer-cloud/java-sdk/wiki/Migration).
249+
208250
## Debug
209251

210-
HTTP requests can be logging by adding a `loggging.properties` file to your classpath.
252+
HTTP requests can be logged by adding a `logging.properties` file to your classpath.
211253

212254
```none
213255
handlers=java.util.logging.ConsoleHandler
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.ibm.watson.developer_cloud.http;
2+
3+
import java.util.List;
4+
import java.util.Set;
5+
6+
/**
7+
* Wrapper class for the internal HTTP headers class.
8+
*/
9+
public class Headers {
10+
11+
private okhttp3.Headers headers;
12+
13+
public Headers(okhttp3.Headers headers) {
14+
this.headers = headers;
15+
}
16+
17+
/**
18+
* Returns true if other is a Headers object with the same headers, with the same casing, in the same order.
19+
*
20+
* @param other the other object to compare
21+
* @return whether the two objects are equal or not
22+
*/
23+
public boolean equals(Object other) {
24+
return this.headers.equals(other);
25+
}
26+
27+
public int hashCode() {
28+
return this.headers.hashCode();
29+
}
30+
31+
public String toString() {
32+
return this.headers.toString();
33+
}
34+
35+
/**
36+
* Returns an immutable, case-insensitive set of header names.
37+
*
38+
* @return the list of header names
39+
*/
40+
public Set<String> names() {
41+
return this.headers.names();
42+
}
43+
44+
/**
45+
* Returns an immutable list of the header values for the specified name.
46+
*
47+
* @param name the name of the specified header
48+
* @return the values associated with the name
49+
*/
50+
public List<String> values(String name) {
51+
return this.headers.values(name);
52+
}
53+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2018 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.ibm.watson.developer_cloud.http;
14+
15+
/**
16+
* Class holding the converted service call result along with some HTTP response data.
17+
*
18+
* @param <T> the generic type
19+
*/
20+
public class Response<T> {
21+
22+
private T result;
23+
private Headers headers;
24+
25+
public Response(T result, okhttp3.Response httpResponse) {
26+
this.result = result;
27+
this.headers = new Headers(httpResponse.headers());
28+
}
29+
30+
public T getResult() {
31+
return this.result;
32+
}
33+
34+
public Headers getHeaders() {
35+
return this.headers;
36+
}
37+
}

core/src/main/java/com/ibm/watson/developer_cloud/http/ServiceCall.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
*/
2222
public interface ServiceCall<T> {
2323

24+
/**
25+
* Add a header to the request before executing.
26+
*
27+
* @param name the name of the header
28+
* @param value the value of the header
29+
* @return the ServiceCall with updated headers
30+
*/
31+
ServiceCall<T> addHeader(String name, String value);
32+
2433
/**
2534
* Synchronous request.
2635
*
@@ -29,17 +38,41 @@ public interface ServiceCall<T> {
2938
*/
3039
T execute() throws RuntimeException;
3140

41+
/**
42+
* Synchronous request with added HTTP information.
43+
*
44+
* @return a Response object with the generic response model and various HTTP information fields
45+
* @throws RuntimeException the exception from the HTTP request
46+
*/
47+
Response<T> executeWithDetails() throws RuntimeException;
48+
3249
/**
3350
* Asynchronous requests, in this case, you receive a callback when the data has been received.
3451
*
3552
* @param callback the callback
3653
*/
3754
void enqueue(ServiceCallback<? super T> callback);
3855

56+
/**
57+
* Asynchronous requests with added HTTP information. In this case, you receive a callback when the data has been
58+
* received.
59+
*
60+
* @param callback the callback
61+
*/
62+
void enqueueWithDetails(ServiceCallbackWithDetails<T> callback);
63+
3964
/**
4065
* Reactive requests, in this case, you could take advantage both synchronous and asynchronous.
4166
*
4267
* @return a CompletableFuture wrapper for your response
4368
*/
4469
CompletableFuture<T> rx();
70+
71+
/**
72+
* Reactive requests with added HTTP information. In this case, you could take advantage both synchronous and
73+
* asynchronous.
74+
*
75+
* @return a CompletableFuture wrapper for your response
76+
*/
77+
CompletableFuture<Response<T>> rxWithDetails();
4578
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2018 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.ibm.watson.developer_cloud.http;
14+
15+
/**
16+
* Callback with the response for an Asynchronous request that also provides additional HTTP response information.
17+
*
18+
* @param <T> the generic type
19+
*/
20+
public interface ServiceCallbackWithDetails<T> {
21+
22+
/**
23+
* Called with the response.
24+
*
25+
* @param response the response
26+
*/
27+
void onResponse(Response<T> response);
28+
29+
/**
30+
* Called if there is an error during the request.
31+
*
32+
* @param e the exception thrown during the request
33+
*/
34+
void onFailure(Exception e);
35+
}

0 commit comments

Comments
 (0)