Search in sources :

Example 1 with ServiceCallback

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) {
        }
    });
}
Also used : ToneAnalysis(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis) HashMap(java.util.HashMap) MessageResponse(com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse) ServiceCallback(com.ibm.watson.developer_cloud.http.ServiceCallback) ToneAnalyzer(com.ibm.watson.developer_cloud.tone_analyzer.v3.ToneAnalyzer) MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) Assistant(com.ibm.watson.developer_cloud.assistant.v1.Assistant)

Example 2 with ServiceCallback

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()?");
            }
        }
    };
}
Also used : Call(okhttp3.Call) ServiceCall(com.ibm.watson.developer_cloud.http.ServiceCall) ServiceCall(com.ibm.watson.developer_cloud.http.ServiceCall) Request(okhttp3.Request) IOException(java.io.IOException) ForbiddenException(com.ibm.watson.developer_cloud.service.exception.ForbiddenException) BadRequestException(com.ibm.watson.developer_cloud.service.exception.BadRequestException) ServiceResponseException(com.ibm.watson.developer_cloud.service.exception.ServiceResponseException) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) RequestTooLargeException(com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException) TooManyRequestsException(com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) IOException(java.io.IOException) ConflictException(com.ibm.watson.developer_cloud.service.exception.ConflictException) UnsupportedException(com.ibm.watson.developer_cloud.service.exception.UnsupportedException) InternalServerErrorException(com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException) ServiceUnavailableException(com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException) Response(okhttp3.Response) CompletableFuture(jersey.repackaged.jsr166e.CompletableFuture) ServiceCallback(com.ibm.watson.developer_cloud.http.ServiceCallback) Callback(okhttp3.Callback) ServiceCallback(com.ibm.watson.developer_cloud.http.ServiceCallback)

Aggregations

ServiceCallback (com.ibm.watson.developer_cloud.http.ServiceCallback)2 Assistant (com.ibm.watson.developer_cloud.assistant.v1.Assistant)1 MessageOptions (com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions)1 MessageResponse (com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse)1 ServiceCall (com.ibm.watson.developer_cloud.http.ServiceCall)1 BadRequestException (com.ibm.watson.developer_cloud.service.exception.BadRequestException)1 ConflictException (com.ibm.watson.developer_cloud.service.exception.ConflictException)1 ForbiddenException (com.ibm.watson.developer_cloud.service.exception.ForbiddenException)1 InternalServerErrorException (com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException)1 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)1 RequestTooLargeException (com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException)1 ServiceResponseException (com.ibm.watson.developer_cloud.service.exception.ServiceResponseException)1 ServiceUnavailableException (com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException)1 TooManyRequestsException (com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException)1 UnauthorizedException (com.ibm.watson.developer_cloud.service.exception.UnauthorizedException)1 UnsupportedException (com.ibm.watson.developer_cloud.service.exception.UnsupportedException)1 ToneAnalyzer (com.ibm.watson.developer_cloud.tone_analyzer.v3.ToneAnalyzer)1 ToneAnalysis (com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1