use of com.ibm.watson.developer_cloud.http.ServiceCallback in project java-sdk by watson-developer-cloud.
the class AssistantToneAnalyzerIntegrationExample method main.
public static void main(String[] args) throws Exception {
// instantiate the assistant service
Assistant assistantService = new Assistant("2018-02-16");
assistantService.setUsernameAndPassword("<username>", "<password>");
// instantiate the tone analyzer service
ToneAnalyzer toneService = new ToneAnalyzer("2017-09-21");
toneService.setUsernameAndPassword("<username>", "<password>");
// workspace id
String workspaceId = "<workspace-id>";
// maintain history in the context variable - will add a history variable to
// each of the emotion, social
// and language tones
boolean maintainHistory = false;
/**
* Input for the Assistant service: input (String): an input string (the user's conversation turn) and context
* (Map<String,Object>: any context that needs to be maintained - either added by the client app or passed in the
* response from the Assistant service on the previous conversation turn.
*/
String input = "I am happy";
Map<String, Object> context = new HashMap<>();
// UPDATE CONTEXT HERE IF CONTINUING AN ONGOING CONVERSATION
// set local context variable to the context from the last response from the
// Assistant Service
// (see the getContext() method of the MessageResponse class in
// com.ibm.watson.developer_cloud.assistant.v1.model)
// async call to Tone Analyzer
ToneOptions toneOptions = new ToneOptions.Builder().text(input).build();
toneService.tone(toneOptions).enqueue(new ServiceCallback<ToneAnalysis>() {
@Override
public void onResponse(ToneAnalysis toneResponsePayload) {
// update context with the tone data returned by the Tone Analyzer
ToneDetection.updateUserTone(context, toneResponsePayload, maintainHistory);
// call Assistant Service with the input and tone-aware context
MessageOptions messageOptions = new MessageOptions.Builder(workspaceId).input(new InputData.Builder(input).build()).context(context).build();
assistantService.message(messageOptions).enqueue(new ServiceCallback<MessageResponse>() {
@Override
public void onResponse(MessageResponse response) {
System.out.println(response);
}
@Override
public void onFailure(Exception e) {
}
});
}
@Override
public void onFailure(Exception e) {
}
});
}
use of com.ibm.watson.developer_cloud.http.ServiceCallback in project java-sdk by watson-developer-cloud.
the class WatsonService method createServiceCall.
/**
* Creates the service call.
*
* @param <T> the generic type
* @param request the request
* @param converter the converter
* @return the service call
*/
protected final <T> ServiceCall<T> createServiceCall(final Request request, final ResponseConverter<T> converter) {
final Call call = createCall(request);
return new ServiceCall<T>() {
@Override
public T execute() {
try {
Response response = call.execute();
return processServiceCall(converter, response);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void enqueue(final ServiceCallback<? super T> callback) {
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callback.onFailure(e);
}
@Override
public void onResponse(Call call, Response response) {
try {
callback.onResponse(processServiceCall(converter, response));
} catch (Exception e) {
callback.onFailure(e);
}
}
});
}
@Override
public CompletableFuture<T> rx() {
final CompletableFuture<T> completableFuture = new CompletableFuture<T>();
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
completableFuture.completeExceptionally(e);
}
@Override
public void onResponse(Call call, Response response) {
try {
completableFuture.complete(processServiceCall(converter, response));
} catch (Exception e) {
completableFuture.completeExceptionally(e);
}
}
});
return completableFuture;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
if (!call.isExecuted()) {
final Request r = call.request();
LOG.warning(r.method() + " request to " + r.url() + " has not been sent. Did you forget to call execute()?");
}
}
};
}
Aggregations