mirror of https://github.com/dapr/java-sdk.git
Encapsulating async call in AbstractDaprClient as a single method. (#46)
This commit is contained in:
parent
ec5723b628
commit
1ab0287817
|
@ -1,11 +1,10 @@
|
||||||
package io.dapr.actors;
|
package io.dapr.actors;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
import reactor.core.scheduler.Schedulers;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
@ -69,37 +68,21 @@ public abstract class AbstractDaprClient {
|
||||||
* @return Asynchronous text
|
* @return Asynchronous text
|
||||||
*/
|
*/
|
||||||
protected final Mono<String> invokeAPI(String method, String urlString, String json) {
|
protected final Mono<String> invokeAPI(String method, String urlString, String json) {
|
||||||
return Mono.fromSupplier(() -> {
|
|
||||||
try {
|
|
||||||
return tryInvokeAPI(method, urlString, json);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Invokes an API synchronously and returns a text payload.
|
|
||||||
* @param method HTTP method.
|
|
||||||
* @param urlString url as String.
|
|
||||||
* @param json JSON payload or null.
|
|
||||||
* @return text
|
|
||||||
*/
|
|
||||||
protected final String tryInvokeAPI(String method, String urlString, String json) throws IOException {
|
|
||||||
String requestId = UUID.randomUUID().toString();
|
String requestId = UUID.randomUUID().toString();
|
||||||
RequestBody body = json != null ? RequestBody.create(MEDIA_TYPE_APPLICATION_JSON, json) : REQUEST_BODY_EMPTY_JSON;
|
RequestBody body = json != null ? RequestBody.create(json, MEDIA_TYPE_APPLICATION_JSON) : REQUEST_BODY_EMPTY_JSON;
|
||||||
|
|
||||||
|
// use Mono blocking wrapper to be reactive
|
||||||
|
Mono<String> blockingWrapper = Mono.fromCallable(() -> {
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(new URL(this.baseUrl + urlString))
|
.url(new URL(this.baseUrl + urlString))
|
||||||
.method(method, body)
|
.method(method, body)
|
||||||
.addHeader(Constants.HEADER_DAPR_REQUEST_ID, requestId)
|
.addHeader(Constants.HEADER_DAPR_REQUEST_ID, requestId)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// TODO: make this call async as well.
|
|
||||||
Response response = this.httpClient.newCall(request).execute();
|
try(Response response = this.httpClient.newCall(request).execute())
|
||||||
if (!response.isSuccessful())
|
|
||||||
{
|
{
|
||||||
|
if (!response.isSuccessful()) {
|
||||||
DaprError error = parseDaprError(response.body().string());
|
DaprError error = parseDaprError(response.body().string());
|
||||||
if ((error != null) && (error.getErrorCode() != null) && (error.getMessage() != null)) {
|
if ((error != null) && (error.getErrorCode() != null) && (error.getMessage() != null)) {
|
||||||
throw new DaprException(error);
|
throw new DaprException(error);
|
||||||
|
@ -110,6 +93,11 @@ public abstract class AbstractDaprClient {
|
||||||
|
|
||||||
return response.body().string();
|
return response.body().string();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return blockingWrapper.subscribeOn(Schedulers.boundedElastic());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to parse an error from Dapr response body.
|
* Tries to parse an error from Dapr response body.
|
||||||
|
|
Loading…
Reference in New Issue