Skip to content

Commit

Permalink
DaprResponse support bytes[]
Browse files Browse the repository at this point in the history
  • Loading branch information
naah69 committed Jul 1, 2022
1 parent 8bf8a19 commit e8f3f4f
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 38 deletions.
7 changes: 1 addition & 6 deletions sdk/src/main/java/io/dapr/client/DaprClientGrpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,7 @@ public <T> Mono<T> invokeMethod(InvokeMethodRequest invokeMethodRequest, TypeRef
}

private <T> Mono<T> getMono(TypeRef<T> type, byte[] data, Map<String,String> headers) {
if (type.getType() instanceof ParameterizedType) {
Type[] actualTypeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments();
if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) {
type = TypeRef.get(actualTypeArguments[0]);
}
}
type = DaprResponse.getSubType(type);
return (Mono<T>) Mono.just(new GrpcDaprResponse<T>(data, headers,objectSerializer, type));
}

Expand Down
7 changes: 1 addition & 6 deletions sdk/src/main/java/io/dapr/client/DaprClientHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,7 @@ public <T> Mono<T> invokeMethod(InvokeMethodRequest invokeMethodRequest, TypeRef
private <T> Mono<T> getMono(TypeRef<T> type, DaprHttp.Response r) {
try {
if (type.getType().getTypeName().startsWith(DaprResponse.class.getName())) {
if (type.getType() instanceof ParameterizedType) {
Type[] actualTypeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments();
if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) {
type = TypeRef.get(actualTypeArguments[0]);
}
}
type = DaprResponse.getSubType(type);
return (Mono<T>) Mono.just(new HttpDaprResponse<T>(r, objectSerializer, type));
}
T object = objectSerializer.deserialize(r.getBody(), type);
Expand Down
45 changes: 45 additions & 0 deletions sdk/src/main/java/io/dapr/client/domain/response/DaprResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@

package io.dapr.client.domain.response;

import io.dapr.utils.TypeRef;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

/**
* Response.
Expand All @@ -38,4 +44,43 @@ public interface DaprResponse<T> {
* @return response header
*/
Map<String,String> getHeaders();

/**
* get sub type from type.
* @param type response type
* @param <T> type
* @return sub type
*/
static <T> TypeRef getSubType(TypeRef<T> type) {
TypeRef resultType = type;
if (type.getType() instanceof ParameterizedType) {
Type[] actualTypeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments();
if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) {
resultType = TypeRef.get(actualTypeArguments[0]);
}
} else {
resultType = TypeRef.get(byte[].class);
}
return resultType;
}

/**
* get response bytes.
* @return bytes
*/
byte[] getSourceBytesData();

/**
* get repsonse bytes without `34`.
* string that serialized by objectmapper will surrounded by `"`.
* it will be removed when return bytes.
* @return bytes
*/
default byte[] getBytes() {
byte[] data = getSourceBytesData();
if (data.length > 1 && data[0] == 34) {
data = Arrays.copyOfRange(data, 1, data.length - 1);
}
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,21 @@ public int getCode() {

@Override
public T getData() throws IOException {
if (type.getType() == byte[].class) {
return (T) getBytes();
}
return serializer.deserialize(data, type);
}

@Override
public Map<String, String> getHeaders() {
return this.headers;
}

@Override
public byte[] getSourceBytesData() {
return data;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,19 @@ public T getData() throws IOException {
if (type.getType() == String.class) {
return (T) new String(data);
}
if (type.getType() == byte[].class) {
return (T) getBytes();
}
return serializer.deserialize(data, type);
}

@Override
public Map<String, String> getHeaders() {
return response.getHeaders();
}

@Override
public byte[] getSourceBytesData() {
return response.getBody();
}
}
16 changes: 16 additions & 0 deletions sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,22 @@ public void invokeServiceTestReturnResponse() throws IOException {
assertEquals(expected, res.getData());
}

@Test
public void invokeServiceTestReturnResponseWithBytes() throws IOException {
String expected = "Value";
doAnswer((Answer<Void>) invocation -> {
StreamObserver<CommonProtos.InvokeResponse> observer = (StreamObserver<CommonProtos.InvokeResponse>) invocation.getArguments()[1];
observer.onNext(CommonProtos.InvokeResponse.newBuilder().setData(getAny(expected)).build());
observer.onCompleted();
return null;
}).when(daprStub).invokeService(any(DaprProtos.InvokeServiceRequest.class), any());

Mono<DaprResponse> result = client.invokeMethod("appId", "method", "request", HttpExtension.NONE, null, new TypeRef<DaprResponse>() {});
DaprResponse res = result.block();

assertEquals(expected, new String((byte[]) res.getData()));
}

@Test
public void invokeServiceObjectTest() throws Exception {
MyObject object = new MyObject(1, "Value");
Expand Down
22 changes: 22 additions & 0 deletions sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,28 @@ public void invokeServiceReturnResponse() throws IOException {
Assertions.assertEquals(resultHeaderValue,response.getHeaders().get(resultHeaderName));
}

@Test
public void invokeServiceReturnResponseBytes() throws IOException {
String resultString = "request success";
String resultHeaderName = "test-header";
String resultHeaderValue = "1";
mockInterceptor.addRule()
.post("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
.respond(resultString)
.addHeader(resultHeaderName,resultHeaderValue);

InvokeMethodRequest req = new InvokeMethodRequest("41", "neworder")
.setBody("request")
.setHttpExtension(HttpExtension.POST);

Mono<DaprResponse> result = daprClientHttp.invokeMethod(req, new TypeRef<DaprResponse>() {});
DaprResponse response = result.block();
Assertions.assertNotNull(response);
Assertions.assertEquals(200, response.getCode());
Assertions.assertEquals(resultString,new String((byte[]) response.getData()));
Assertions.assertEquals(resultHeaderValue,response.getHeaders().get(resultHeaderName));
}

@Test
public void invokeBinding() throws IOException {
String resultString = "request success";
Expand Down
26 changes: 0 additions & 26 deletions settings.xml

This file was deleted.

0 comments on commit e8f3f4f

Please sign in to comment.