response) throws Exception {
- String contentType = response
- .headers()
- .firstValue("Content-Type")
- .orElse("application/octet-stream");
- UpdateAuditEvidenceResponse.Builder resBuilder =
- UpdateAuditEvidenceResponse
- .builder()
- .contentType(contentType)
- .statusCode(response.statusCode())
- .rawResponse(response);
-
- UpdateAuditEvidenceResponse res = resBuilder.build();
-
- if (Utils.statusCodeMatches(response.statusCode(), "200")) {
- if (Utils.contentTypeMatches(contentType, "application/json")) {
- Evidence out = Utils.mapper().readValue(
- response.body(),
- new TypeReference<>() {
- });
- res.withEvidence(out);
- return res;
- } else {
- throw new APIException(
- response,
- response.statusCode(),
- "Unexpected content-type received: " + contentType,
- Utils.extractByteArrayFromBody(response));
- }
- }
- if (Utils.statusCodeMatches(response.statusCode(), "4XX")) {
- // no content
- throw new APIException(
- response,
- response.statusCode(),
- "API error occurred",
- Utils.extractByteArrayFromBody(response));
- }
- if (Utils.statusCodeMatches(response.statusCode(), "5XX")) {
- // no content
- throw new APIException(
- response,
- response.statusCode(),
- "API error occurred",
- Utils.extractByteArrayFromBody(response));
- }
- throw new APIException(
- response,
- response.statusCode(),
- "Unexpected status code received: " + response.statusCode(),
- Utils.extractByteArrayFromBody(response));
- }
-}
diff --git a/src/main/java/com/vanta/vanta_auditor_api/utils/HTTPClient.java b/src/main/java/com/vanta/vanta_auditor_api/utils/HTTPClient.java
index bd1d358..22120d6 100644
--- a/src/main/java/com/vanta/vanta_auditor_api/utils/HTTPClient.java
+++ b/src/main/java/com/vanta/vanta_auditor_api/utils/HTTPClient.java
@@ -6,29 +6,34 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
-import java.net.http.HttpResponse;
+import java.net.http.HttpClient;
import java.net.http.HttpRequest;
-import java.util.concurrent.CompletableFuture;
+import java.net.http.HttpResponse;
public interface HTTPClient {
+ HttpClient client = HttpClient.newHttpClient();
/**
* Sends an HTTP request and returns the response.
- *
- * Note that {@link HttpRequest} is immutable. To modify the request you can use
- * {@code HttpRequest#newBuilder(HttpRequest, BiPredicate)} with
- * JDK 16 and later (which will copy the request for modification in a builder).
- * If that method is not available then use {@link Helpers#copy} (which also returns
- * a builder).
- *
+ *
+ *
+ * Note that {@link HttpRequest} is immutable. To modify the request you can
+ * use
+ * {@code HttpRequest#newBuilder(HttpRequest, BiPredicate)}
+ * with JDK 16 and later (which will copy the request for modification in a
+ * builder). If that method is not available then use {@link Helpers#copy}
+ * (which also returns a builder).
+ *
* @param request HTTP request
* @return HTTP response
* @throws IOException
* @throws InterruptedException
* @throws URISyntaxException
- */
- HttpResponse send(HttpRequest request)
- throws IOException, InterruptedException, URISyntaxException;
+ */
+ default HttpResponse send(HttpRequest request)
+ throws IOException, InterruptedException, URISyntaxException {
+ return client.send(request, HttpResponse.BodyHandlers.ofInputStream());
+ }
/**
* Controls the debug flag that can be used by clients to perform conditional
diff --git a/src/main/java/com/vanta/vanta_auditor_api/utils/Hooks.java b/src/main/java/com/vanta/vanta_auditor_api/utils/Hooks.java
index 609acad..b9d2b36 100644
--- a/src/main/java/com/vanta/vanta_auditor_api/utils/Hooks.java
+++ b/src/main/java/com/vanta/vanta_auditor_api/utils/Hooks.java
@@ -27,7 +27,7 @@
* For example, this code will add a transaction id header to every request:
*
*
- * hooks.registerBeforeRequest((context, request) -> {
+ * hooks.registerBeforeRequest((context, request) -> {
* request.headers().map().put("acme-transaction-id", nextTransactionId());
* return request;
* });
diff --git a/src/main/java/com/vanta/vanta_auditor_api/utils/ResponseWithBody.java b/src/main/java/com/vanta/vanta_auditor_api/utils/ResponseWithBody.java
new file mode 100644
index 0000000..2dacf5c
--- /dev/null
+++ b/src/main/java/com/vanta/vanta_auditor_api/utils/ResponseWithBody.java
@@ -0,0 +1,82 @@
+/*
+ * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
+ */
+package com.vanta.vanta_auditor_api.utils;
+
+import javax.net.ssl.SSLSession;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpHeaders;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.util.Optional;
+import java.util.function.Function;
+
+/**
+ * A wrapper for {@link HttpResponse} that allows mappi ng the response body from type {@code R} to type {@code B}.
+ *
+ * This class delegates all methods to the original response, except for {@link #body()}, which returns the mapped body.
+ * The mapping is performed using the provided {@code bodyMapper} function at construction time.
+ *
+ * @param the type of the original response body
+ * @param the type of the mapped response body
+ */
+public class ResponseWithBody implements HttpResponse {
+ private final HttpResponse original;
+ private final Function bodyMapper;
+ private final B body;
+
+ /**
+ * Constructs a new {@code ResponseWithBody} by wrapping an existing {@link HttpResponse} and applying
+ * a mapping function to its body.
+ *
+ * @param original the original response to wrap
+ * @param bodyMapper a function to map the original body to the new body type
+ */
+ public ResponseWithBody(HttpResponse original, Function bodyMapper) {
+ this.original = original;
+ this.bodyMapper = bodyMapper;
+ this.body = bodyMapper.apply(original.body());
+ }
+
+ @Override
+ public int statusCode() {
+ return original.statusCode();
+ }
+
+ @Override
+ public HttpRequest request() {
+ return original.request();
+ }
+
+ @Override
+ public Optional> previousResponse() {
+ return original.previousResponse()
+ .map(prev -> new ResponseWithBody<>(prev, bodyMapper));
+ }
+
+ @Override
+ public HttpHeaders headers() {
+ return original.headers();
+ }
+
+ @Override
+ public B body() {
+ return body;
+ }
+
+ @Override
+ public Optional sslSession() {
+ return original.sslSession();
+ }
+
+ @Override
+ public URI uri() {
+ return original.uri();
+ }
+
+ @Override
+ public HttpClient.Version version() {
+ return original.version();
+ }
+}
diff --git a/src/main/java/com/vanta/vanta_auditor_api/utils/Utils.java b/src/main/java/com/vanta/vanta_auditor_api/utils/Utils.java
index 8f4a2af..17c08f6 100644
--- a/src/main/java/com/vanta/vanta_auditor_api/utils/Utils.java
+++ b/src/main/java/com/vanta/vanta_auditor_api/utils/Utils.java
@@ -52,6 +52,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
+import java.util.concurrent.CompletableFuture;
import javax.net.ssl.SSLSession;