Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/main/java/com/coveo/pushapiclient/ApiCore.java
Original file line number Diff line number Diff line change
@@ -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<String> post(URI uri, String[] headers)
throws IOException, InterruptedException {
return this.post(uri, headers, HttpRequest.BodyPublishers.ofString(""));
}

public HttpResponse<String> 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<String> 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<String> 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<String> 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());
}
}
154 changes: 31 additions & 123 deletions src/main/java/com/coveo/pushapiclient/PlatformClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -126,14 +126,9 @@ public HttpResponse<String> 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));
}

/**
Expand All @@ -156,14 +151,7 @@ public HttpResponse<String> 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));
}

/**
Expand All @@ -186,14 +174,7 @@ public HttpResponse<String> 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));
}

/**
Expand All @@ -215,14 +196,7 @@ public HttpResponse<String> 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));
}

/**
Expand All @@ -240,16 +214,15 @@ public HttpResponse<String> deleteOldSecurityIdentities(
throws IOException, InterruptedException {
String[] headers =
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());

URI uri =
URI.create(
this.getBaseProviderURL(securityProviderId)
+ String.format(
"/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);
}

/**
Expand Down Expand Up @@ -280,21 +253,15 @@ public HttpResponse<String> manageSecurityIdentities(
throws IOException, InterruptedException {
String[] headers =
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());

URI uri =
URI.create(
this.getBaseProviderURL(securityProviderId)
+ String.format(
"/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());
}

/**
Expand All @@ -314,21 +281,15 @@ public HttpResponse<String> pushDocument(
throws IOException, InterruptedException {
String[] headers =
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());

URI uri =
URI.create(
this.getBasePushURL()
+ String.format(
"/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));
}

/**
Expand All @@ -348,34 +309,25 @@ public HttpResponse<String> deleteDocument(
throws IOException, InterruptedException {
String[] headers =
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());

URI uri =
URI.create(
this.getBasePushURL()
+ String.format(
"/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<String> 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<String> closeStream(String sourceId, String streamId)
Expand All @@ -387,33 +339,20 @@ public HttpResponse<String> 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<String> 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);
}

/**
Expand All @@ -427,16 +366,10 @@ public HttpResponse<String> requireStreamChunk(String sourceId, String streamId)
public HttpResponse<String> 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);
}

/**
Expand All @@ -452,19 +385,13 @@ public HttpResponse<String> 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);
}

/**
Expand All @@ -486,16 +413,10 @@ public HttpResponse<String> 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));
}

/**
Expand All @@ -520,14 +441,7 @@ public HttpResponse<String> 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(""));
}

/**
Expand All @@ -546,16 +460,10 @@ public HttpResponse<String> 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() {
Expand Down