Skip to content

Commit c5a5ef5

Browse files
authored
Merge pull request #16 from IBM/new-async-lib
Replace reactive programming library
2 parents 111ea91 + afd601d commit c5a5ef5

File tree

6 files changed

+80
-208
lines changed

6 files changed

+80
-208
lines changed

pom.xml

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<gson-version>2.8.5</gson-version>
4949
<commons-io-version>2.6</commons-io-version>
5050
<commons-lang3-version>3.8.1</commons-lang3-version>
51-
<jersey-version>2.25.1</jersey-version>
51+
<rx-version>2.2.7</rx-version>
5252
</properties>
5353

5454
<!-- we use the travis providers for deployment so don't need to specify repositories here -->
@@ -81,11 +81,6 @@
8181
<version>${okhttp3-version}</version>
8282
<scope>test</scope>
8383
</dependency>
84-
<dependency>
85-
<groupId>org.glassfish.jersey.bundles.repackaged</groupId>
86-
<artifactId>jersey-jsr166e</artifactId>
87-
<version>${jersey-version}</version>
88-
</dependency>
8984
<dependency>
9085
<groupId>commons-io</groupId>
9186
<artifactId>commons-io</artifactId>
@@ -134,6 +129,11 @@
134129
<artifactId>gson</artifactId>
135130
<version>${gson-version}</version>
136131
</dependency>
132+
<dependency>
133+
<groupId>io.reactivex.rxjava2</groupId>
134+
<artifactId>rxjava</artifactId>
135+
<version>${rx-version}</version>
136+
</dependency>
137137
</dependencies>
138138

139139
<build>

src/main/java/com/ibm/cloud/sdk/core/http/ServiceCall.java

+9-31
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
package com.ibm.cloud.sdk.core.http;
1414

15-
import jersey.repackaged.jsr166e.CompletableFuture;
15+
import io.reactivex.Single;
1616

1717
/**
1818
* Service Call.
@@ -33,46 +33,24 @@ public interface ServiceCall<T> {
3333
/**
3434
* Synchronous request.
3535
*
36-
* @return the generic type
37-
* @throws RuntimeException the exception from HTTP request
38-
*/
39-
T execute() throws RuntimeException;
40-
41-
/**
42-
* Synchronous request with added HTTP information.
43-
*
4436
* @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-
49-
/**
50-
* Asynchronous requests, in this case, you receive a callback when the data has been received.
51-
*
52-
* @param callback the callback
37+
* @throws RuntimeException the exception from HTTP request
5338
*/
54-
void enqueue(ServiceCallback<? super T> callback);
39+
Response<T> execute() throws RuntimeException;
5540

5641
/**
57-
* Asynchronous requests with added HTTP information. In this case, you receive a callback when the data has been
42+
* Asynchronous request with added HTTP information. In this case, you receive a callback when the data has been
5843
* received.
5944
*
6045
* @param callback the callback
6146
*/
62-
void enqueueWithDetails(ServiceCallbackWithDetails<T> callback);
63-
64-
/**
65-
* Reactive requests, in this case, you could take advantage both synchronous and asynchronous.
66-
*
67-
* @return a CompletableFuture wrapper for your response
68-
*/
69-
CompletableFuture<T> rx();
47+
void enqueue(ServiceCallback<? super T> callback);
7048

7149
/**
72-
* Reactive requests with added HTTP information. In this case, you could take advantage both synchronous and
73-
* asynchronous.
50+
* Reactive request using the RxJava 2 library. See https://github.com/ReactiveX/RxJava. In addition, the wrapped
51+
* service call will contain added HTTP information.
7452
*
75-
* @return a CompletableFuture wrapper for your response
53+
* @return a Single object containing the service call to be observed/subscribed to
7654
*/
77-
CompletableFuture<Response<T>> rxWithDetails();
55+
Single<Response<T>> reactiveRequest();
7856
}

