Skip to content

Replace reactive programming library #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 18, 2019
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
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<gson-version>2.8.5</gson-version>
<commons-io-version>2.6</commons-io-version>
<commons-lang3-version>3.8.1</commons-lang3-version>
<jersey-version>2.25.1</jersey-version>
<rx-version>2.2.7</rx-version>
</properties>

<!-- we use the travis providers for deployment so don't need to specify repositories here -->
Expand Down Expand Up @@ -81,11 +81,6 @@
<version>${okhttp3-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.bundles.repackaged</groupId>
<artifactId>jersey-jsr166e</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down Expand Up @@ -134,6 +129,11 @@
<artifactId>gson</artifactId>
<version>${gson-version}</version>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>${rx-version}</version>
</dependency>
</dependencies>

<build>
Expand Down
40 changes: 9 additions & 31 deletions src/main/java/com/ibm/cloud/sdk/core/http/ServiceCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package com.ibm.cloud.sdk.core.http;

import jersey.repackaged.jsr166e.CompletableFuture;
import io.reactivex.Single;

/**
* Service Call.
Expand All @@ -33,46 +33,24 @@ public interface ServiceCall<T> {
/**
* Synchronous request.
*
* @return the generic type
* @throws RuntimeException the exception from HTTP request
*/
T execute() throws RuntimeException;

/**
* Synchronous request with added HTTP information.
*
* @return a Response object with the generic response model and various HTTP information fields
* @throws RuntimeException the exception from the HTTP request
*/
Response<T> executeWithDetails() throws RuntimeException;

/**
* Asynchronous requests, in this case, you receive a callback when the data has been received.
*
* @param callback the callback
* @throws RuntimeException the exception from HTTP request
*/
void enqueue(ServiceCallback<? super T> callback);
Response<T> execute() throws RuntimeException;

/**
* Asynchronous requests with added HTTP information. In this case, you receive a callback when the data has been
* Asynchronous request with added HTTP information. In this case, you receive a callback when the data has been
* received.
*
* @param callback the callback
*/
void enqueueWithDetails(ServiceCallbackWithDetails<T> callback);

/**
* Reactive requests, in this case, you could take advantage both synchronous and asynchronous.
*
* @return a CompletableFuture wrapper for your response
*/
CompletableFuture<T> rx();
void enqueue(ServiceCallback<? super T> callback);

/**
* Reactive requests with added HTTP information. In this case, you could take advantage both synchronous and
* asynchronous.
* Reactive request using the RxJava 2 library. See https://github.com/ReactiveX/RxJava. In addition, the wrapped
* service call will contain added HTTP information.
*
* @return a CompletableFuture wrapper for your response
* @return a Single object containing the service call to be observed/subscribed to
*/
CompletableFuture<Response<T>> rxWithDetails();
Single<Response<T>> reactiveRequest();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface ServiceCallback<T> {
*
* @param response the response
*/
void onResponse(T response);
void onResponse(Response<T> response);

/**
* Called if there is an error during the request.
Expand Down

This file was deleted.

87 changes: 11 additions & 76 deletions src/main/java/com/ibm/cloud/sdk/core/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.ibm.cloud.sdk.core.http.ResponseConverter;
import com.ibm.cloud.sdk.core.http.ServiceCall;
import com.ibm.cloud.sdk.core.http.ServiceCallback;
import com.ibm.cloud.sdk.core.http.ServiceCallbackWithDetails;
import com.ibm.cloud.sdk.core.service.exception.BadRequestException;
import com.ibm.cloud.sdk.core.service.exception.ConflictException;
import com.ibm.cloud.sdk.core.service.exception.ForbiddenException;
Expand All @@ -35,7 +34,7 @@
import com.ibm.cloud.sdk.core.service.security.IamTokenManager;
import com.ibm.cloud.sdk.core.util.CredentialUtils;
import com.ibm.cloud.sdk.core.util.RequestUtils;
import jersey.repackaged.jsr166e.CompletableFuture;
import io.reactivex.Single;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Credentials;
Expand All @@ -47,6 +46,7 @@

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.logging.Logger;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -479,47 +479,18 @@ public ServiceCall<T> addHeader(String name, String value) {
}

@Override
public T execute() {
public com.ibm.cloud.sdk.core.http.Response<T> execute() {
try {
Response response = call.execute();
return processServiceCall(converter, response);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public com.ibm.cloud.sdk.core.http.Response<T> executeWithDetails() throws RuntimeException {
try {
Response httpResponse = call.execute();
T responseModel = processServiceCall(converter, httpResponse);
return new com.ibm.cloud.sdk.core.http.Response<>(responseModel, httpResponse);
T responseModel = processServiceCall(converter, response);
return new com.ibm.cloud.sdk.core.http.Response<>(responseModel, 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 void enqueueWithDetails(final ServiceCallbackWithDetails<T> callback) {
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Expand All @@ -539,51 +510,15 @@ public void onResponse(Call call, Response response) {
}

@Override
public CompletableFuture<T> rx() {
final CompletableFuture<T> completableFuture = new CompletableFuture<T>();

call.enqueue(new Callback() {
public Single<com.ibm.cloud.sdk.core.http.Response<T>> reactiveRequest() {
return Single.fromCallable(new Callable<com.ibm.cloud.sdk.core.http.Response<T>>() {
@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
public CompletableFuture<com.ibm.cloud.sdk.core.http.Response<T>> rxWithDetails() {
final CompletableFuture<com.ibm.cloud.sdk.core.http.Response<T>> completableFuture
= new CompletableFuture<>();

call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
completableFuture.completeExceptionally(e);
}

@Override
public void onResponse(Call call, Response response) {
try {
T responseModel = processServiceCall(converter, response);
completableFuture.complete(new com.ibm.cloud.sdk.core.http.Response<>(responseModel, response));
} catch (Exception e) {
completableFuture.completeExceptionally(e);
}
public com.ibm.cloud.sdk.core.http.Response<T> call() throws Exception {
Response response = call.execute();
T responseModel = processServiceCall(converter, response);
return new com.ibm.cloud.sdk.core.http.Response<>(responseModel, response);
}
});

return completableFuture;
}

@Override
Expand Down
Loading