Change Service Invocation API. Fix metadata bug. (#312)

* Change Service Invocation API

* Address review comments
This commit is contained in:
Mukundan Sundararajan 2020-07-30 10:44:25 -07:00 committed by GitHub
parent 8e19ba68c9
commit 55b0e3a1e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 429 additions and 238 deletions

View File

@ -7,7 +7,7 @@ package io.dapr.examples.invoke.grpc;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.Verb;
import io.dapr.client.HttpExtension;
/**
* 1. Build and install jars:
@ -33,7 +33,7 @@ public class HelloWorldClient {
while (true) {
String message = "Message #" + (count++);
System.out.println("Sending message: " + message);
client.invokeService(Verb.POST, serviceAppId, method, message).block();
client.invokeService(serviceAppId, method, message, HttpExtension.NONE).block();
System.out.println("Message sent: " + message);
Thread.sleep(1000);

View File

@ -96,7 +96,7 @@ private static class HelloWorldClient {
while (true) {
String message = "Message #" + (count++);
System.out.println("Sending message: " + message);
client.invokeService(Verb.POST, serviceAppId, method, message).block();
client.invokeService(serviceAppId, method, message, HttpExtension.NONE).block();
System.out.println("Message sent: " + message);
Thread.sleep(1000);

View File

@ -7,7 +7,7 @@ package io.dapr.examples.invoke.http;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.Verb;
import io.dapr.client.HttpExtension;
/**
* 1. Build and install jars:
@ -32,8 +32,8 @@ public class InvokeClient {
public static void main(String[] args) {
DaprClient client = (new DaprClientBuilder()).build();
for (String message : args) {
byte[] response = client.invokeService(
Verb.POST, SERVICE_APP_ID, "say", message, null, byte[].class).block();
byte[] response = client.invokeService(SERVICE_APP_ID, "say", message, HttpExtension.POST, null,
byte[].class).block();
System.out.println(new String(response));
}

View File

@ -113,8 +113,8 @@ private static final String SERVICE_APP_ID = "invokedemo";
public static void main(String[] args) {
DaprClient client = (new DaprClientBuilder()).build();
for (String message : args) {
byte[] response = client.invokeService(
Verb.POST, SERVICE_APP_ID, "say", message, null, byte[].class).block();
byte[] response = client.invokeService(SERVICE_APP_ID, "say",
message, HttpExtension.POST, null, byte[].class).block();
System.out.println(new String(response));
}
}

View File

@ -8,22 +8,18 @@ package io.dapr.it.binding.http;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.Verb;
import io.dapr.client.HttpExtension;
import io.dapr.it.BaseIT;
import io.dapr.it.DaprRun;
import io.dapr.it.services.EmptyService;
import io.dapr.serializer.DefaultObjectSerializer;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static io.dapr.it.Retry.callWithRetry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@ -100,10 +96,10 @@ public class BindingIT extends BaseIT {
System.out.println("Checking results ...");
final List<String> messages =
client.invokeService(
Verb.GET,
daprRun.getAppName(),
"messages",
null,
HttpExtension.GET,
List.class).block();
assertEquals(2, messages.size());

View File

@ -2,20 +2,20 @@ package io.dapr.it.methodinvoke.http;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.Verb;
import io.dapr.client.DaprHttp;
import io.dapr.client.HttpExtension;
import io.dapr.it.BaseIT;
import io.dapr.it.DaprRun;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.IOException;
import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.runners.Parameterized.*;
import static org.junit.runners.Parameterized.Parameter;
import static org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class MethodInvokeIT extends BaseIT {
@ -66,20 +66,21 @@ public class MethodInvokeIT extends BaseIT {
for (int i = 0; i < NUM_MESSAGES; i++) {
String message = String.format("This is message #%d", i);
//Publishing messages
client.invokeService(Verb.POST, daprRun.getAppName(), "messages", message.getBytes()).block();
client.invokeService(daprRun.getAppName(), "messages", message.getBytes(), HttpExtension.POST).block();
System.out.println("Invoke method messages : " + message);
}
Map<Integer,String> messages = client.invokeService(Verb.GET, daprRun.getAppName(), "messages", null, Map.class).block();
Map<Integer,String> messages = client.invokeService(daprRun.getAppName(), "messages", null,
HttpExtension.GET, Map.class).block();
assertEquals(10, messages.size());
client.invokeService(Verb.DELETE,daprRun.getAppName(),"messages/1",null).block();
client.invokeService(daprRun.getAppName(),"messages/1",null, HttpExtension.DELETE).block();
messages = client.invokeService(Verb.GET, daprRun.getAppName(), "messages", null, Map.class).block();
messages = client.invokeService(daprRun.getAppName(), "messages", null, HttpExtension.GET, Map.class).block();
assertEquals(9, messages.size());
client.invokeService(Verb.PUT, daprRun.getAppName(), "messages/2", "updated message".getBytes()).block();
messages = client.invokeService(Verb.GET, daprRun.getAppName(), "messages", null, Map.class).block();
client.invokeService(daprRun.getAppName(), "messages/2", "updated message".getBytes(), HttpExtension.PUT).block();
messages = client.invokeService(daprRun.getAppName(), "messages", null, HttpExtension.GET, Map.class).block();
assertEquals("updated message", messages.get("2"));
}
@ -94,16 +95,16 @@ public class MethodInvokeIT extends BaseIT {
person.setLastName(String.format("Last Name %d", i));
person.setBirthDate(new Date());
//Publishing messages
client.invokeService(Verb.POST, daprRun.getAppName(), "persons", person).block();
client.invokeService(daprRun.getAppName(), "persons", person, HttpExtension.POST).block();
System.out.println("Invoke method persons with parameter : " + person);
}
List<Person> persons = Arrays.asList(client.invokeService(Verb.GET, daprRun.getAppName(), "persons", null, Person[].class).block());
List<Person> persons = Arrays.asList(client.invokeService(daprRun.getAppName(), "persons", null, HttpExtension.GET, Person[].class).block());
assertEquals(10, persons.size());
client.invokeService(Verb.DELETE,daprRun.getAppName(),"persons/1",null).block();
client.invokeService(daprRun.getAppName(),"persons/1",null, HttpExtension.DELETE).block();
persons = Arrays.asList(client.invokeService(Verb.GET, daprRun.getAppName(), "persons", null, Person[].class).block());
persons = Arrays.asList(client.invokeService(daprRun.getAppName(), "persons", null, HttpExtension.GET, Person[].class).block());
assertEquals(9, persons.size());
Person person= new Person();
@ -111,9 +112,9 @@ public class MethodInvokeIT extends BaseIT {
person.setLastName("Smith");
person.setBirthDate(Calendar.getInstance().getTime());
client.invokeService(Verb.PUT, daprRun.getAppName(), "persons/2", person).block();
client.invokeService(daprRun.getAppName(), "persons/2", person, HttpExtension.PUT).block();
persons = Arrays.asList(client.invokeService(Verb.GET, daprRun.getAppName(), "persons", null, Person[].class).block());
persons = Arrays.asList(client.invokeService(daprRun.getAppName(), "persons", null, HttpExtension.GET, Person[].class).block());
Person resultPerson= persons.get(1);
assertEquals("John", resultPerson.getName());
assertEquals("Smith", resultPerson.getLastName());

View File

@ -7,18 +7,19 @@ package io.dapr.it.pubsub.http;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.Verb;
import io.dapr.client.DaprHttp;
import io.dapr.client.HttpExtension;
import io.dapr.it.BaseIT;
import io.dapr.it.DaprRun;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import java.util.Collections;
import java.util.List;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static io.dapr.it.Retry.callWithRetry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -91,7 +92,7 @@ public class PubSubIT extends BaseIT {
callWithRetry(() -> {
System.out.println("Checking results for topic " + TOPIC_NAME);
final List<String> messages = client.invokeService(Verb.GET, daprRun.getAppName(), "messages/testingtopic", null, List.class).block();
final List<String> messages = client.invokeService(daprRun.getAppName(), "messages/testingtopic", null, HttpExtension.GET, List.class).block();
assertEquals(11, messages.size());
for (int i = 0; i < NUM_MESSAGES; i++) {
@ -110,7 +111,7 @@ public class PubSubIT extends BaseIT {
callWithRetry(() -> {
System.out.println("Checking results for topic " + ANOTHER_TOPIC_NAME);
final List<String> messages = client.invokeService(Verb.GET, daprRun.getAppName(), "messages/anothertopic", null, List.class).block();
final List<String> messages = client.invokeService(daprRun.getAppName(), "messages/anothertopic", null, HttpExtension.GET, List.class).block();
assertEquals(10, messages.size());
for (int i = 0; i < NUM_MESSAGES; i++) {

View File

@ -43,130 +43,144 @@ public interface DaprClient {
/**
* Invoke a service with all possible parameters, using serialization.
*
* @param verb The Verb to be used for HTTP will be the HTTP Verb, for GRPC is just a metadata value.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be send in request.
* @param type The Type needed as return for the call.
* @param <T> The Type of the return, use byte[] to skip serialization.
* @return A Mono Plan of type type .
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param httpExtension Additional fields that are needed if the receiving app is listening on
* HTTP, {@link HttpExtension#NONE} otherwise.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be sent in request.
* @param type The Type needed as return for the call.
* @param <T> The Type of the return, use byte[] to skip serialization.
* @return A Mono Plan of type type.
*/
<T> Mono<T> invokeService(
Verb verb, String appId, String method, Object request, Map<String, String> metadata, TypeRef<T> type);
<T> Mono<T> invokeService(String appId, String method, Object request, HttpExtension httpExtension,
Map<String, String> metadata, TypeRef<T> type);
/**
* Invoke a service with all possible parameters, using serialization.
*
* @param verb The Verb to be used for HTTP will be the HTTP Verb, for GRPC is just a metadata value.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be send in request.
* @param clazz The type needed as return for the call.
* @param <T> The type of the return, use byte[] to skip serialization.
* @return A Mono Plan of type type .
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param httpExtension Additional fields that are needed if the receiving app is listening on
* HTTP, {@link HttpExtension#NONE} otherwise.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be sent in request.
* @param clazz The type needed as return for the call.
* @param <T> The Type of the return, use byte[] to skip serialization.
* @return A Mono Plan of type type.
*/
<T> Mono<T> invokeService(
Verb verb, String appId, String method, Object request, Map<String, String> metadata, Class<T> clazz);
<T> Mono<T> invokeService(String appId, String method, Object request, HttpExtension httpExtension,
Map<String, String> metadata, Class<T> clazz);
/**
* Invoke a service without metadata, using serialization.
* Invoke a service with all possible parameters, using serialization.
*
* @param verb The Verb to be used for HTTP will be the HTTP Verb, for GRPC is just a metadata value.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param type The type needed as return for the call.
* @param <T> The type of the return, use byte[] to skip serialization.
* @return A Mono Plan of type type .
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param httpExtension Additional fields that are needed if the receiving app is listening on
* HTTP, {@link HttpExtension#NONE} otherwise.
* @param type The Type needed as return for the call.
* @param <T> The Type of the return, use byte[] to skip serialization.
* @return A Mono Plan of type type.
*/
<T> Mono<T> invokeService(Verb verb, String appId, String method, Object request, TypeRef<T> type);
<T> Mono<T> invokeService(String appId, String method, Object request, HttpExtension httpExtension, TypeRef<T> type);
/**
* Invoke a service without metadata, using serialization.
* Invoke a service with all possible parameters, using serialization.
*
* @param verb The Verb to be used for HTTP will be the HTTP Verb, for GRPC is just a metadata value.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param clazz The type needed as return for the call.
* @param <T> The type of the return, use byte[] to skip serialization.
* @return A Mono Plan of type type .
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param httpExtension Additional fields that are needed if the receiving app is listening on
* HTTP, {@link HttpExtension#NONE} otherwise.
* @param clazz The type needed as return for the call.
* @param <T> The Type of the return, use byte[] to skip serialization.
* @return A Mono Plan of type type.
*/
<T> Mono<T> invokeService(Verb verb, String appId, String method, Object request, Class<T> clazz);
<T> Mono<T> invokeService(String appId, String method, Object request, HttpExtension httpExtension, Class<T> clazz);
/**
* Invoke a service without input, using serialization for response.
* Invoke a service with all possible parameters, using serialization.
*
* @param verb The Verb to be used for HTTP will be the HTTP Verb, for GRPC is just a metadata value.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be send in request.
* @param type The type needed as return for the call.
* @param <T> The type of the return, use byte[] to skip serialization.
* @return A Mono plan of type type .
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param httpExtension Additional fields that are needed if the receiving app is listening on
* HTTP, {@link HttpExtension#NONE} otherwise.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be sent in request.
* @param type The Type needed as return for the call.
* @param <T> The Type of the return, use byte[] to skip serialization.
* @return A Mono Plan of type type.
*/
<T> Mono<T> invokeService(Verb verb, String appId, String method, Map<String, String> metadata, TypeRef<T> type);
<T> Mono<T> invokeService(String appId, String method, HttpExtension httpExtension, Map<String, String> metadata,
TypeRef<T> type);
/**
* Invoke a service without input, using serialization for response.
* Invoke a service with all possible parameters, using serialization.
*
* @param verb The Verb to be used for HTTP will be the HTTP Verb, for GRPC is just a metadata value.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be send in request.
* @param clazz The type needed as return for the call.
* @param <T> The type of the return, use byte[] to skip serialization.
* @return A Mono plan of type type .
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param httpExtension Additional fields that are needed if the receiving app is listening on
* HTTP, {@link HttpExtension#NONE} otherwise.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be sent in request.
* @param clazz The type needed as return for the call.
* @param <T> The Type of the return, use byte[] to skip serialization.
* @return A Mono Plan of type type.
*/
<T> Mono<T> invokeService(Verb verb, String appId, String method, Map<String, String> metadata, Class<T> clazz);
<T> Mono<T> invokeService(String appId, String method, HttpExtension httpExtension, Map<String, String> metadata,
Class<T> clazz);
/**
* Invoke a service with void response, using serialization.
* Invoke a service with all possible parameters, using serialization.
*
* @param verb The Verb to be used for HTTP will be the HTTP Verb, for GRPC is just a metadata value.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be send in request.
* @return A Mono plan for Void.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param httpExtension Additional fields that are needed if the receiving app is listening on
* HTTP, {@link HttpExtension#NONE} otherwise.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be sent in request.
* @return A Mono Plan of type type.
*/
Mono<Void> invokeService(Verb verb, String appId, String method, Object request, Map<String, String> metadata);
Mono<Void> invokeService(String appId, String method, Object request, HttpExtension httpExtension,
Map<String, String> metadata);
/**
* Invoke a service with void response, no metadata and using serialization.
* Invoke a service with all possible parameters, using serialization.
*
* @param verb The Verb to be used for HTTP will be the HTTP Verb, for GRPC is just a metadata value.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @return A Mono plan for Void.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param httpExtension Additional fields that are needed if the receiving app is listening on
* HTTP, {@link HttpExtension#NONE} otherwise.
* @return A Mono Plan of type type.
*/
Mono<Void> invokeService(Verb verb, String appId, String method, Object request);
Mono<Void> invokeService(String appId, String method, Object request, HttpExtension httpExtension);
/**
* Invoke a service without input and void response.
* Invoke a service with all possible parameters, using serialization.
*
* @param verb The Verb to be used for HTTP will be the HTTP Verb, for GRPC is just a metadata value.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be send in request.
* @return A Mono plan for Void.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param httpExtension Additional fields that are needed if the receiving app is listening on
* HTTP, {@link HttpExtension#NONE} otherwise.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be sent in request.
* @return A Mono Plan of type type.
*/
Mono<Void> invokeService(Verb verb, String appId, String method, Map<String, String> metadata);
Mono<Void> invokeService(String appId, String method, HttpExtension httpExtension, Map<String, String> metadata);
/**
* Invoke a service without serialization.
* Invoke a service with all possible parameters, using serialization.
*
* @param verb The Verb to be used for HTTP will be the HTTP Verb, for GRPC is just a metadata value.
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be send in request.
* @return A Mono plan of byte[].
* @param appId The Application ID where the service is.
* @param method The actual Method to be call in the application.
* @param request The request to be sent to invoke the service, use byte[] to skip serialization.
* @param httpExtension Additional fields that are needed if the receiving app is listening on
* HTTP, {@link HttpExtension#NONE} otherwise.
* @param metadata Metadata (in GRPC) or headers (in HTTP) to be sent in request.
* @return A Mono Plan of type type.
*/
Mono<byte[]> invokeService(Verb verb, String appId, String method, byte[] request, Map<String, String> metadata);
Mono<byte[]> invokeService(String appId, String method, byte[] request, HttpExtension httpExtension,
Map<String, String> metadata);
/**
* Invokes a Binding operation.

View File

@ -12,7 +12,6 @@ import com.google.protobuf.Duration;
import com.google.protobuf.Empty;
import io.dapr.client.domain.State;
import io.dapr.client.domain.StateOptions;
import io.dapr.client.domain.Verb;
import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.utils.TypeRef;
import io.dapr.v1.CommonProtos;
@ -130,19 +129,11 @@ public class DaprClientGrpc implements DaprClient {
}
}
/**
* {@inheritDoc}
*/
@Override
public <T> Mono<T> invokeService(
Verb verb,
String appId,
String method,
Object request,
Map<String, String> metadata,
TypeRef<T> type) {
public <T> Mono<T> invokeService(String appId, String method, Object request, HttpExtension httpExtension,
Map<String, String> metadata, TypeRef<T> type) {
try {
DaprProtos.InvokeServiceRequest envelope = buildInvokeServiceRequest(verb.toString(), appId, method, request);
DaprProtos.InvokeServiceRequest envelope = buildInvokeServiceRequest(httpExtension, appId, method, request);
return Mono.fromCallable(() -> {
ListenableFuture<CommonProtos.InvokeResponse> futureResponse =
client.invokeService(envelope);
@ -159,13 +150,13 @@ public class DaprClientGrpc implements DaprClient {
*/
@Override
public <T> Mono<T> invokeService(
Verb verb,
String appId,
String method,
Object request,
HttpExtension httpExtension,
Map<String, String> metadata,
Class<T> clazz) {
return this.invokeService(verb, appId, method, request, metadata, TypeRef.get(clazz));
return this.invokeService(appId, method, request, httpExtension, metadata, TypeRef.get(clazz));
}
/**
@ -173,8 +164,8 @@ public class DaprClientGrpc implements DaprClient {
*/
@Override
public <T> Mono<T> invokeService(
Verb verb, String appId, String method, Map<String, String> metadata, TypeRef<T> type) {
return this.invokeService(verb, appId, method, null, metadata, type);
String appId, String method, HttpExtension httpExtension, Map<String, String> metadata, TypeRef<T> type) {
return this.invokeService(appId, method, null, httpExtension, metadata, type);
}
/**
@ -182,32 +173,34 @@ public class DaprClientGrpc implements DaprClient {
*/
@Override
public <T> Mono<T> invokeService(
Verb verb, String appId, String method, Map<String, String> metadata, Class<T> clazz) {
return this.invokeService(verb, appId, method, null, metadata, TypeRef.get(clazz));
String appId, String method, HttpExtension httpExtension, Map<String, String> metadata, Class<T> clazz) {
return this.invokeService(appId, method, null, httpExtension, metadata, TypeRef.get(clazz));
}
/**
* {@inheritDoc}
*/
@Override
public <T> Mono<T> invokeService(Verb verb, String appId, String method, Object request, TypeRef<T> type) {
return this.invokeService(verb, appId, method, request, null, type);
public <T> Mono<T> invokeService(String appId, String method, Object request, HttpExtension httpExtension,
TypeRef<T> type) {
return this.invokeService(appId, method, request, httpExtension, null, type);
}
/**
* {@inheritDoc}
*/
@Override
public <T> Mono<T> invokeService(Verb verb, String appId, String method, Object request, Class<T> clazz) {
return this.invokeService(verb, appId, method, request, null, TypeRef.get(clazz));
public <T> Mono<T> invokeService(String appId, String method, Object request, HttpExtension httpExtension,
Class<T> clazz) {
return this.invokeService(appId, method, request, httpExtension, null, TypeRef.get(clazz));
}
/**
* {@inheritDoc}
*/
@Override
public Mono<Void> invokeService(Verb verb, String appId, String method, Object request) {
return this.invokeService(verb, appId, method, request, null, TypeRef.BYTE_ARRAY).then();
public Mono<Void> invokeService(String appId, String method, Object request, HttpExtension httpExtension) {
return this.invokeService(appId, method, request, httpExtension, null, TypeRef.BYTE_ARRAY).then();
}
/**
@ -215,8 +208,8 @@ public class DaprClientGrpc implements DaprClient {
*/
@Override
public Mono<Void> invokeService(
Verb verb, String appId, String method, Object request, Map<String, String> metadata) {
return this.invokeService(verb, appId, method, request, metadata, TypeRef.BYTE_ARRAY).then();
String appId, String method, Object request, HttpExtension httpExtension, Map<String, String> metadata) {
return this.invokeService(appId, method, request, httpExtension, metadata, TypeRef.BYTE_ARRAY).then();
}
/**
@ -224,8 +217,8 @@ public class DaprClientGrpc implements DaprClient {
*/
@Override
public Mono<Void> invokeService(
Verb verb, String appId, String method, Map<String, String> metadata) {
return this.invokeService(verb, appId, method, null, metadata, TypeRef.BYTE_ARRAY).then();
String appId, String method, HttpExtension httpExtension, Map<String, String> metadata) {
return this.invokeService(appId, method, null, httpExtension, metadata, TypeRef.BYTE_ARRAY).then();
}
/**
@ -233,8 +226,8 @@ public class DaprClientGrpc implements DaprClient {
*/
@Override
public Mono<byte[]> invokeService(
Verb verb, String appId, String method, byte[] request, Map<String, String> metadata) {
return this.invokeService(verb, appId, method, request, metadata, TypeRef.BYTE_ARRAY);
String appId, String method, byte[] request, HttpExtension httpExtension, Map<String, String> metadata) {
return this.invokeService(appId, method, request, httpExtension, metadata, TypeRef.BYTE_ARRAY);
}
/**
@ -578,16 +571,19 @@ public class DaprClientGrpc implements DaprClient {
/**
* Builds the object io.dapr.{@link DaprProtos.InvokeServiceRequest} to be send based on the parameters.
*
* @param verb String that must match HTTP Methods
* @param appId The application id to be invoked
* @param method The application method to be invoked
* @param request The body of the request to be send as part of the invokation
* @param <K> The Type of the Body
* @return The object to be sent as part of the invokation.
* @param httpExtension Object for HttpExtension
* @param appId The application id to be invoked
* @param method The application method to be invoked
* @param request The body of the request to be send as part of the invocation
* @param <K> The Type of the Body
* @return The object to be sent as part of the invocation.
* @throws IOException If there's an issue serializing the request.
*/
private <K> DaprProtos.InvokeServiceRequest buildInvokeServiceRequest(
String verb, String appId, String method, K request) throws IOException {
HttpExtension httpExtension, String appId, String method, K request) throws IOException {
if (httpExtension == null) {
throw new IllegalArgumentException("HttpExtension cannot be null. Use HttpExtension.NONE instead.");
}
CommonProtos.InvokeRequest.Builder requestBuilder = CommonProtos.InvokeRequest.newBuilder();
requestBuilder.setMethod(method);
if (request != null) {
@ -597,13 +593,9 @@ public class DaprClientGrpc implements DaprClient {
} else {
requestBuilder.setData(Any.newBuilder().build());
}
CommonProtos.HTTPExtension.Builder httpExtensionBuilder = CommonProtos.HTTPExtension.newBuilder();
if ((verb != null) && !verb.isEmpty()) {
httpExtensionBuilder.setVerb(CommonProtos.HTTPExtension.Verb.valueOf(verb.toUpperCase()));
} else {
httpExtensionBuilder.setVerb(CommonProtos.HTTPExtension.Verb.NONE);
}
httpExtensionBuilder.setVerb(CommonProtos.HTTPExtension.Verb.valueOf(httpExtension.getMethod().toString()))
.putAllQuerystring(httpExtension.getQueryString());
requestBuilder.setHttpExtension(httpExtensionBuilder.build());
DaprProtos.InvokeServiceRequest.Builder envelopeBuilder = DaprProtos.InvokeServiceRequest.newBuilder()

View File

@ -122,14 +122,19 @@ public class DaprClientHttp implements DaprClient {
/**
* {@inheritDoc}
*/
@Override
public <T> Mono<T> invokeService(
Verb verb, String appId, String method, Object request, Map<String, String> metadata, TypeRef<T> type) {
String appId,
String method,
Object request,
HttpExtension httpExtension,
Map<String, String> metadata,
TypeRef<T> type) {
try {
if (verb == null) {
throw new IllegalArgumentException("Verb cannot be null.");
if (httpExtension == null) {
throw new IllegalArgumentException("HttpExtension cannot be null. Use HttpExtension.NONE instead.");
}
String httMethod = verb.toString();
// If the httpExtension is not null, then the method will not be null based on checks in constructor
String httMethod = httpExtension.getMethod().toString();
if (appId == null || appId.trim().isEmpty()) {
throw new IllegalArgumentException("App Id cannot be null or empty.");
}
@ -138,7 +143,8 @@ public class DaprClientHttp implements DaprClient {
}
String path = String.format("%s/%s/method/%s", Constants.INVOKE_PATH, appId, method);
byte[] serializedRequestBody = objectSerializer.serialize(request);
Mono<DaprHttp.Response> response = this.client.invokeApi(httMethod, path, metadata, serializedRequestBody, null);
Mono<DaprHttp.Response> response = this.client.invokeApi(httMethod, path,
httpExtension.getQueryString(), serializedRequestBody, metadata);
return response.flatMap(r -> {
try {
T object = objectSerializer.deserialize(r.getBody(), type);
@ -161,13 +167,13 @@ public class DaprClientHttp implements DaprClient {
*/
@Override
public <T> Mono<T> invokeService(
Verb verb,
String appId,
String method,
Object request,
HttpExtension httpExtension,
Map<String, String> metadata,
Class<T> clazz) {
return this.invokeService(verb, appId, method, request, metadata, TypeRef.get(clazz));
return this.invokeService(appId, method, request, httpExtension, metadata, TypeRef.get(clazz));
}
/**
@ -175,8 +181,8 @@ public class DaprClientHttp implements DaprClient {
*/
@Override
public <T> Mono<T> invokeService(
Verb verb, String appId, String method, Map<String, String> metadata, TypeRef<T> type) {
return this.invokeService(verb, appId, method, null, metadata, type);
String appId, String method, HttpExtension httpExtension, Map<String, String> metadata, TypeRef<T> type) {
return this.invokeService(appId, method, null, httpExtension, metadata, type);
}
/**
@ -184,32 +190,34 @@ public class DaprClientHttp implements DaprClient {
*/
@Override
public <T> Mono<T> invokeService(
Verb verb, String appId, String method, Map<String, String> metadata, Class<T> clazz) {
return this.invokeService(verb, appId, method, null, metadata, TypeRef.get(clazz));
String appId, String method, HttpExtension httpExtension, Map<String, String> metadata, Class<T> clazz) {
return this.invokeService(appId, method, null, httpExtension, metadata, TypeRef.get(clazz));
}
/**
* {@inheritDoc}
*/
@Override
public <T> Mono<T> invokeService(Verb verb, String appId, String method, Object request, TypeRef<T> type) {
return this.invokeService(verb, appId, method, request, null, type);
public <T> Mono<T> invokeService(String appId, String method, Object request, HttpExtension httpExtension,
TypeRef<T> type) {
return this.invokeService(appId, method, request, httpExtension, null, type);
}
/**
* {@inheritDoc}
*/
@Override
public <T> Mono<T> invokeService(Verb verb, String appId, String method, Object request, Class<T> clazz) {
return this.invokeService(verb, appId, method, request, null, TypeRef.get(clazz));
public <T> Mono<T> invokeService(String appId, String method, Object request, HttpExtension httpExtension,
Class<T> clazz) {
return this.invokeService(appId, method, request, httpExtension,null, TypeRef.get(clazz));
}
/**
* {@inheritDoc}
*/
@Override
public Mono<Void> invokeService(Verb verb, String appId, String method, Object request) {
return this.invokeService(verb, appId, method, request, null, TypeRef.BYTE_ARRAY).then();
public Mono<Void> invokeService(String appId, String method, Object request, HttpExtension httpExtension) {
return this.invokeService(appId, method, request, httpExtension, null, TypeRef.BYTE_ARRAY).then();
}
/**
@ -217,8 +225,8 @@ public class DaprClientHttp implements DaprClient {
*/
@Override
public Mono<Void> invokeService(
Verb verb, String appId, String method, Object request, Map<String, String> metadata) {
return this.invokeService(verb, appId, method, request, metadata, TypeRef.BYTE_ARRAY).then();
String appId, String method, Object request, HttpExtension httpExtension, Map<String, String> metadata) {
return this.invokeService(appId, method, request, httpExtension, metadata, TypeRef.BYTE_ARRAY).then();
}
/**
@ -226,8 +234,8 @@ public class DaprClientHttp implements DaprClient {
*/
@Override
public Mono<Void> invokeService(
Verb verb, String appId, String method, Map<String, String> metadata) {
return this.invokeService(verb, appId, method, null, metadata, TypeRef.BYTE_ARRAY).then();
String appId, String method, HttpExtension httpExtension, Map<String, String> metadata) {
return this.invokeService(appId, method, null, httpExtension, metadata, TypeRef.BYTE_ARRAY).then();
}
/**
@ -235,8 +243,8 @@ public class DaprClientHttp implements DaprClient {
*/
@Override
public Mono<byte[]> invokeService(
Verb verb, String appId, String method, byte[] request, Map<String, String> metadata) {
return this.invokeService(verb, appId, method, request, metadata, TypeRef.BYTE_ARRAY);
String appId, String method, byte[] request, HttpExtension httpExtension, Map<String, String> metadata) {
return this.invokeService(appId, method, request, httpExtension, metadata, TypeRef.BYTE_ARRAY);
}
/**

View File

@ -26,15 +26,24 @@ import java.util.Optional;
import java.util.UUID;
public class DaprHttp {
/**
* Dapr's http default scheme.
*/
private static final String DEFAULT_HTTP_SCHEME = "http";
/**
* HTTP Methods supported.
*/
public enum HttpMethods {
NONE,
GET,
PUT,
POST,
DELETE
DELETE,
HEAD,
CONNECT,
OPTIONS,
TRACE
}
public static class Response {
@ -171,7 +180,10 @@ public class DaprHttp {
body = RequestBody.Companion.create(content, mediaType);
}
HttpUrl.Builder urlBuilder = new HttpUrl.Builder();
urlBuilder.scheme("http").host(Constants.DEFAULT_HOSTNAME).port(this.port).addPathSegments(urlString);
urlBuilder.scheme(DEFAULT_HTTP_SCHEME)
.host(Constants.DEFAULT_HOSTNAME)
.port(this.port)
.addPathSegments(urlString);
Optional.ofNullable(urlParameters).orElse(Collections.emptyMap()).entrySet().stream()
.forEach(urlParameter -> urlBuilder.addQueryParameter(urlParameter.getKey(), urlParameter.getValue()));

View File

@ -0,0 +1,90 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/
package io.dapr.client;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* HTTP Extension class.
* This class is only needed if the app you are calling is listening on HTTP.
* It contains properties that represent data that may be populated for an HTTP receiver.
*/
public final class HttpExtension {
/**
* Convenience HttpExtension object for {@link DaprHttp.HttpMethods#NONE} with empty queryString.
*/
public static final HttpExtension NONE = new HttpExtension(DaprHttp.HttpMethods.NONE, new HashMap<>());
/**
* Convenience HttpExtension object for the {@link DaprHttp.HttpMethods#GET} Verb with empty queryString.
*/
public static final HttpExtension GET = new HttpExtension(DaprHttp.HttpMethods.GET, new HashMap<>());
/**
* Convenience HttpExtension object for the {@link DaprHttp.HttpMethods#PUT} Verb with empty queryString.
*/
public static final HttpExtension PUT = new HttpExtension(DaprHttp.HttpMethods.PUT, new HashMap<>());
/**
* Convenience HttpExtension object for the {@link DaprHttp.HttpMethods#POST} Verb with empty queryString.
*/
public static final HttpExtension POST = new HttpExtension(DaprHttp.HttpMethods.POST, new HashMap<>());
/**
* Convenience HttpExtension object for the {@link DaprHttp.HttpMethods#DELETE} Verb with empty queryString.
*/
public static final HttpExtension DELETE = new HttpExtension(DaprHttp.HttpMethods.DELETE, new HashMap<>());
/**
* Convenience HttpExtension object for the {@link DaprHttp.HttpMethods#HEAD} Verb with empty queryString.
*/
public static final HttpExtension HEAD = new HttpExtension(DaprHttp.HttpMethods.HEAD, new HashMap<>());
/**
* Convenience HttpExtension object for the {@link DaprHttp.HttpMethods#CONNECT} Verb with empty queryString.
*/
public static final HttpExtension CONNECT = new HttpExtension(DaprHttp.HttpMethods.CONNECT, new HashMap<>());
/**
* Convenience HttpExtension object for the {@link DaprHttp.HttpMethods#OPTIONS} Verb with empty queryString.
*/
public static final HttpExtension OPTIONS = new HttpExtension(DaprHttp.HttpMethods.OPTIONS, new HashMap<>());
/**
* Convenience HttpExtension object for the {@link DaprHttp.HttpMethods#TRACE} Verb with empty queryString.
*/
public static final HttpExtension TRACE = new HttpExtension(DaprHttp.HttpMethods.TRACE, new HashMap<>());
/**
* HTTP verb.
*/
private DaprHttp.HttpMethods method;
/**
* HTTP querystring.
*/
private Map<String, String> queryString;
/**
* Construct a HttpExtension object.
* @param method Required value denoting the HttpMethod.
* @param queryString Non-null map value for the queryString for a HTTP listener.
* @see io.dapr.client.DaprHttp.HttpMethods for supported methods.
* @throws IllegalArgumentException on null method or queryString.
*/
public HttpExtension(DaprHttp.HttpMethods method, Map<String, String> queryString) {
if (method == null) {
throw new IllegalArgumentException("HttpExtension method cannot be null");
} else if (queryString == null) {
throw new IllegalArgumentException("HttpExtension queryString map cannot be null");
}
this.method = method;
this.queryString = Collections.unmodifiableMap(queryString);
}
public DaprHttp.HttpMethods getMethod() {
return method;
}
public Map<String, String> getQueryString() {
return queryString;
}
}

View File

@ -14,12 +14,11 @@ import io.dapr.client.domain.State;
import io.dapr.client.domain.StateOptions;
import io.dapr.client.domain.Verb;
import io.dapr.serializer.DefaultObjectSerializer;
import io.dapr.utils.TypeRef;
import io.dapr.v1.CommonProtos;
import io.dapr.v1.DaprGrpc;
import io.dapr.v1.DaprProtos;
import java.util.Collections;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
@ -27,6 +26,7 @@ import reactor.core.publisher.Mono;
import java.io.IOException;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -206,7 +206,15 @@ public class DaprClientGrpcTest {
public void invokeServiceVoidExceptionThrownTest() {
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenThrow(RuntimeException.class);
Mono<Void> result = adapter.invokeService(Verb.GET, "appId", "method", "request");
Mono<Void> result = adapter.invokeService("appId", "method", "request", HttpExtension.NONE);
result.block();
}
@Test(expected = RuntimeException.class)
public void invokeServiceEmptyRequestVoidExceptionThrownTest() {
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenThrow(RuntimeException.class);
Mono<Void> result = adapter.invokeService("appId", "method", HttpExtension.NONE, (Map<String, String>)null);
result.block();
}
@ -220,7 +228,7 @@ public class DaprClientGrpcTest {
settableFuture.setException(ex);
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<Void> result = adapter.invokeService(Verb.GET, "appId", "method", "request");
Mono<Void> result = adapter.invokeService("appId", "method", "request", HttpExtension.NONE);
result.block();
}
@ -234,7 +242,7 @@ public class DaprClientGrpcTest {
addCallback(settableFuture, callback, directExecutor());
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<Void> result = adapter.invokeService(Verb.GET, "appId", "method", "request");
Mono<Void> result = adapter.invokeService("appId", "method", "request", HttpExtension.NONE);
settableFuture.set(CommonProtos.InvokeResponse.newBuilder().setData(getAny("Value")).build());
result.block();
assertTrue(callback.wasCalled);
@ -251,7 +259,7 @@ public class DaprClientGrpcTest {
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
MyObject request = new MyObject(1, "Event");
Mono<Void> result = adapter.invokeService(Verb.GET, "appId", "method", request);
Mono<Void> result = adapter.invokeService("appId", "method", request, HttpExtension.NONE);
settableFuture.set(CommonProtos.InvokeResponse.newBuilder().setData(getAny("Value")).build());
result.block();
assertTrue(callback.wasCalled);
@ -261,7 +269,23 @@ public class DaprClientGrpcTest {
public void invokeServiceExceptionThrownTest() {
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenThrow(RuntimeException.class);
Mono<String> result = adapter.invokeService(Verb.GET, "appId", "method", "request", null, String.class);
Mono<String> result = adapter.invokeService("appId", "method", "request", HttpExtension.NONE, null, String.class);
result.block();
}
@Test(expected = RuntimeException.class)
public void invokeServiceNoRequestClassExceptionThrownTest() {
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenThrow(RuntimeException.class);
Mono<String> result = adapter.invokeService("appId", "method", HttpExtension.NONE, (Map<String, String>)null, String.class);
result.block();
}
@Test(expected = RuntimeException.class)
public void invokeServiceNoRequestTypeRefExceptionThrownTest() {
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenThrow(RuntimeException.class);
Mono<String> result = adapter.invokeService("appId", "method", HttpExtension.NONE, (Map<String, String>)null, TypeRef.STRING);
result.block();
}
@ -274,11 +298,41 @@ public class DaprClientGrpcTest {
addCallback(settableFuture, callback, directExecutor());
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<String> result = adapter.invokeService(Verb.GET, "appId", "method", "request", null, String.class);
Mono<String> result = adapter.invokeService("appId", "method", "request", HttpExtension.NONE, null, String.class);
settableFuture.setException(ex);
result.block();
}
@Test
public void invokeServiceWithHttpExtensionTest() throws IOException {
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET, new HashMap<String, String>() {{
put("test", "1");
}});
CommonProtos.InvokeRequest message = CommonProtos.InvokeRequest.newBuilder()
.setMethod("method")
.setData(getAny("request"))
.setHttpExtension(CommonProtos.HTTPExtension.newBuilder()
.setVerb(CommonProtos.HTTPExtension.Verb.GET)
.putQuerystring("test", "1").build())
.build();
DaprProtos.InvokeServiceRequest request = DaprProtos.InvokeServiceRequest.newBuilder()
.setId("appId")
.setMessage(message)
.build();
String expected = "Value";
SettableFuture<CommonProtos.InvokeResponse> settableFuture = SettableFuture.create();
MockCallback<CommonProtos.InvokeResponse> callback =
new MockCallback<CommonProtos.InvokeResponse>(CommonProtos.InvokeResponse.newBuilder()
.setData(getAny(expected)).build());
addCallback(settableFuture, callback, directExecutor());
settableFuture.set(CommonProtos.InvokeResponse.newBuilder().setData(getAny(expected)).build());
when(client.invokeService(eq(request)))
.thenReturn(settableFuture);
Mono<String> result = adapter.invokeService("appId", "method", "request", httpExtension, null, String.class);
String strOutput = result.block();
assertEquals(expected, strOutput);
}
@Test
public void invokeServiceTest() throws Exception {
String expected = "Value";
@ -290,7 +344,7 @@ public class DaprClientGrpcTest {
settableFuture.set(CommonProtos.InvokeResponse.newBuilder().setData(getAny(expected)).build());
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<String> result = adapter.invokeService(Verb.GET, "appId", "method", "request", null, String.class);
Mono<String> result = adapter.invokeService("appId", "method", "request", HttpExtension.NONE, null, String.class);
String strOutput = result.block();
assertEquals(expected, strOutput);
}
@ -306,7 +360,7 @@ public class DaprClientGrpcTest {
settableFuture.set(CommonProtos.InvokeResponse.newBuilder().setData(getAny(object)).build());
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<MyObject> result = adapter.invokeService(Verb.GET, "appId", "method", "request", null, MyObject.class);
Mono<MyObject> result = adapter.invokeService("appId", "method", "request", HttpExtension.NONE, null, MyObject.class);
MyObject resultObject = result.block();
assertEquals(object.id, resultObject.id);
assertEquals(object.value, resultObject.value);
@ -316,7 +370,7 @@ public class DaprClientGrpcTest {
public void invokeServiceNoRequestBodyExceptionThrownTest() {
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenThrow(RuntimeException.class);
Mono<String> result = adapter.invokeService(Verb.GET, "appId", "method", null, String.class);
Mono<String> result = adapter.invokeService("appId", "method", (Object)null, HttpExtension.NONE, (Class)String.class);
result.block();
}
@ -329,7 +383,7 @@ public class DaprClientGrpcTest {
addCallback(settableFuture, callback, directExecutor());
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<String> result = adapter.invokeService(Verb.GET, "appId", "method", null, String.class);
Mono<String> result = adapter.invokeService("appId", "method", (Object)null, HttpExtension.NONE, String.class);
settableFuture.setException(ex);
result.block();
}
@ -346,7 +400,7 @@ public class DaprClientGrpcTest {
settableFuture.set(CommonProtos.InvokeResponse.newBuilder().setData(getAny(expected)).build());
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<String> result = adapter.invokeService(Verb.GET, "appId", "method", null, String.class);
Mono<String> result = adapter.invokeService("appId", "method", (Object)null, HttpExtension.NONE, String.class);
String strOutput = result.block();
assertEquals(expected, strOutput);
}
@ -363,7 +417,7 @@ public class DaprClientGrpcTest {
settableFuture.set(CommonProtos.InvokeResponse.newBuilder().setData(getAny(object)).build());
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<MyObject> result = adapter.invokeService(Verb.GET, "appId", "method", null, MyObject.class);
Mono<MyObject> result = adapter.invokeService("appId", "method", (Object)null, HttpExtension.NONE, MyObject.class);
MyObject resultObject = result.block();
assertEquals(object.id, resultObject.id);
assertEquals(object.value, resultObject.value);
@ -375,7 +429,7 @@ public class DaprClientGrpcTest {
.thenThrow(RuntimeException.class);
String request = "Request";
byte[] byteRequest = serializer.serialize(request);
Mono<byte[]> result = adapter.invokeService(Verb.GET, "appId", "method", byteRequest, byte[].class);
Mono<byte[]> result = adapter.invokeService("appId", "method", byteRequest, HttpExtension.NONE, byte[].class);
result.block();
}
@ -391,7 +445,7 @@ public class DaprClientGrpcTest {
String request = "Request";
byte[] byteRequest = serializer.serialize(request);
Mono<byte[]> result =
adapter.invokeService(Verb.GET, "appId", "method", byteRequest, (HashMap<String, String>) null);
adapter.invokeService("appId", "method", byteRequest, HttpExtension.NONE,(HashMap<String, String>) null);
settableFuture.setException(ex);
result.block();
}
@ -410,7 +464,7 @@ public class DaprClientGrpcTest {
String request = "Request";
byte[] byteRequest = serializer.serialize(request);
Mono<byte[]> result = adapter.invokeService(
Verb.GET, "appId", "method", byteRequest, (HashMap<String, String>) null);
"appId", "method", byteRequest, HttpExtension.NONE, (HashMap<String, String>) null);
byte[] byteOutput = result.block();
String strOutput = serializer.deserialize(byteOutput, String.class);
assertEquals(expected, strOutput);
@ -429,7 +483,7 @@ public class DaprClientGrpcTest {
.thenReturn(settableFuture);
String request = "Request";
byte[] byteRequest = serializer.serialize(request);
Mono<byte[]> result = adapter.invokeService(Verb.GET, "appId", "method", byteRequest, byte[].class);
Mono<byte[]> result = adapter.invokeService("appId", "method", byteRequest, HttpExtension.NONE, byte[].class);
byte[] byteOutput = result.block();
assertEquals(resultObj, serializer.deserialize(byteOutput, MyObject.class));
}
@ -438,7 +492,7 @@ public class DaprClientGrpcTest {
public void invokeServiceNoRequestNoClassBodyExceptionThrownTest() {
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenThrow(RuntimeException.class);
Mono<Void> result = adapter.invokeService(Verb.GET, "appId", "method", null);
Mono<Void> result = adapter.invokeService("appId", "method", (Object)null, HttpExtension.NONE);
result.block();
}
@ -451,7 +505,7 @@ public class DaprClientGrpcTest {
addCallback(settableFuture, callback, directExecutor());
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<Void> result = adapter.invokeService(Verb.GET, "appId", "method", null);
Mono<Void> result = adapter.invokeService("appId", "method", (Object)null, HttpExtension.NONE);
settableFuture.setException(ex);
result.block();
}
@ -466,7 +520,7 @@ public class DaprClientGrpcTest {
addCallback(settableFuture, callback, directExecutor());
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<Void> result = adapter.invokeService(Verb.GET, "appId", "method", null);
Mono<Void> result = adapter.invokeService("appId", "method", (Object)null, HttpExtension.NONE);
settableFuture.set(CommonProtos.InvokeResponse.newBuilder().setData(getAny(expected)).build());
result.block();
assertTrue(callback.wasCalled);
@ -485,7 +539,7 @@ public class DaprClientGrpcTest {
settableFuture.set(CommonProtos.InvokeResponse.newBuilder().setData(getAny(expected)).build());
return settableFuture;
});
adapter.invokeService(Verb.GET, "appId", "method", null);
adapter.invokeService("appId", "method", (Object)null, HttpExtension.NONE);
// Do not call block() on mono above, so nothing should happen.
assertFalse(callback.wasCalled);
}
@ -502,7 +556,7 @@ public class DaprClientGrpcTest {
settableFuture.set(CommonProtos.InvokeResponse.newBuilder().setData(getAny(resultObj)).build());
when(client.invokeService(any(DaprProtos.InvokeServiceRequest.class)))
.thenReturn(settableFuture);
Mono<Void> result = adapter.invokeService(Verb.GET, "appId", "method", null);
Mono<Void> result = adapter.invokeService("appId", "method", (Object)null, HttpExtension.NONE);
result.block();
assertTrue(callback.wasCalled);
}

View File

@ -6,7 +6,6 @@ package io.dapr.client;
import io.dapr.client.domain.State;
import io.dapr.client.domain.StateOptions;
import io.dapr.client.domain.Verb;
import okhttp3.OkHttpClient;
import okhttp3.mock.Behavior;
import okhttp3.mock.MockInterceptor;
@ -112,19 +111,28 @@ public class DaprClientHttpTest {
daprHttp = new DaprHttp(3000, okHttpClient);
daprClientHttp = new DaprClientHttp(daprHttp);
assertThrows(IllegalArgumentException.class, () -> {
daprClientHttp.invokeService(null, "", "", null, null, (Class)null).block();
// null HttpMethod
daprClientHttp.invokeService("1", "2", "3", new HttpExtension(null, null), null, (Class)null).block();
});
assertThrows(IllegalArgumentException.class, () -> {
daprClientHttp.invokeService(Verb.POST, null, "", null, null, (Class)null).block();
// null HttpExtension
daprClientHttp.invokeService("1", "2", "3", null, null, (Class)null).block();
});
assertThrows(IllegalArgumentException.class, () -> {
daprClientHttp.invokeService(Verb.POST, "", "", null, null, (Class)null).block();
// empty appId
daprClientHttp.invokeService("", "1", null, HttpExtension.GET, null, (Class)null).block();
});
assertThrows(IllegalArgumentException.class, () -> {
daprClientHttp.invokeService(Verb.POST, "1", null, null, null, (Class)null).block();
// null appId, empty method
daprClientHttp.invokeService(null, "", null, HttpExtension.POST, null, (Class)null).block();
});
assertThrows(IllegalArgumentException.class, () -> {
daprClientHttp.invokeService(Verb.POST, "1", "", null, null, (Class)null).block();
// empty method
daprClientHttp.invokeService("1", "", null, HttpExtension.PUT, null, (Class)null).block();
});
assertThrows(IllegalArgumentException.class, () -> {
// null method
daprClientHttp.invokeService("1", null, null, HttpExtension.DELETE, null, (Class)null).block();
});
}
@ -137,7 +145,7 @@ public class DaprClientHttpTest {
String event = "{ \"message\": \"This is a test\" }";
daprHttp = new DaprHttp(3000, okHttpClient);
daprClientHttp = new DaprClientHttp(daprHttp);
Mono<Void> mono = daprClientHttp.invokeService(Verb.POST, "1", "", null, null, (Class)null);
Mono<Void> mono = daprClientHttp.invokeService("1", "", null, HttpExtension.POST, null, (Class)null);
assertNull(mono.block());
}
@ -148,7 +156,7 @@ public class DaprClientHttpTest {
.respond("\"hello world\"");
daprHttp = new DaprHttp(3000, okHttpClient);
daprClientHttp = new DaprClientHttp(daprHttp);
Mono<String> mono = daprClientHttp.invokeService(Verb.GET, "41", "neworder", null, null, String.class);
Mono<String> mono = daprClientHttp.invokeService("41", "neworder", null, HttpExtension.GET, null, String.class);
assertEquals("hello world", mono.block());
}
@ -160,19 +168,19 @@ public class DaprClientHttpTest {
.respond(EXPECTED_RESULT);
daprHttp = new DaprHttp(3000, okHttpClient);
daprClientHttp = new DaprClientHttp(daprHttp);
Mono<byte[]> mono = daprClientHttp.invokeService(Verb.GET, "41", "neworder", null, byte[].class);
Mono<byte[]> mono = daprClientHttp.invokeService("41", "neworder", null, HttpExtension.GET, byte[].class);
assertEquals(new String(mono.block()), EXPECTED_RESULT);
}
@Test
public void invokeServiceWithMaps() {
public void invokeServiceWithMetadataMap() {
Map<String, String> map = new HashMap<>();
mockInterceptor.addRule()
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
.respond(EXPECTED_RESULT);
daprHttp = new DaprHttp(3000, okHttpClient);
daprClientHttp = new DaprClientHttp(daprHttp);
Mono<byte[]> mono = daprClientHttp.invokeService(Verb.GET, "41", "neworder", (byte[]) null, map);
Mono<byte[]> mono = daprClientHttp.invokeService("41", "neworder", (byte[]) null, HttpExtension.GET, map);
String monoString = new String(mono.block());
assertEquals(monoString, EXPECTED_RESULT);
}
@ -185,7 +193,7 @@ public class DaprClientHttpTest {
.respond(EXPECTED_RESULT);
daprHttp = new DaprHttp(3000, okHttpClient);
daprClientHttp = new DaprClientHttp(daprHttp);
Mono<Void> mono = daprClientHttp.invokeService(Verb.GET, "41", "neworder", map);
Mono<Void> mono = daprClientHttp.invokeService("41", "neworder", HttpExtension.GET, map);
assertNull(mono.block());
}
@ -197,7 +205,22 @@ public class DaprClientHttpTest {
.respond(EXPECTED_RESULT);
daprHttp = new DaprHttp(3000, okHttpClient);
daprClientHttp = new DaprClientHttp(daprHttp);
Mono<Void> mono = daprClientHttp.invokeService(Verb.GET, "41", "neworder", "", map);
Mono<Void> mono = daprClientHttp.invokeService("41", "neworder", "", HttpExtension.GET, map);
assertNull(mono.block());
}
@Test
public void invokeServiceWithRequestAndQueryString() {
Map<String, String> map = new HashMap<>();
mockInterceptor.addRule()
.get("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder?test=1")
.respond(EXPECTED_RESULT);
daprHttp = new DaprHttp(3000, okHttpClient);
daprClientHttp = new DaprClientHttp(daprHttp);
Map<String, String> queryString = new HashMap<>();
queryString.put("test", "1");
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET, queryString);
Mono<Void> mono = daprClientHttp.invokeService("41", "neworder", "", httpExtension, map);
assertNull(mono.block());
}
@ -209,7 +232,7 @@ public class DaprClientHttpTest {
.respond(500);
daprHttp = new DaprHttp(3000, okHttpClient);
daprClientHttp = new DaprClientHttp(daprHttp);
daprClientHttp.invokeService(Verb.GET, "41", "neworder", "", map);
daprClientHttp.invokeService("41", "neworder", "", HttpExtension.GET, map);
// No exception should be thrown because did not call block() on mono above.
}

View File

@ -202,15 +202,15 @@ public class DaprRuntimeTest {
eq(Constants.INVOKE_PATH + "/" + APP_ID + "/method/" + METHOD_NAME),
any(),
eq(serializer.serialize(message.data)),
eq(null)))
any()))
.thenAnswer(x ->
this.daprRuntime.handleInvocation(
METHOD_NAME,
serializer.serialize(message.data),
message.metadata)
.map(r -> new DaprHttpStub.ResponseStub(r, null, 200)));
Mono<byte[]> response = client.invokeService(Verb.POST, APP_ID, METHOD_NAME, message.data, message.metadata, byte[].class);
Mono<byte[]> response = client.invokeService(APP_ID, METHOD_NAME, message.data, HttpExtension.POST,
message.metadata, byte[].class);
Assert.assertArrayEquals(expectedResponse, response.block());
verify(listener, times(1))