diff options
author | Valerij Fredriksen <valerijf@vespa.ai> | 2024-02-26 16:31:13 +0100 |
---|---|---|
committer | Valerij Fredriksen <valerijf@vespa.ai> | 2024-02-26 16:31:13 +0100 |
commit | d3d06b16994108b0ac48e885d64b906c2d922819 (patch) | |
tree | 6d9f0a4da6657a77dfe7d03160d3f9355f997ee5 | |
parent | db245fbde8ab6546520747917ab7ab5b94ec17dc (diff) |
Add method to read response as Json
-rw-r--r-- | http-client/src/main/java/ai/vespa/hosted/client/AbstractHttpClient.java | 7 | ||||
-rw-r--r-- | http-client/src/main/java/ai/vespa/hosted/client/HttpClient.java | 4 |
2 files changed, 11 insertions, 0 deletions
diff --git a/http-client/src/main/java/ai/vespa/hosted/client/AbstractHttpClient.java b/http-client/src/main/java/ai/vespa/hosted/client/AbstractHttpClient.java index 9b08919600e..f6109f3ead8 100644 --- a/http-client/src/main/java/ai/vespa/hosted/client/AbstractHttpClient.java +++ b/http-client/src/main/java/ai/vespa/hosted/client/AbstractHttpClient.java @@ -4,6 +4,8 @@ package ai.vespa.hosted.client; import ai.vespa.http.HttpURL; import ai.vespa.http.HttpURL.Path; import ai.vespa.http.HttpURL.Query; +import ai.vespa.json.Json; +import com.yahoo.slime.SlimeUtils; import com.yahoo.time.TimeBudget; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; @@ -282,6 +284,11 @@ public abstract class AbstractHttpClient implements HttpClient { } @Override + public Json readAsJson() { + return read(bytes -> Json.of(SlimeUtils.jsonToSlime(bytes))); + } + + @Override public void discard() throws UncheckedIOException, ResponseException { handle((response, __) -> { try (response) { diff --git a/http-client/src/main/java/ai/vespa/hosted/client/HttpClient.java b/http-client/src/main/java/ai/vespa/hosted/client/HttpClient.java index b764b3ad61a..57a7615421d 100644 --- a/http-client/src/main/java/ai/vespa/hosted/client/HttpClient.java +++ b/http-client/src/main/java/ai/vespa/hosted/client/HttpClient.java @@ -4,6 +4,7 @@ package ai.vespa.hosted.client; import ai.vespa.http.HttpURL; import ai.vespa.http.HttpURL.Path; import ai.vespa.http.HttpURL.Query; +import ai.vespa.json.Json; import com.yahoo.time.TimeBudget; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.core5.http.ClassicHttpRequest; @@ -140,6 +141,9 @@ public interface HttpClient extends Closeable { /** Reads and maps the response, or throws if unsuccessful. */ <T> T read(Function<byte[], T> mapper); + /** Reads the response as a {@link Json}, or throws if unsuccessful. */ + Json readAsJson(); + /** Discards the response, but throws if unsuccessful. */ void discard(); |