Search in sources :

Example 1 with ServiceCall

use of com.ibm.watson.developer_cloud.http.ServiceCall 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

ServiceCall (com.ibm.watson.developer_cloud.http.ServiceCall)1 ServiceCallback (com.ibm.watson.developer_cloud.http.ServiceCallback)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 IOException (java.io.IOException)1 CompletableFuture (jersey.repackaged.jsr166e.CompletableFuture)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1