diff --git a/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpOp.java b/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpOp.java index 099975e09..de1726237 100644 --- a/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpOp.java +++ b/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpOp.java @@ -46,14 +46,24 @@ public class HttpOp implements CycleOp { private final HttpClient client; private final HttpSpace space; private final long cycle; + private final HttpResultType resultType; - public HttpOp(HttpClient client, HttpRequest request, Pattern ok_status, Pattern ok_body, HttpSpace space, long cycle) { + public HttpOp( + HttpClient client, + HttpRequest request, + Pattern ok_status, + Pattern ok_body, + HttpSpace space, + long cycle, + HttpResultType resultType + ) { this.client = client; this.request = request; this.ok_status = ok_status; this.ok_body = ok_body; this.space = space; this.cycle = cycle; + this.resultType = resultType; } @Override @@ -91,31 +101,14 @@ public class HttpOp implements CycleOp { System.out.println(); } // propogate exception so main error handling logic can take over - if (error!=null) { + if (error != null) { throw new RuntimeException(error); } } - try { - JsonParser parser = new JsonParser(); - JsonObject json = parser.parse(response.body()).getAsJsonObject(); - - if (!json.has("hits") || !json.getAsJsonObject("hits").has("hits")) { - return null; - } - JsonArray hits = json.getAsJsonObject("hits").getAsJsonArray("hits"); - - int count = hits.size(); - int[] keys = new int[count]; - int i = 0; - for (JsonElement element : hits) { - JsonObject hit = element.getAsJsonObject(); - keys[i] = hit.getAsJsonObject("_source").get("key").getAsInt(); - i++; - } - return keys; - } catch (Exception e) { - throw new RuntimeException(e); - } - + return switch (resultType) { + case string -> response.body(); + case json_element -> JsonParser.parseString(response.body()).getAsJsonObject(); + case none -> null; + }; } } diff --git a/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpOpDispenser.java b/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpOpDispenser.java index 6895a18b7..9351bf140 100644 --- a/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpOpDispenser.java +++ b/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpOpDispenser.java @@ -93,7 +93,7 @@ public class HttpOpDispenser extends BaseOpDispenser { .filter(n -> n.charAt(0) >= 'A') .filter(n -> n.charAt(0) <= 'Z') .toList(); - if (headerNames.size() > 0) { + if (!headerNames.isEmpty()) { for (String headerName : headerNames) { initBuilderF = op.enhanceFunc(initBuilderF, headerName, String.class, (b, h) -> b.header(headerName, h)); } @@ -113,12 +113,16 @@ public class HttpOpDispenser extends BaseOpDispenser { .map(Pattern::compile) .orElse(null); + HttpResultType resultType = op.getOptionalEnumFromField(HttpResultType.class,"result-type").orElse(HttpResultType.none); + LongFunction opFunc = cycle -> new HttpOp( ctxF.apply(cycle).getClient(), reqF.apply(cycle), ok_status, ok_body, - ctxF.apply(cycle), cycle + ctxF.apply(cycle), + cycle, + resultType ); return opFunc; } diff --git a/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpResultType.java b/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpResultType.java new file mode 100644 index 000000000..0b821157b --- /dev/null +++ b/adapter-http/src/main/java/io/nosqlbench/adapter/http/core/HttpResultType.java @@ -0,0 +1,15 @@ +package io.nosqlbench.adapter.http.core; + +import com.google.gson.JsonElement; + +public enum HttpResultType { + none(Void.class), + string(String.class), + json_element(JsonElement.class); + + public final Class resultClass; + + HttpResultType(Class resultClass) { + this.resultClass = resultClass; + } +} diff --git a/adapter-http/src/main/resources/http.md b/adapter-http/src/main/resources/http.md index 545ce7080..8664a443d 100644 --- a/adapter-http/src/main/resources/http.md +++ b/adapter-http/src/main/resources/http.md @@ -163,6 +163,11 @@ defaults: - **ok-body** - An optional regex pattern which will be applied to the body to verify that it is a valid response. If this is not provided, then content bodies are read, but any content is considered valid. +- **result-type** - If provided, you can specify `none`, `string`, or `json_element`. + By default, this is set to `none` and the http op will not produce a result type. + If you use string, then the raw body is returned, and if you use json_element, + then the body is presumed to be valid JSON, and it is parsed and returned as + a JsonElement. Any other statement parameter which is capitalized is taken as a request header. If additional fields are provided which are not included in the