added result-type option to http op fields

This commit is contained in:
Jonathan Shook 2023-10-16 13:17:19 -05:00
parent 86d445cd44
commit ac68ea5a0f
4 changed files with 43 additions and 26 deletions

View File

@ -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;
};
}
}

View File

@ -93,7 +93,7 @@ public class HttpOpDispenser extends BaseOpDispenser<HttpOp, HttpSpace> {
.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<HttpOp, HttpSpace> {
.map(Pattern::compile)
.orElse(null);
HttpResultType resultType = op.getOptionalEnumFromField(HttpResultType.class,"result-type").orElse(HttpResultType.none);
LongFunction<HttpOp> 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;
}

View File

@ -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;
}
}

View File

@ -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