diff --git a/src/main/java/com/coveo/pushapiclient/ApiCore.java b/src/main/java/com/coveo/pushapiclient/ApiCore.java new file mode 100644 index 00000000..5e07feb3 --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/ApiCore.java @@ -0,0 +1,51 @@ +package com.coveo.pushapiclient; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpRequest.BodyPublisher; +import java.net.http.HttpResponse; + +// TODO: LENS-934 - Support throttling +class ApiCore { + private final HttpClient httpClient; + + public ApiCore() { + this.httpClient = HttpClient.newHttpClient(); + } + + public ApiCore(HttpClient httpClient) { + this.httpClient = httpClient; + } + + public HttpResponse post(URI uri, String[] headers) + throws IOException, InterruptedException { + return this.post(uri, headers, HttpRequest.BodyPublishers.ofString("")); + } + + public HttpResponse post(URI uri, String[] headers, BodyPublisher body) + throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder().headers(headers).uri(uri).POST(body).build(); + return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + } + + public HttpResponse put(URI uri, String[] headers, BodyPublisher body) + throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder().headers(headers).uri(uri).PUT(body).build(); + return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + } + + public HttpResponse delete(URI uri, String[] headers) + throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder().headers(headers).uri(uri).DELETE().build(); + return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + } + + public HttpResponse delete(URI uri, String[] headers, BodyPublisher body) + throws IOException, InterruptedException { + HttpRequest request = + HttpRequest.newBuilder().headers(headers).uri(uri).method("DELETE", body).build(); + return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + } +} diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index 81a6c02a..81707f8c 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -15,7 +15,7 @@ public class PlatformClient { private final String apiKey; private final String organizationId; - private final HttpClient httpClient; + private final ApiCore api; private final PlatformUrl platformUrl; /** @@ -42,7 +42,7 @@ public PlatformClient(String apiKey, String organizationId) { public PlatformClient(String apiKey, String organizationId, PlatformUrl platformUrl) { this.apiKey = apiKey; this.organizationId = organizationId; - this.httpClient = HttpClient.newHttpClient(); + this.api = new ApiCore(); this.platformUrl = platformUrl; } @@ -58,7 +58,7 @@ public PlatformClient(String apiKey, String organizationId, PlatformUrl platform public PlatformClient(String apiKey, String organizationId, HttpClient httpClient) { this.apiKey = apiKey; this.organizationId = organizationId; - this.httpClient = httpClient; + this.api = new ApiCore(httpClient); this.platformUrl = new PlatformUrlBuilder().build(); } @@ -75,7 +75,7 @@ public PlatformClient(String apiKey, String organizationId, HttpClient httpClien public PlatformClient(String apiKey, String organizationId, Environment environment) { this.apiKey = apiKey; this.organizationId = organizationId; - this.httpClient = HttpClient.newHttpClient(); + this.api = new ApiCore(); this.platformUrl = new PlatformUrlBuilder().withEnvironment(environment).build(); } @@ -126,14 +126,9 @@ public HttpResponse createSource( } }); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .POST(HttpRequest.BodyPublishers.ofString(json)) - .uri(URI.create(this.getBaseSourceURL())) - .build(); + URI uri = URI.create(this.getBaseSourceURL()); - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.post(uri, headers, HttpRequest.BodyPublishers.ofString(json)); } /** @@ -156,14 +151,7 @@ public HttpResponse createOrUpdateSecurityIdentity( String json = new Gson().toJson(securityIdentityModel); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .PUT(HttpRequest.BodyPublishers.ofString(json)) - .uri(uri) - .build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(json)); } /** @@ -186,14 +174,7 @@ public HttpResponse createOrUpdateSecurityIdentityAlias( String json = new Gson().toJson(securityIdentityAlias); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .PUT(HttpRequest.BodyPublishers.ofString(json)) - .uri(uri) - .build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(json)); } /** @@ -215,14 +196,7 @@ public HttpResponse deleteSecurityIdentity( String json = new Gson().toJson(securityIdentityToDelete); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .method("DELETE", HttpRequest.BodyPublishers.ofString(json)) - .uri(uri) - .build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.delete(uri, headers, HttpRequest.BodyPublishers.ofString(json)); } /** @@ -240,6 +214,7 @@ public HttpResponse deleteOldSecurityIdentities( throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create( this.getBaseProviderURL(securityProviderId) @@ -247,9 +222,7 @@ public HttpResponse deleteOldSecurityIdentities( "/permissions/olderthan?queueDelay=%s%s", batchDelete.getQueueDelay(), appendOrderingId(batchDelete.getOrderingId()))); - HttpRequest request = HttpRequest.newBuilder().headers(headers).DELETE().uri(uri).build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.delete(uri, headers); } /** @@ -280,6 +253,7 @@ public HttpResponse manageSecurityIdentities( throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create( this.getBaseProviderURL(securityProviderId) @@ -287,14 +261,7 @@ public HttpResponse manageSecurityIdentities( "/permissions/batch?fileId=%s%s", batchConfig.getFileId(), appendOrderingId(batchConfig.getOrderingId()))); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .PUT(HttpRequest.BodyPublishers.noBody()) - .uri(uri) - .build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.put(uri, headers, HttpRequest.BodyPublishers.noBody()); } /** @@ -314,6 +281,7 @@ public HttpResponse pushDocument( throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create( this.getBasePushURL() @@ -321,14 +289,7 @@ public HttpResponse pushDocument( "/sources/%s/documents?documentId=%s&compressionType=%s", sourceId, documentId, compressionType.toString())); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .PUT(HttpRequest.BodyPublishers.ofString(documentJSON)) - .uri(uri) - .build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(documentJSON)); } /** @@ -348,6 +309,7 @@ public HttpResponse deleteDocument( throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create( this.getBasePushURL() @@ -355,27 +317,17 @@ public HttpResponse deleteDocument( "/sources/%s/documents?documentId=%s&deleteChildren=%s", sourceId, documentId, deleteChildren)); - HttpRequest request = HttpRequest.newBuilder().headers(headers).DELETE().uri(uri).build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.delete(uri, headers); } public HttpResponse openStream(String sourceId) throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); - // TODO: LENS-875: standardize string manipulation + URI uri = URI.create(this.getBasePushURL() + String.format("/sources/%s/stream/open", sourceId)); - // TODO: LENS-876: reduce code duplication - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .uri(uri) - .POST(HttpRequest.BodyPublishers.ofString("")) - .build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.post(uri, headers); } public HttpResponse closeStream(String sourceId, String streamId) @@ -387,33 +339,20 @@ public HttpResponse closeStream(String sourceId, String streamId) this.getBasePushURL() + String.format("/sources/%s/stream/%s/close", sourceId, streamId)); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .uri(uri) - .POST(HttpRequest.BodyPublishers.ofString("")) - .build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.post(uri, headers); } public HttpResponse requireStreamChunk(String sourceId, String streamId) throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create( this.getBasePushURL() + String.format("/sources/%s/stream/%s/chunk", sourceId, streamId)); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .uri(uri) - .POST(HttpRequest.BodyPublishers.ofString("")) - .build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.post(uri, headers); } /** @@ -427,16 +366,10 @@ public HttpResponse requireStreamChunk(String sourceId, String streamId) public HttpResponse createFileContainer() throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); - URI uri = URI.create(this.getBasePushURL() + "/files"); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .uri(uri) - .POST(HttpRequest.BodyPublishers.ofString("")) - .build(); + URI uri = URI.create(this.getBasePushURL() + "/files"); - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.post(uri, headers); } /** @@ -452,19 +385,13 @@ public HttpResponse updateSourceStatus(String sourceId, PushAPIStatus st throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create( this.getBasePushURL() + String.format("/sources/%s/status?statusType=%s", sourceId, status.toString())); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .uri(uri) - .POST(HttpRequest.BodyPublishers.ofString("")) - .build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.post(uri, headers); } /** @@ -486,16 +413,10 @@ public HttpResponse uploadContentToFileContainer( fileContainer.requiredHeaders.entrySet().stream() .flatMap(entry -> Stream.of(entry.getKey(), entry.getValue())) .toArray(String[]::new); - URI uri = URI.create(fileContainer.uploadUri); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .uri(uri) - .PUT(HttpRequest.BodyPublishers.ofString(batchUpdateJson)) - .build(); + URI uri = URI.create(fileContainer.uploadUri); - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(batchUpdateJson)); } /** @@ -520,14 +441,7 @@ public HttpResponse pushFileContainerContent(String sourceId, FileContai + String.format( "/sources/%s/documents/batch?fileId=%s", sourceId, fileContainer.fileId)); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .uri(uri) - .PUT(HttpRequest.BodyPublishers.ofString("")) - .build(); - - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString("")); } /** @@ -546,16 +460,10 @@ public HttpResponse pushBinaryToFileContainer( FileContainer fileContainer, byte[] fileAsBytes) throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAes256Header(), this.getContentTypeApplicationOctetStreamHeader()); - URI uri = URI.create(fileContainer.uploadUri); - HttpRequest request = - HttpRequest.newBuilder() - .headers(headers) - .uri(uri) - .PUT(HttpRequest.BodyPublishers.ofByteArray(fileAsBytes)) - .build(); + URI uri = URI.create(fileContainer.uploadUri); - return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofByteArray(fileAsBytes)); } private String getBaseSourceURL() {