Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@
*/
class DaprHttpClient implements DaprClient {

/**
* Base URL for Dapr Actor APIs.
*/
private static final String ACTORS_BASE_URL = DaprHttp.API_VERSION + "/" + "actors";

/**
* String format for Actors method invocation relative url.
*/
private static final String ACTOR_METHOD_RELATIVE_URL_FORMAT = ACTORS_BASE_URL + "/%s/%s/method/%s";

/**
* The HTTP client to be used.
*
Expand All @@ -46,9 +36,9 @@ class DaprHttpClient implements DaprClient {
*/
@Override
public Mono<byte[]> invoke(String actorType, String actorId, String methodName, byte[] jsonPayload) {
String url = String.format(ACTOR_METHOD_RELATIVE_URL_FORMAT, actorType, actorId, methodName);
String[] pathSegments = new String[] { DaprHttp.API_VERSION, "actors", actorType, actorId, "method", methodName };
Mono<DaprHttp.Response> responseMono =
this.client.invokeApi(DaprHttp.HttpMethods.POST.name(), url, null, jsonPayload, null, null);
this.client.invokeApi(DaprHttp.HttpMethods.POST.name(), pathSegments, null, jsonPayload, null, null);
return responseMono.map(r -> r.getBody());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,6 @@ class DaprHttpClient implements DaprClient {
*/
private static final JsonFactory JSON_FACTORY = new JsonFactory();

/**
* Base URL for Dapr Actor APIs.
*/
private static final String ACTORS_BASE_URL = DaprHttp.API_VERSION + "/" + "actors";

/**
* String format for Actors state management relative url.
*/
private static final String ACTOR_STATE_KEY_RELATIVE_URL_FORMAT = ACTORS_BASE_URL + "/%s/%s/state/%s";

/**
* String format for Actors state management relative url.
*/
private static final String ACTOR_STATE_RELATIVE_URL_FORMAT = ACTORS_BASE_URL + "/%s/%s/state";

/**
* String format for Actors reminder registration relative url.
*/
private static final String ACTOR_REMINDER_RELATIVE_URL_FORMAT = ACTORS_BASE_URL + "/%s/%s/reminders/%s";

/**
* String format for Actors timer registration relative url.
*/
private static final String ACTOR_TIMER_RELATIVE_URL_FORMAT = ACTORS_BASE_URL + "/%s/%s/timers/%s";

/**
* The HTTP client to be used.
*
Expand All @@ -75,9 +50,9 @@ class DaprHttpClient implements DaprClient {
*/
@Override
public Mono<byte[]> getState(String actorType, String actorId, String keyName) {
String url = String.format(ACTOR_STATE_KEY_RELATIVE_URL_FORMAT, actorType, actorId, keyName);
String[] pathSegments = new String[] { DaprHttp.API_VERSION, "actors", actorType, actorId, "state", keyName };
Mono<DaprHttp.Response> responseMono =
this.client.invokeApi(DaprHttp.HttpMethods.GET.name(), url, null, "", null, null);
this.client.invokeApi(DaprHttp.HttpMethods.GET.name(), pathSegments, null, "", null, null);
return responseMono.map(r -> {
if ((r.getStatusCode() != 200) && (r.getStatusCode() != 204)) {
throw new IllegalStateException(
Expand Down Expand Up @@ -144,8 +119,8 @@ public Mono<Void> saveStateTransactionally(
return Mono.error(e);
}

String url = String.format(ACTOR_STATE_RELATIVE_URL_FORMAT, actorType, actorId);
return this.client.invokeApi(DaprHttp.HttpMethods.PUT.name(), url, null, payload, null, null).then();
String[] pathSegments = new String[] { DaprHttp.API_VERSION, "actors", actorType, actorId, "state" };
return this.client.invokeApi(DaprHttp.HttpMethods.PUT.name(), pathSegments, null, payload, null, null).then();
}

/**
Expand All @@ -157,10 +132,17 @@ public Mono<Void> registerReminder(
String actorId,
String reminderName,
ActorReminderParams reminderParams) {
String url = String.format(ACTOR_REMINDER_RELATIVE_URL_FORMAT, actorType, actorId, reminderName);
String[] pathSegments = new String[] {
DaprHttp.API_VERSION,
"actors",
actorType,
actorId,
"reminders",
reminderName
};
return Mono.fromCallable(() -> INTERNAL_SERIALIZER.serialize(reminderParams))
.flatMap(data ->
this.client.invokeApi(DaprHttp.HttpMethods.PUT.name(), url, null, data, null, null)
this.client.invokeApi(DaprHttp.HttpMethods.PUT.name(), pathSegments, null, data, null, null)
).then();
}

Expand All @@ -169,8 +151,15 @@ public Mono<Void> registerReminder(
*/
@Override
public Mono<Void> unregisterReminder(String actorType, String actorId, String reminderName) {
String url = String.format(ACTOR_REMINDER_RELATIVE_URL_FORMAT, actorType, actorId, reminderName);
return this.client.invokeApi(DaprHttp.HttpMethods.DELETE.name(), url, null, null, null).then();
String[] pathSegments = new String[] {
DaprHttp.API_VERSION,
"actors",
actorType,
actorId,
"reminders",
reminderName
};
return this.client.invokeApi(DaprHttp.HttpMethods.DELETE.name(), pathSegments, null, null, null).then();
}

/**
Expand All @@ -184,8 +173,15 @@ public Mono<Void> registerTimer(
ActorTimerParams timerParams) {
return Mono.fromCallable(() -> INTERNAL_SERIALIZER.serialize(timerParams))
.flatMap(data -> {
String url = String.format(ACTOR_TIMER_RELATIVE_URL_FORMAT, actorType, actorId, timerName);
return this.client.invokeApi(DaprHttp.HttpMethods.PUT.name(), url, null, data, null, null);
String[] pathSegments = new String[] {
DaprHttp.API_VERSION,
"actors",
actorType,
actorId,
"timers",
timerName
};
return this.client.invokeApi(DaprHttp.HttpMethods.PUT.name(), pathSegments, null, data, null, null);
}).then();
}

Expand All @@ -194,8 +190,8 @@ public Mono<Void> registerTimer(
*/
@Override
public Mono<Void> unregisterTimer(String actorType, String actorId, String timerName) {
String url = String.format(ACTOR_TIMER_RELATIVE_URL_FORMAT, actorType, actorId, timerName);
return this.client.invokeApi(DaprHttp.HttpMethods.DELETE.name(), url, null, null, null).then();
String[] pathSegments = new String[] { DaprHttp.API_VERSION, "actors", actorType, actorId, "timers", timerName };
return this.client.invokeApi(DaprHttp.HttpMethods.DELETE.name(), pathSegments, null, null, null).then();
}

}
95 changes: 35 additions & 60 deletions sdk/src/main/java/io/dapr/client/DaprClientHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,6 @@ public class DaprClientHttp extends AbstractDaprClient {
*/
private static final ObjectSerializer INTERNAL_SERIALIZER = new ObjectSerializer();

/**
* Base path to invoke methods.
*/
public static final String INVOKE_PATH = DaprHttp.API_VERSION + "/invoke";

/**
* Invoke Publish Path.
*/
public static final String PUBLISH_PATH = DaprHttp.API_VERSION + "/publish";

/**
* Invoke Binding Path.
*/
public static final String BINDING_PATH = DaprHttp.API_VERSION + "/bindings";

/**
* State Path.
*/
public static final String STATE_PATH = DaprHttp.API_VERSION + "/state";

/**
* String format for transaction API.
*/
private static final String TRANSACTION_URL_FORMAT = STATE_PATH + "/%s/transaction";

/**
* Secrets Path.
*/
public static final String SECRETS_PATH = DaprHttp.API_VERSION + "/secrets";

/**
* State Path format for bulk state API.
*/
public static final String STATE_BULK_PATH_FORMAT = STATE_PATH + "/%s/bulk";

/**
* The HTTP client to be used.
*
Expand Down Expand Up @@ -152,12 +117,12 @@ public Mono<Response<Void>> publishEvent(PublishEventRequest request) {
throw new IllegalArgumentException("Topic name cannot be null or empty.");
}

StringBuilder url = new StringBuilder(PUBLISH_PATH)
.append("/").append(pubsubName)
.append("/").append(topic);
byte[] serializedEvent = objectSerializer.serialize(data);

String[] pathSegments = new String[]{ DaprHttp.API_VERSION, "publish", pubsubName, topic };

return this.client.invokeApi(
DaprHttp.HttpMethods.POST.name(), url.toString(), null, serializedEvent, metadata, context)
DaprHttp.HttpMethods.POST.name(), pathSegments, null, serializedEvent, metadata, context)
.thenReturn(new Response<>(context, null));
} catch (Exception ex) {
return DaprException.wrapMono(ex);
Expand Down Expand Up @@ -186,9 +151,11 @@ public <T> Mono<Response<T>> invokeMethod(InvokeServiceRequest invokeServiceRequ
if (method == null || method.trim().isEmpty()) {
throw new IllegalArgumentException("Method name cannot be null or empty.");
}
String path = String.format("%s/%s/method/%s", INVOKE_PATH, appId, method);
byte[] serializedRequestBody = objectSerializer.serialize(request);
Mono<DaprHttp.Response> response = this.client.invokeApi(httMethod, path,

String[] pathSegments = new String[]{ DaprHttp.API_VERSION, "invoke", appId, "method", method };

Mono<DaprHttp.Response> response = this.client.invokeApi(httMethod, pathSegments,
httpExtension.getQueryString(), serializedRequestBody, metadata, context);
return response.flatMap(r -> getMono(type, r)).map(r -> new Response<>(context, r));
} catch (Exception ex) {
Expand Down Expand Up @@ -254,12 +221,13 @@ public <T> Mono<Response<T>> invokeBinding(InvokeBindingRequest request, TypeRef
}
}

StringBuilder url = new StringBuilder(BINDING_PATH).append("/").append(name);

byte[] payload = INTERNAL_SERIALIZER.serialize(jsonMap);
String httpMethod = DaprHttp.HttpMethods.POST.name();

String[] pathSegments = new String[]{ DaprHttp.API_VERSION, "bindings", name };

Mono<DaprHttp.Response> response = this.client.invokeApi(
httpMethod, url.toString(), null, payload, null, context);
httpMethod, pathSegments, null, payload, null, context);
return response.flatMap(r -> getMono(type, r)).map(r -> new Response<>(context, r));
} catch (Exception ex) {
return DaprException.wrapMono(ex);
Expand Down Expand Up @@ -287,14 +255,16 @@ public <T> Mono<Response<List<State<T>>>> getBulkState(GetBulkStateRequest reque
throw new IllegalArgumentException("Parallelism cannot be negative.");
}

String url = String.format(STATE_BULK_PATH_FORMAT, stateStoreName);
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("keys", keys);
jsonMap.put("parallelism", parallelism);

byte[] requestBody = INTERNAL_SERIALIZER.serialize(jsonMap);

String[] pathSegments = new String[]{ DaprHttp.API_VERSION, "state", stateStoreName, "bulk"};

return this.client
.invokeApi(DaprHttp.HttpMethods.POST.name(), url, null, requestBody, null, context)
.invokeApi(DaprHttp.HttpMethods.POST.name(), pathSegments, null, requestBody, null, context)
.flatMap(s -> {
try {
return Mono.just(buildStates(s, type));
Expand Down Expand Up @@ -333,19 +303,16 @@ public <T> Mono<Response<State<T>>> getState(GetStateRequest request, TypeRef<T>
headers.put(HEADER_HTTP_ETAG_ID, etag);
}

StringBuilder url = new StringBuilder(STATE_PATH)
.append("/")
.append(stateStoreName)
.append("/")
.append(key);
Map<String, String> urlParameters = Optional.ofNullable(options)
.map(o -> o.getStateOptionsAsMap())
.orElse(new HashMap<>());

request.getMetadata().forEach(urlParameters::put);

String[] pathSegments = new String[]{ DaprHttp.API_VERSION, "state", stateStoreName, key};

return this.client
.invokeApi(DaprHttp.HttpMethods.GET.name(), url.toString(), urlParameters, headers, context)
.invokeApi(DaprHttp.HttpMethods.GET.name(), pathSegments, urlParameters, headers, context)
.flatMap(s -> {
try {
return Mono.just(buildState(s, key, options, type));
Expand Down Expand Up @@ -375,7 +342,7 @@ public Mono<Response<Void>> executeStateTransaction(ExecuteStateTransactionReque
if (operations == null || operations.isEmpty()) {
return Mono.empty();
}
final String url = String.format(TRANSACTION_URL_FORMAT, stateStoreName);

List<TransactionalStateOperation<Object>> internalOperationObjects = new ArrayList<>(operations.size());
for (TransactionalStateOperation operation : operations) {
if (operation == null) {
Expand All @@ -400,8 +367,11 @@ public Mono<Response<Void>> executeStateTransaction(ExecuteStateTransactionReque
}
TransactionalStateRequest<Object> req = new TransactionalStateRequest<>(internalOperationObjects, metadata);
byte[] serializedOperationBody = INTERNAL_SERIALIZER.serialize(req);

String[] pathSegments = new String[]{ DaprHttp.API_VERSION, "state", stateStoreName, "transaction"};

return this.client.invokeApi(
DaprHttp.HttpMethods.POST.name(), url, null, serializedOperationBody, null, context)
DaprHttp.HttpMethods.POST.name(), pathSegments, null, serializedOperationBody, null, context)
.thenReturn(new Response<>(context, null));
} catch (Exception e) {
return DaprException.wrapMono(e);
Expand All @@ -423,7 +393,7 @@ public Mono<Response<Void>> saveBulkState(SaveStateRequest request) {
if (states == null || states.isEmpty()) {
return Mono.empty();
}
final String url = STATE_PATH + "/" + stateStoreName;

List<State<Object>> internalStateObjects = new ArrayList<>(states.size());
for (State state : states) {
if (state == null) {
Expand All @@ -444,8 +414,11 @@ public Mono<Response<Void>> saveBulkState(SaveStateRequest request) {
state.getOptions()));
}
byte[] serializedStateBody = INTERNAL_SERIALIZER.serialize(internalStateObjects);

String[] pathSegments = new String[]{ DaprHttp.API_VERSION, "state", stateStoreName};

return this.client.invokeApi(
DaprHttp.HttpMethods.POST.name(), url, null, serializedStateBody, null, context)
DaprHttp.HttpMethods.POST.name(), pathSegments, null, serializedStateBody, null, context)
.thenReturn(new Response<>(context, null));
} catch (Exception ex) {
return DaprException.wrapMono(ex);
Expand Down Expand Up @@ -474,15 +447,17 @@ public Mono<Response<Void>> deleteState(DeleteStateRequest request) {
if (etag != null && !etag.trim().isEmpty()) {
headers.put(HEADER_HTTP_ETAG_ID, etag);
}
String url = STATE_PATH + "/" + stateStoreName + "/" + key;

Map<String, String> urlParameters = Optional.ofNullable(options)
.map(stateOptions -> stateOptions.getStateOptionsAsMap())
.orElse(new HashMap<>());

request.getMetadata().forEach(urlParameters::put);

String[] pathSegments = new String[]{ DaprHttp.API_VERSION, "state", stateStoreName, key};

return this.client.invokeApi(
DaprHttp.HttpMethods.DELETE.name(), url, urlParameters, headers, context)
DaprHttp.HttpMethods.DELETE.name(), pathSegments, urlParameters, headers, context)
.thenReturn(new Response<>(context, null));
} catch (Exception ex) {
return DaprException.wrapMono(ex);
Expand Down Expand Up @@ -566,9 +541,9 @@ public Mono<Response<Map<String, String>>> getSecret(GetSecretRequest request) {
return DaprException.wrapMono(e);
}

String url = SECRETS_PATH + "/" + secretStoreName + "/" + key;
String[] pathSegments = new String[]{ DaprHttp.API_VERSION, "secrets", secretStoreName, key};
return this.client
.invokeApi(DaprHttp.HttpMethods.GET.name(), url, metadata, (String)null, null, context)
.invokeApi(DaprHttp.HttpMethods.GET.name(), pathSegments, metadata, (String)null, null, context)
.flatMap(response -> {
try {
Map m = INTERNAL_SERIALIZER.deserialize(response.getBody(), Map.class);
Expand Down
Loading