From 40e348e4f0035e5c3b433aa52a2751f8ce9b696a Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Thu, 15 Jun 2023 10:50:50 -0400 Subject: [PATCH] chore: put API requests into dedicated class --- .../java/com/coveo/pushapiclient/ApiCore.java | 51 ++++++ .../coveo/pushapiclient/PlatformClient.java | 154 ++++-------------- 2 files changed, 82 insertions(+), 123 deletions(-) create mode 100644 src/main/java/com/coveo/pushapiclient/ApiCore.java 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 ba28dc78..8e6d8061 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; /** @@ -40,7 +40,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; } @@ -55,7 +55,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(); } @@ -71,7 +71,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(); } @@ -121,14 +121,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)); } /** @@ -151,14 +146,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)); } /** @@ -181,14 +169,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)); } /** @@ -210,14 +191,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)); } /** @@ -235,6 +209,7 @@ public HttpResponse deleteOldSecurityIdentities( throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create( this.getBaseProviderURL(securityProviderId) @@ -242,9 +217,7 @@ public HttpResponse deleteOldSecurityIdentities( "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/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); } /** @@ -275,6 +248,7 @@ public HttpResponse manageSecurityIdentities( throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create( this.getBaseProviderURL(securityProviderId) @@ -282,14 +256,7 @@ public HttpResponse manageSecurityIdentities( "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/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()); } /** @@ -309,6 +276,7 @@ public HttpResponse pushDocument( throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create( this.getBasePushURL() @@ -316,14 +284,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)); } /** @@ -342,6 +303,7 @@ public HttpResponse deleteDocument( throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create( this.getBasePushURL() @@ -349,27 +311,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) @@ -381,33 +333,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); } /** @@ -420,16 +359,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); } /** @@ -445,19 +378,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); } /** @@ -477,16 +404,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)); } /** @@ -509,14 +430,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("")); } /** @@ -533,16 +447,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() {