Skip to content

Commit

Permalink
3.2.3-SNAPSHOT
Browse files Browse the repository at this point in the history
ES API Response as Json Array... let it be wrapped in Json Object

Signed-off-by: Sinri Edogawa <[email protected]>
  • Loading branch information
sinri committed Apr 2, 2024
1 parent 85cb0bb commit d0fba90
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/main/java/io/github/sinri/keel/elasticsearch/ESApiMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
Expand Down Expand Up @@ -64,9 +66,7 @@ default Future<JsonObject> call(@Nonnull HttpMethod httpMethod, @Nonnull String
})
.compose(bufferHttpResponse -> {
int statusCode = bufferHttpResponse.statusCode();
JsonObject resp = bufferHttpResponse.bodyAsJsonObject();

if ((statusCode >= 300 || statusCode < 200) || resp == null) {
if ((statusCode >= 300 || statusCode < 200)) {
// this.getLogger().error(log -> {
// logRequestEnricher.handle(log);
// log.message("ES API Response Error")
Expand All @@ -86,6 +86,14 @@ default Future<JsonObject> call(@Nonnull HttpMethod httpMethod, @Nonnull String
requestBody
));
}
JsonObject resp;
try {
resp = bufferHttpResponse.bodyAsJsonObject();
} catch (DecodeException decodeException) {
// There are situations that use Json Array as the response body!
resp = new JsonObject()
.put("array", new JsonArray(bufferHttpResponse.bodyAsString()));
}
// this.getLogger().info(log -> {
// logRequestEnricher.handle(log);
// log.message("ES API Response Error")
Expand Down Expand Up @@ -208,6 +216,18 @@ public ESApiException(
this.requestBody = requestBody;
}

@Override
public String toString() {
return getClass().getName()
+ "{status_code: " + statusCode
+ ", response: " + response
+ ", http_method: " + httpMethod.name()
+ ", endpoint: " + endpoint
+ ", queries: " + queries
+ ", request_body: " + requestBody
+ "}";
}

public int getStatusCode() {
return statusCode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.sinri.keel.test.lab.elasticsearch;

import io.github.sinri.keel.elasticsearch.ElasticSearchKit;
import io.github.sinri.keel.tesuto.KeelTest;
import io.github.sinri.keel.tesuto.TestUnit;
import io.vertx.core.Future;
import io.vertx.core.http.HttpMethod;

import javax.annotation.Nonnull;

import static io.github.sinri.keel.facade.KeelInstance.Keel;

public class ESCatIndicesTest extends KeelTest {
@Nonnull
@Override
protected Future<Void> starting() {
Keel.getConfiguration().loadPropertiesFile("config.properties");
return super.starting();
}

@TestUnit
public Future<Void> test() {
// {"client_code":"ai-test","timestamp":1712023360984,"checksum":"d6abf7d98af34907d97f6a6578a429b5","http_method":"GET","endpoint":"/_cat/indices"}
return new ElasticSearchKit("kumori")
.call(
HttpMethod.GET,
"/_cat/indices",
null,
null
)
.compose(resp -> {
getLogger().info("resp", resp);
return Future.succeededFuture();
});
}
}

0 comments on commit d0fba90

Please sign in to comment.