mirror of https://github.com/dapr/java-sdk.git
New overload in invokeBinding + allow null typeref. (#1159)
Signed-off-by: Artur Souza <asouza.pro@gmail.com>
This commit is contained in:
parent
83e175355e
commit
bd1667b043
|
@ -270,6 +270,14 @@ abstract class AbstractDaprClient implements DaprClient, DaprPreviewClient {
|
||||||
return this.invokeBinding(bindingName, operation, data, metadata, TypeRef.get(clazz));
|
return this.invokeBinding(bindingName, operation, data, metadata, TypeRef.get(clazz));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Mono<Void> invokeBinding(InvokeBindingRequest request) {
|
||||||
|
return this.invokeBinding(request, TypeRef.VOID);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -314,6 +314,14 @@ public interface DaprClient extends AutoCloseable {
|
||||||
<T> Mono<T> invokeBinding(String bindingName, String operation, Object data, Map<String, String> metadata,
|
<T> Mono<T> invokeBinding(String bindingName, String operation, Object data, Map<String, String> metadata,
|
||||||
Class<T> clazz);
|
Class<T> clazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invokes a Binding operation.
|
||||||
|
*
|
||||||
|
* @param request The binding invocation request.
|
||||||
|
* @return a Mono with void.
|
||||||
|
*/
|
||||||
|
Mono<Void> invokeBinding(InvokeBindingRequest request);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes a Binding operation.
|
* Invokes a Binding operation.
|
||||||
*
|
*
|
||||||
|
|
|
@ -459,8 +459,10 @@ public class DaprClientImpl extends AbstractDaprClient {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
CloudEvent<T> cloudEvent = new CloudEvent<>();
|
CloudEvent<T> cloudEvent = new CloudEvent<>();
|
||||||
var object =
|
T object = null;
|
||||||
DaprClientImpl.this.objectSerializer.deserialize(message.getData().toByteArray(), type);
|
if (type != null) {
|
||||||
|
object = DaprClientImpl.this.objectSerializer.deserialize(message.getData().toByteArray(), type);
|
||||||
|
}
|
||||||
cloudEvent.setData(object);
|
cloudEvent.setData(object);
|
||||||
cloudEvent.setDatacontenttype(message.getDataContentType());
|
cloudEvent.setDatacontenttype(message.getDataContentType());
|
||||||
cloudEvent.setId(message.getId());
|
cloudEvent.setId(message.getId());
|
||||||
|
@ -528,6 +530,10 @@ public class DaprClientImpl extends AbstractDaprClient {
|
||||||
|
|
||||||
private <T> Mono<T> getMonoForHttpResponse(TypeRef<T> type, DaprHttp.Response r) {
|
private <T> Mono<T> getMonoForHttpResponse(TypeRef<T> type, DaprHttp.Response r) {
|
||||||
try {
|
try {
|
||||||
|
if (type == null) {
|
||||||
|
return Mono.empty();
|
||||||
|
}
|
||||||
|
|
||||||
T object = objectSerializer.deserialize(r.getBody(), type);
|
T object = objectSerializer.deserialize(r.getBody(), type);
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
return Mono.empty();
|
return Mono.empty();
|
||||||
|
@ -585,6 +591,9 @@ public class DaprClientImpl extends AbstractDaprClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (type == null) {
|
||||||
|
return Mono.empty();
|
||||||
|
}
|
||||||
return Mono.justOrEmpty(objectSerializer.deserialize(it.getData().toByteArray(), type));
|
return Mono.justOrEmpty(objectSerializer.deserialize(it.getData().toByteArray(), type));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw DaprException.propagate(e);
|
throw DaprException.propagate(e);
|
||||||
|
@ -706,13 +715,18 @@ public class DaprClientImpl extends AbstractDaprClient {
|
||||||
return new State<>(key, error);
|
return new State<>(key, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteString payload = item.getData();
|
|
||||||
byte[] data = payload == null ? null : payload.toByteArray();
|
|
||||||
T value = stateSerializer.deserialize(data, type);
|
|
||||||
String etag = item.getEtag();
|
String etag = item.getEtag();
|
||||||
if (etag.equals("")) {
|
if (etag.equals("")) {
|
||||||
etag = null;
|
etag = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T value = null;
|
||||||
|
if (type != null) {
|
||||||
|
ByteString payload = item.getData();
|
||||||
|
byte[] data = payload == null ? null : payload.toByteArray();
|
||||||
|
value = stateSerializer.deserialize(data, type);
|
||||||
|
}
|
||||||
|
|
||||||
return new State<>(key, value, etag, item.getMetadataMap(), null);
|
return new State<>(key, value, etag, item.getMetadataMap(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -723,7 +737,11 @@ public class DaprClientImpl extends AbstractDaprClient {
|
||||||
TypeRef<T> type) throws IOException {
|
TypeRef<T> type) throws IOException {
|
||||||
ByteString payload = response.getData();
|
ByteString payload = response.getData();
|
||||||
byte[] data = payload == null ? null : payload.toByteArray();
|
byte[] data = payload == null ? null : payload.toByteArray();
|
||||||
T value = stateSerializer.deserialize(data, type);
|
T value = null;
|
||||||
|
if (type != null) {
|
||||||
|
value = stateSerializer.deserialize(data, type);
|
||||||
|
}
|
||||||
|
|
||||||
String etag = response.getEtag();
|
String etag = response.getEtag();
|
||||||
if (etag.equals("")) {
|
if (etag.equals("")) {
|
||||||
etag = null;
|
etag = null;
|
||||||
|
@ -1108,7 +1126,11 @@ public class DaprClientImpl extends AbstractDaprClient {
|
||||||
}
|
}
|
||||||
ByteString payload = item.getData();
|
ByteString payload = item.getData();
|
||||||
byte[] data = payload == null ? null : payload.toByteArray();
|
byte[] data = payload == null ? null : payload.toByteArray();
|
||||||
T value = stateSerializer.deserialize(data, type);
|
T value = null;
|
||||||
|
if (type != null) {
|
||||||
|
value = stateSerializer.deserialize(data, type);
|
||||||
|
}
|
||||||
|
|
||||||
String etag = item.getEtag();
|
String etag = item.getEtag();
|
||||||
if (etag.equals("")) {
|
if (etag.equals("")) {
|
||||||
etag = null;
|
etag = null;
|
||||||
|
|
|
@ -25,6 +25,7 @@ import io.dapr.client.domain.DeleteStateRequest;
|
||||||
import io.dapr.client.domain.ExecuteStateTransactionRequest;
|
import io.dapr.client.domain.ExecuteStateTransactionRequest;
|
||||||
import io.dapr.client.domain.GetBulkStateRequest;
|
import io.dapr.client.domain.GetBulkStateRequest;
|
||||||
import io.dapr.client.domain.GetStateRequest;
|
import io.dapr.client.domain.GetStateRequest;
|
||||||
|
import io.dapr.client.domain.InvokeBindingRequest;
|
||||||
import io.dapr.client.domain.PublishEventRequest;
|
import io.dapr.client.domain.PublishEventRequest;
|
||||||
import io.dapr.client.domain.RuleMetadata;
|
import io.dapr.client.domain.RuleMetadata;
|
||||||
import io.dapr.client.domain.State;
|
import io.dapr.client.domain.State;
|
||||||
|
@ -327,7 +328,7 @@ public class DaprClientGrpcTest {
|
||||||
@Test
|
@Test
|
||||||
public void invokeBindingTest() throws IOException {
|
public void invokeBindingTest() throws IOException {
|
||||||
DaprProtos.InvokeBindingResponse.Builder responseBuilder =
|
DaprProtos.InvokeBindingResponse.Builder responseBuilder =
|
||||||
DaprProtos.InvokeBindingResponse.newBuilder().setData(serialize("OK"));
|
DaprProtos.InvokeBindingResponse.newBuilder().setData(serialize("OK"));
|
||||||
doAnswer((Answer<Void>) invocation -> {
|
doAnswer((Answer<Void>) invocation -> {
|
||||||
StreamObserver<DaprProtos.InvokeBindingResponse> observer = (StreamObserver<DaprProtos.InvokeBindingResponse>) invocation.getArguments()[1];
|
StreamObserver<DaprProtos.InvokeBindingResponse> observer = (StreamObserver<DaprProtos.InvokeBindingResponse>) invocation.getArguments()[1];
|
||||||
observer.onNext(responseBuilder.build());
|
observer.onNext(responseBuilder.build());
|
||||||
|
@ -339,6 +340,23 @@ public class DaprClientGrpcTest {
|
||||||
result.block();
|
result.block();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void invokeBindingVoidReturnTest() throws IOException {
|
||||||
|
DaprProtos.InvokeBindingResponse.Builder responseBuilder =
|
||||||
|
DaprProtos.InvokeBindingResponse.newBuilder().setData(serialize("OK"));
|
||||||
|
doAnswer((Answer<Void>) invocation -> {
|
||||||
|
StreamObserver<DaprProtos.InvokeBindingResponse> observer = (StreamObserver<DaprProtos.InvokeBindingResponse>) invocation.getArguments()[1];
|
||||||
|
observer.onNext(responseBuilder.build());
|
||||||
|
observer.onCompleted();
|
||||||
|
return null;
|
||||||
|
}).when(daprStub).invokeBinding(any(DaprProtos.InvokeBindingRequest.class), any());
|
||||||
|
|
||||||
|
var request = new InvokeBindingRequest("BindingName", "MyOperation");
|
||||||
|
request.setData("request");
|
||||||
|
Mono<Void> result = client.invokeBinding(request, null);
|
||||||
|
result.block();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invokeBindingByteArrayTest() {
|
public void invokeBindingByteArrayTest() {
|
||||||
DaprProtos.InvokeBindingResponse.Builder responseBuilder =
|
DaprProtos.InvokeBindingResponse.Builder responseBuilder =
|
||||||
|
|
Loading…
Reference in New Issue