src/main/java/com/ibm/cloud/sdk/core/http/ServiceCallback.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface ServiceCallback<T> {
2424
*
2525
* @param response the response
2626
*/
27-
void onResponse(T response);
27+
void onResponse(Response<T> response);
2828

2929
/**
3030
* Called if there is an error during the request.

src/main/java/com/ibm/cloud/sdk/core/http/ServiceCallbackWithDetails.java

-35
This file was deleted.

src/main/java/com/ibm/cloud/sdk/core/service/BaseService.java

+11-76
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.ibm.cloud.sdk.core.http.ResponseConverter;
2020
import com.ibm.cloud.sdk.core.http.ServiceCall;
2121
import com.ibm.cloud.sdk.core.http.ServiceCallback;
22-
import com.ibm.cloud.sdk.core.http.ServiceCallbackWithDetails;
2322
import com.ibm.cloud.sdk.core.service.exception.BadRequestException;
2423
import com.ibm.cloud.sdk.core.service.exception.ConflictException;
2524
import com.ibm.cloud.sdk.core.service.exception.ForbiddenException;
@@ -35,7 +34,7 @@
3534
import com.ibm.cloud.sdk.core.service.security.IamTokenManager;
3635
import com.ibm.cloud.sdk.core.util.CredentialUtils;
3736
import com.ibm.cloud.sdk.core.util.RequestUtils;
38-
import jersey.repackaged.jsr166e.CompletableFuture;
37+
import io.reactivex.Single;
3938
import okhttp3.Call;
4039
import okhttp3.Callback;
4140
import okhttp3.Credentials;
@@ -47,6 +46,7 @@
4746

4847
import java.io.IOException;
4948
import java.util.Map;
49+
import java.util.concurrent.Callable;
5050
import java.util.logging.Logger;
5151
import java.util.regex.Pattern;
5252

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

481481
@Override
482-
public T execute() {
482+
public com.ibm.cloud.sdk.core.http.Response<T> execute() {
483483
try {
484484
Response response = call.execute();
485-
return processServiceCall(converter, response);
486-
} catch (IOException e) {
487-
throw new RuntimeException(e);
488-
}
489-
}
490-
491-
@Override
492-
public com.ibm.cloud.sdk.core.http.Response<T> executeWithDetails() throws RuntimeException {
493-
try {
494-
Response httpResponse = call.execute();
495-
T responseModel = processServiceCall(converter, httpResponse);
496-
return new com.ibm.cloud.sdk.core.http.Response<>(responseModel, httpResponse);
485+
T responseModel = processServiceCall(converter, response);
486+
return new com.ibm.cloud.sdk.core.http.Response<>(responseModel, response);
497487
} catch (IOException e) {
498488
throw new RuntimeException(e);
499489
}
500490
}
501491

502492
@Override
503493
public void enqueue(final ServiceCallback<? super T> callback) {
504-
call.enqueue(new Callback() {
505-
@Override
506-
public void onFailure(Call call, IOException e) {
507-
callback.onFailure(e);
508-
}
509-
510-
@Override
511-
public void onResponse(Call call, Response response) {
512-
try {
513-
callback.onResponse(processServiceCall(converter, response));
514-
} catch (Exception e) {
515-
callback.onFailure(e);
516-
}
517-
}
518-
});
519-
}
520-
521-
@Override
522-
public void enqueueWithDetails(final ServiceCallbackWithDetails<T> callback) {
523494
call.enqueue(new Callback() {
524495
@Override
525496
public void onFailure(Call call, IOException e) {
@@ -539,51 +510,15 @@ public void onResponse(Call call, Response response) {
539510
}
540511

541512
@Override
542-
public CompletableFuture<T> rx() {
543-
final CompletableFuture<T> completableFuture = new CompletableFuture<T>();
544-
545-
call.enqueue(new Callback() {
513+
public Single<com.ibm.cloud.sdk.core.http.Response<T>> reactiveRequest() {
514+
return Single.fromCallable(new Callable<com.ibm.cloud.sdk.core.http.Response<T>>() {
546515
@Override
547-
public void onFailure(Call call, IOException e) {
548-
completableFuture.completeExceptionally(e);
549-
}
550-
551-
@Override
552-
public void onResponse(Call call, Response response) {
553-
try {
554-
completableFuture.complete(processServiceCall(converter, response));
555-
} catch (Exception e) {
556-
completableFuture.completeExceptionally(e);
557-
}
558-
}
559-
});
560-
561-
return completableFuture;
562-
}
563-
564-
@Override
565-
public CompletableFuture<com.ibm.cloud.sdk.core.http.Response<T>> rxWithDetails() {
566-
final CompletableFuture<com.ibm.cloud.sdk.core.http.Response<T>> completableFuture
567-
= new CompletableFuture<>();
568-
569-
call.enqueue(new Callback() {
570-
@Override
571-
public void onFailure(Call call, IOException e) {
572-
completableFuture.completeExceptionally(e);
573-
}
574-
575-
@Override
576-
public void onResponse(Call call, Response response) {
577-
try {
578-
T responseModel = processServiceCall(converter, response);
579-
completableFuture.complete(new com.ibm.cloud.sdk.core.http.Response<>(responseModel, response));
580-
} catch (Exception e) {
581-
completableFuture.completeExceptionally(e);
582-
}
516+
public com.ibm.cloud.sdk.core.http.Response<T> call() throws Exception {
517+
Response response = call.execute();
518+
T responseModel = processServiceCall(converter, response);
519+
return new com.ibm.cloud.sdk.core.http.Response<>(responseModel, response);
583520
}
584521
});
585-
586-
return completableFuture;
587522
}
588523

589524
@Override

0 commit comments

Comments
 (0)