mirror of https://github.com/dapr/java-sdk.git
Fix input encoding and error parsing for HTTP calls. (#428)
* Fix input encoding and error parsing for HTTP calls. * Adds tests for special char in HTTP call. * Rename path to pathSegments
This commit is contained in:
parent
4c1773c8cd
commit
2714402a0e
|
|
@ -15,16 +15,6 @@ import reactor.core.publisher.Mono;
|
|||
*/
|
||||
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.
|
||||
*
|
||||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
*
|
||||
|
|
@ -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(
|
||||
|
|
@ -144,8 +119,8 @@ class DaprHttpClient implements DaprClient {
|
|||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -157,10 +132,17 @@ class DaprHttpClient implements DaprClient {
|
|||
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();
|
||||
}
|
||||
|
||||
|
|
@ -169,8 +151,15 @@ class DaprHttpClient implements DaprClient {
|
|||
*/
|
||||
@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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -184,8 +173,15 @@ class DaprHttpClient implements DaprClient {
|
|||
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();
|
||||
}
|
||||
|
||||
|
|
@ -194,8 +190,8 @@ class DaprHttpClient implements DaprClient {
|
|||
*/
|
||||
@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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
*
|
||||
|
|
@ -152,12 +117,12 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
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);
|
||||
|
|
@ -186,9 +151,11 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
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) {
|
||||
|
|
@ -254,12 +221,13 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
|
@ -287,14 +255,16 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
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));
|
||||
|
|
@ -333,19 +303,16 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
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));
|
||||
|
|
@ -375,7 +342,7 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
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) {
|
||||
|
|
@ -400,8 +367,11 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
}
|
||||
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);
|
||||
|
|
@ -423,7 +393,7 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
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) {
|
||||
|
|
@ -444,8 +414,11 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
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);
|
||||
|
|
@ -474,15 +447,17 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
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);
|
||||
|
|
@ -566,9 +541,9 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
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);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
package io.dapr.client;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.dapr.config.Properties;
|
||||
import io.dapr.exceptions.DaprError;
|
||||
|
|
@ -18,7 +19,6 @@ import okhttp3.OkHttpClient;
|
|||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.io.IOException;
|
||||
|
|
@ -153,7 +153,7 @@ public class DaprHttp implements AutoCloseable {
|
|||
* Invokes an API asynchronously without payload that returns a text payload.
|
||||
*
|
||||
* @param method HTTP method.
|
||||
* @param urlString url as String.
|
||||
* @param pathSegments Array of path segments ("/a/b/c" maps to ["a", "b", "c"]).
|
||||
* @param urlParameters URL parameters
|
||||
* @param headers HTTP headers.
|
||||
* @param context OpenTelemetry's Context.
|
||||
|
|
@ -161,18 +161,18 @@ public class DaprHttp implements AutoCloseable {
|
|||
*/
|
||||
public Mono<Response> invokeApi(
|
||||
String method,
|
||||
String urlString,
|
||||
String[] pathSegments,
|
||||
Map<String, String> urlParameters,
|
||||
Map<String, String> headers,
|
||||
Context context) {
|
||||
return this.invokeApi(method, urlString, urlParameters, (byte[]) null, headers, context);
|
||||
return this.invokeApi(method, pathSegments, urlParameters, (byte[]) null, headers, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes an API asynchronously that returns a text payload.
|
||||
*
|
||||
* @param method HTTP method.
|
||||
* @param urlString url as String.
|
||||
* @param pathSegments Array of path segments ("/a/b/c" maps to ["a", "b", "c"]).
|
||||
* @param urlParameters Parameters in the URL
|
||||
* @param content payload to be posted.
|
||||
* @param headers HTTP headers.
|
||||
|
|
@ -181,14 +181,14 @@ public class DaprHttp implements AutoCloseable {
|
|||
*/
|
||||
public Mono<Response> invokeApi(
|
||||
String method,
|
||||
String urlString,
|
||||
String[] pathSegments,
|
||||
Map<String, String> urlParameters,
|
||||
String content,
|
||||
Map<String, String> headers,
|
||||
Context context) {
|
||||
|
||||
return this.invokeApi(
|
||||
method, urlString, urlParameters, content == null
|
||||
method, pathSegments, urlParameters, content == null
|
||||
? EMPTY_BYTES
|
||||
: content.getBytes(StandardCharsets.UTF_8), headers, context);
|
||||
}
|
||||
|
|
@ -197,7 +197,7 @@ public class DaprHttp implements AutoCloseable {
|
|||
* Invokes an API asynchronously that returns a text payload.
|
||||
*
|
||||
* @param method HTTP method.
|
||||
* @param urlString url as String.
|
||||
* @param pathSegments Array of path segments ("/a/b/c" maps to ["a", "b", "c"]).
|
||||
* @param urlParameters Parameters in the URL
|
||||
* @param content payload to be posted.
|
||||
* @param headers HTTP headers.
|
||||
|
|
@ -206,12 +206,12 @@ public class DaprHttp implements AutoCloseable {
|
|||
*/
|
||||
public Mono<Response> invokeApi(
|
||||
String method,
|
||||
String urlString,
|
||||
String[] pathSegments,
|
||||
Map<String, String> urlParameters,
|
||||
byte[] content,
|
||||
Map<String, String> headers,
|
||||
Context context) {
|
||||
return Mono.fromCallable(() -> doInvokeApi(method, urlString, urlParameters, content, headers, context));
|
||||
return Mono.fromCallable(() -> doInvokeApi(method, pathSegments, urlParameters, content, headers, context));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -227,16 +227,15 @@ public class DaprHttp implements AutoCloseable {
|
|||
* Invokes an API that returns a text payload.
|
||||
*
|
||||
* @param method HTTP method.
|
||||
* @param urlString url as String.
|
||||
* @param pathSegments Array of path segments (/a/b/c -> ["a", "b", "c"]).
|
||||
* @param urlParameters Parameters in the URL
|
||||
* @param content payload to be posted.
|
||||
* @param headers HTTP headers.
|
||||
* @param context OpenTelemetry's Context.
|
||||
* @return Response
|
||||
*/
|
||||
@NotNull
|
||||
private Response doInvokeApi(String method,
|
||||
String urlString,
|
||||
String[] pathSegments,
|
||||
Map<String, String> urlParameters,
|
||||
byte[] content, Map<String, String> headers,
|
||||
Context context) throws IOException {
|
||||
|
|
@ -255,8 +254,10 @@ public class DaprHttp implements AutoCloseable {
|
|||
HttpUrl.Builder urlBuilder = new HttpUrl.Builder();
|
||||
urlBuilder.scheme(DEFAULT_HTTP_SCHEME)
|
||||
.host(this.hostname)
|
||||
.port(this.port)
|
||||
.addPathSegments(urlString);
|
||||
.port(this.port);
|
||||
for (String pathSegment : pathSegments) {
|
||||
urlBuilder.addPathSegment(pathSegment);
|
||||
}
|
||||
Optional.ofNullable(urlParameters).orElse(Collections.emptyMap()).entrySet().stream()
|
||||
.forEach(urlParameter -> urlBuilder.addQueryParameter(urlParameter.getKey(), urlParameter.getValue()));
|
||||
|
||||
|
|
@ -317,7 +318,12 @@ public class DaprHttp implements AutoCloseable {
|
|||
if ((json == null) || (json.length == 0)) {
|
||||
return null;
|
||||
}
|
||||
return OBJECT_MAPPER.readValue(json, DaprError.class);
|
||||
|
||||
try {
|
||||
return OBJECT_MAPPER.readValue(json, DaprError.class);
|
||||
} catch (JsonParseException e) {
|
||||
throw new DaprException("UNKNOWN", new String(json, StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] getBodyBytesOrEmptyArray(okhttp3.Response response) throws IOException {
|
||||
|
|
|
|||
|
|
@ -1007,8 +1007,8 @@ public class DaprClientHttpTest {
|
|||
@Test
|
||||
public void getSecrets() {
|
||||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/secrets/MySecretStore/key")
|
||||
.respond("{ \"mysecretkey\": \"mysecretvalue\"}");
|
||||
.get("http://127.0.0.1:3000/v1.0/secrets/MySecretStore/key")
|
||||
.respond("{ \"mysecretkey\": \"mysecretvalue\"}");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
|
|
@ -1020,6 +1020,22 @@ public class DaprClientHttpTest {
|
|||
assertEquals("mysecretvalue", secret.get("mysecretkey"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSecretsSpecialCharsInKey() {
|
||||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/secrets/MySecretStore/key%2Fone")
|
||||
.respond("{ \"mysecretkey\": \"mysecretvalue\"}");
|
||||
daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3000, okHttpClient);
|
||||
daprClientHttp = new DaprClientHttp(daprHttp);
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
daprClientHttp.getSecret(SECRET_STORE_NAME, null).block();
|
||||
});
|
||||
Map<String, String> secret = daprClientHttp.getSecret(SECRET_STORE_NAME, "key/one").block();
|
||||
|
||||
assertEquals(1, secret.size());
|
||||
assertEquals("mysecretvalue", secret.get("mysecretkey"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSecretsEmpty() {
|
||||
mockInterceptor.addRule()
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ public class DaprHttpStub extends DaprHttp {
|
|||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Response> invokeApi(String method, String urlString, Map<String, String> urlParameters, Map<String, String> headers, Context context) {
|
||||
public Mono<Response> invokeApi(String method, String[] pathSegments, Map<String, String> urlParameters, Map<String, String> headers, Context context) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
|
|
@ -41,7 +41,7 @@ public class DaprHttpStub extends DaprHttp {
|
|||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Response> invokeApi(String method, String urlString, Map<String, String> urlParameters, String content, Map<String, String> headers, Context context) {
|
||||
public Mono<Response> invokeApi(String method, String[] pathSegments, Map<String, String> urlParameters, String content, Map<String, String> headers, Context context) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
|
|
@ -49,7 +49,7 @@ public class DaprHttpStub extends DaprHttp {
|
|||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Mono<Response> invokeApi(String method, String urlString, Map<String, String> urlParameters, byte[] content, Map<String, String> headers, Context context) {
|
||||
public Mono<Response> invokeApi(String method, String[] pathSegments, Map<String, String> urlParameters, byte[] content, Map<String, String> headers, Context context) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,9 +22,7 @@ import java.io.IOException;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
public class DaprHttpTest {
|
||||
|
|
@ -32,6 +30,8 @@ public class DaprHttpTest {
|
|||
@Rule
|
||||
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
|
||||
|
||||
private static final String STATE_PATH = DaprHttp.API_VERSION + "/state";
|
||||
|
||||
private static final String EXPECTED_RESULT =
|
||||
"{\"data\":\"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"}";
|
||||
|
||||
|
|
@ -42,7 +42,7 @@ public class DaprHttpTest {
|
|||
private ObjectSerializer serializer = new ObjectSerializer();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
public void setUp() {
|
||||
mockInterceptor = new MockInterceptor(Behavior.UNORDERED);
|
||||
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
|
||||
}
|
||||
|
|
@ -57,7 +57,7 @@ public class DaprHttpTest {
|
|||
assertEquals("xyz", Properties.API_TOKEN.get());
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> mono =
|
||||
daprHttp.invokeApi("POST", "v1.0/state", null, (byte[]) null, null, Context.current());
|
||||
daprHttp.invokeApi("POST", "v1.0/state".split("/"), null, (byte[]) null, null, Context.current());
|
||||
DaprHttp.Response response = mono.block();
|
||||
String body = serializer.deserialize(response.getBody(), String.class);
|
||||
assertEquals(EXPECTED_RESULT, body);
|
||||
|
|
@ -73,7 +73,7 @@ public class DaprHttpTest {
|
|||
assertNull(Properties.API_TOKEN.get());
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> mono =
|
||||
daprHttp.invokeApi("POST", "v1.0/state", null, (byte[]) null, null, Context.current());
|
||||
daprHttp.invokeApi("POST", "v1.0/state".split("/"), null, (byte[]) null, null, Context.current());
|
||||
DaprHttp.Response response = mono.block();
|
||||
String body = serializer.deserialize(response.getBody(), String.class);
|
||||
assertEquals(EXPECTED_RESULT, body);
|
||||
|
|
@ -89,7 +89,7 @@ public class DaprHttpTest {
|
|||
.respond(serializer.serialize(EXPECTED_RESULT));
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> mono =
|
||||
daprHttp.invokeApi("POST", "v1.0/state", null, (byte[]) null, headers, Context.current());
|
||||
daprHttp.invokeApi("POST", "v1.0/state".split("/"), null, (byte[]) null, headers, Context.current());
|
||||
DaprHttp.Response response = mono.block();
|
||||
String body = serializer.deserialize(response.getBody(), String.class);
|
||||
assertEquals(EXPECTED_RESULT, body);
|
||||
|
|
@ -103,7 +103,7 @@ public class DaprHttpTest {
|
|||
.addHeader("Header", "Value");
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> mono =
|
||||
daprHttp.invokeApi("POST", "v1.0/state", null, "", null, Context.current());
|
||||
daprHttp.invokeApi("POST", "v1.0/state".split("/"), null, "", null, Context.current());
|
||||
DaprHttp.Response response = mono.block();
|
||||
String body = serializer.deserialize(response.getBody(), String.class);
|
||||
assertEquals(EXPECTED_RESULT, body);
|
||||
|
|
@ -116,7 +116,7 @@ public class DaprHttpTest {
|
|||
.respond(serializer.serialize(EXPECTED_RESULT));
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> mono =
|
||||
daprHttp.invokeApi("DELETE", "v1.0/state", null, (String) null, null, Context.current());
|
||||
daprHttp.invokeApi("DELETE", "v1.0/state".split("/"), null, (String) null, null, Context.current());
|
||||
DaprHttp.Response response = mono.block();
|
||||
String body = serializer.deserialize(response.getBody(), String.class);
|
||||
assertEquals(EXPECTED_RESULT, body);
|
||||
|
|
@ -128,7 +128,7 @@ public class DaprHttpTest {
|
|||
.get("http://127.0.0.1:3500/v1.0/get")
|
||||
.respond(serializer.serialize(EXPECTED_RESULT));
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> mono = daprHttp.invokeApi("GET", "v1.0/get", null, null, Context.current());
|
||||
Mono<DaprHttp.Response> mono = daprHttp.invokeApi("GET", "v1.0/get".split("/"), null, null, Context.current());
|
||||
DaprHttp.Response response = mono.block();
|
||||
String body = serializer.deserialize(response.getBody(), String.class);
|
||||
assertEquals(EXPECTED_RESULT, body);
|
||||
|
|
@ -146,7 +146,7 @@ public class DaprHttpTest {
|
|||
.respond(serializer.serialize(EXPECTED_RESULT));
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> mono =
|
||||
daprHttp.invokeApi("GET", "v1.0/state/order", urlParameters, headers, Context.current());
|
||||
daprHttp.invokeApi("GET", "v1.0/state/order".split("/"), urlParameters, headers, Context.current());
|
||||
DaprHttp.Response response = mono.block();
|
||||
String body = serializer.deserialize(response.getBody(), String.class);
|
||||
assertEquals(EXPECTED_RESULT, body);
|
||||
|
|
@ -159,7 +159,7 @@ public class DaprHttpTest {
|
|||
.respond(500);
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> mono =
|
||||
daprHttp.invokeApi("POST", "v1.0/state", null, null, Context.current());
|
||||
daprHttp.invokeApi("POST", "v1.0/state".split("/"), null, null, Context.current());
|
||||
DaprHttp.Response response = mono.block();
|
||||
String body = serializer.deserialize(response.getBody(), String.class);
|
||||
assertEquals(EXPECTED_RESULT, body);
|
||||
|
|
@ -173,7 +173,7 @@ public class DaprHttpTest {
|
|||
.respond(500, ResponseBody.create(MediaType.parse("text"),
|
||||
"{\"errorCode\":null,\"message\":null}"));
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> mono = daprHttp.invokeApi("POST", "v1.0/state", null, null, Context.current());
|
||||
Mono<DaprHttp.Response> mono = daprHttp.invokeApi("POST", "v1.0/state".split("/"), null, null, Context.current());
|
||||
DaprHttp.Response response = mono.block();
|
||||
String body = serializer.deserialize(response.getBody(), String.class);
|
||||
assertEquals(EXPECTED_RESULT, body);
|
||||
|
|
@ -186,7 +186,7 @@ public class DaprHttpTest {
|
|||
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
|
||||
"{\"errorCode\":\"null\",\"message\":\"null\"}"));
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> mono = daprHttp.invokeApi("POST", "v1.0/state", null, null, Context.current());
|
||||
Mono<DaprHttp.Response> mono = daprHttp.invokeApi("POST", "v1.0/state".split("/"), null, null, Context.current());
|
||||
DaprHttp.Response response = mono.block();
|
||||
String body = serializer.deserialize(response.getBody(), String.class);
|
||||
assertEquals(EXPECTED_RESULT, body);
|
||||
|
|
@ -215,8 +215,8 @@ public class DaprHttpTest {
|
|||
public void testCallbackCalledAtTheExpectedTimeTest() throws IOException {
|
||||
String deletedStateKey = "deletedKey";
|
||||
String existingState = "existingState";
|
||||
String urlDeleteState = DaprClientHttp.STATE_PATH + "/" + deletedStateKey;
|
||||
String urlExistingState = DaprClientHttp.STATE_PATH + "/" + existingState;
|
||||
String urlDeleteState = STATE_PATH + "/" + deletedStateKey;
|
||||
String urlExistingState = STATE_PATH + "/" + existingState;
|
||||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3500/" + urlDeleteState)
|
||||
.respond(200, ResponseBody.create(MediaType.parse("application/json"),
|
||||
|
|
@ -229,11 +229,11 @@ public class DaprHttpTest {
|
|||
.respond(200, ResponseBody.create(MediaType.parse("application/json"),
|
||||
serializer.serialize(existingState)));
|
||||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient);
|
||||
Mono<DaprHttp.Response> response = daprHttp.invokeApi("GET", urlExistingState, null, null, Context.current());
|
||||
Mono<DaprHttp.Response> response = daprHttp.invokeApi("GET", urlExistingState.split("/"), null, null, Context.current());
|
||||
assertEquals(existingState, serializer.deserialize(response.block().getBody(), String.class));
|
||||
Mono<DaprHttp.Response> responseDeleted = daprHttp.invokeApi("GET", urlDeleteState, null, null, Context.current());
|
||||
Mono<DaprHttp.Response> responseDeleted = daprHttp.invokeApi("GET", urlDeleteState.split("/"), null, null, Context.current());
|
||||
Mono<DaprHttp.Response> responseDeleteKey =
|
||||
daprHttp.invokeApi("DELETE", urlDeleteState, null, null, Context.current());
|
||||
daprHttp.invokeApi("DELETE", urlDeleteState.split("/"), null, null, Context.current());
|
||||
assertNull(serializer.deserialize(responseDeleteKey.block().getBody(), String.class));
|
||||
mockInterceptor.reset();
|
||||
mockInterceptor.addRule()
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import com.fasterxml.jackson.core.JsonGenerator;
|
|||
import io.dapr.client.DaprClient;
|
||||
import io.dapr.client.DaprClientHttp;
|
||||
import io.dapr.client.DaprClientTestBuilder;
|
||||
import io.dapr.client.DaprHttp;
|
||||
import io.dapr.client.DaprHttpStub;
|
||||
import io.dapr.client.domain.CloudEvent;
|
||||
import io.dapr.client.domain.HttpExtension;
|
||||
|
|
@ -49,6 +50,10 @@ public class DaprRuntimeTest {
|
|||
|
||||
private static final String METHOD_NAME = "mymethod";
|
||||
|
||||
private static final String INVOKE_PATH = DaprHttp.API_VERSION + "/invoke";
|
||||
|
||||
private static final String PUBLISH_PATH = DaprHttp.API_VERSION + "/publish";
|
||||
|
||||
private final DaprRuntime daprRuntime = Dapr.getInstance();
|
||||
|
||||
@Before
|
||||
|
|
@ -121,7 +126,7 @@ public class DaprRuntimeTest {
|
|||
for (Message message : messages) {
|
||||
when(daprHttp.invokeApi(
|
||||
eq("POST"),
|
||||
eq(DaprClientHttp.PUBLISH_PATH + "/" + PUBSUB_NAME + "/" + TOPIC_NAME),
|
||||
eq((PUBLISH_PATH + "/" + PUBSUB_NAME + "/" + TOPIC_NAME).split("/")),
|
||||
any(),
|
||||
eq(serializer.serialize(message.data)),
|
||||
eq(null),
|
||||
|
|
@ -209,7 +214,7 @@ public class DaprRuntimeTest {
|
|||
|
||||
when(daprHttp.invokeApi(
|
||||
eq("POST"),
|
||||
eq(DaprClientHttp.INVOKE_PATH + "/" + APP_ID + "/method/" + METHOD_NAME),
|
||||
eq((INVOKE_PATH + "/" + APP_ID + "/method/" + METHOD_NAME).split("/")),
|
||||
any(),
|
||||
eq(serializer.serialize(message.data)),
|
||||
any(),
|
||||
|
|
|
|||
Loading…
Reference in New Issue