Skip to content

Commit

Permalink
refactor: use jackson to read response byte array (openGemini#138)
Browse files Browse the repository at this point in the history
Signed-off-by: moxiaoying <[email protected]>
  • Loading branch information
CennyMo authored Oct 8, 2024
1 parent ab65510 commit 23f8db2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -47,6 +48,13 @@ public static <T> T toObject(@Nullable String json, Class<T> type) throws JsonPr
return MAPPER.readValue(json, type);
}

public static <T> T toObject(@Nullable byte[] json, Class<T> type) throws IOException {
if (json == null || json.length == 0) {
return null;
}
return MAPPER.readValue(json, type);
}

public static <T> T toRefer(String json, TypeReference<T> ref) throws JsonProcessingException {
if (json == null || json.isEmpty()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.opengemini.client.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.github.openfacade.http.BasicAuthRequestFilter;
import io.github.openfacade.http.HttpClient;
import io.github.openfacade.http.HttpClientConfig;
Expand Down Expand Up @@ -103,17 +102,17 @@ protected CompletableFuture<Pong> executePing() {
}

private <T> @NotNull CompletableFuture<T> convertResponse(HttpResponse response, Class<T> type) {
String body = response.bodyAsString();
if (response.statusCode() >= 200 && response.statusCode() < 300) {
try {
T resp = JacksonService.toObject(body, type);
T resp = JacksonService.toObject(response.body(), type);
return CompletableFuture.completedFuture(resp);
} catch (JsonProcessingException e) {
} catch (IOException e) {
CompletableFuture<T> future = new CompletableFuture<>();
future.completeExceptionally(e);
return future;
}
} else {
String body = response.bodyAsString();
String errorMsg = "http error: " + body;
OpenGeminiException openGeminiException = new OpenGeminiException(errorMsg, response.statusCode());
CompletableFuture<T> future = new CompletableFuture<>();
Expand Down

0 comments on commit 23f8db2

Please sign in to comment.