diff --git a/.env.sample b/.env.sample
new file mode 100644
index 00000000..856c5819
--- /dev/null
+++ b/.env.sample
@@ -0,0 +1,3 @@
+API_KEY=[REPLACE]
+ORG_ID=[REPLACE]
+SOURCE_ID=[REPLACE]
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index b83d2226..5dc70ab9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
/target/
+.env
\ No newline at end of file
diff --git a/.idea/saveactions_settings.xml b/.idea/saveactions_settings.xml
index 6025467f..8a7a18ad 100644
--- a/.idea/saveactions_settings.xml
+++ b/.idea/saveactions_settings.xml
@@ -6,6 +6,7 @@
+
diff --git a/pom.xml b/pom.xml
index 74d63750..63bef1d5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,6 +13,11 @@
gson
2.8.7
+
+ io.github.cdimascio
+ dotenv-java
+ 2.2.0
+
joda-time
joda-time
diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java
new file mode 100644
index 00000000..f0aaee6a
--- /dev/null
+++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java
@@ -0,0 +1,114 @@
+package com.coveo.pushapiclient;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.stream.Stream;
+
+public class PlatformClient {
+ private final String apiKey;
+ private final String organizationId;
+ private final HttpClient httpClient;
+
+ public PlatformClient(String apiKey, String organizationId) {
+ this.apiKey = apiKey;
+ this.organizationId = organizationId;
+ this.httpClient = HttpClient.newHttpClient();
+ }
+
+ public HttpResponse createSource(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
+ String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
+
+ String json = this.toJSON(new HashMap<>() {{
+ put("sourceType", "PUSH");
+ put("pushEnabled", true);
+ put("name", name);
+ put("sourceVisibility", sourceVisibility);
+ }});
+
+ HttpRequest request = HttpRequest.newBuilder()
+ .headers(headers)
+ .POST(HttpRequest.BodyPublishers.ofString(json))
+ .uri(URI.create(this.getBaseSourceURL()))
+ .build();
+
+ return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
+ }
+
+ public void createOrUpdateSecurityIdentity(String securityProviderId, Object securityIdentityModel) {
+ // TODO
+ }
+
+ public void createOrUpdateSecurityIdentityAlias(String securityProviderId, Object securityIdentityAlias) {
+ // TODO
+ }
+
+ public void deleteSecurityIdentity(String securityProviderId, Object securityIdentityToDelete) {
+ // TODO
+ }
+
+ public void deleteOldSecurityIdentities(String securityProviderId, Object batchDelete) {
+ // TODO
+ }
+
+ public void manageSecurityIdentities(String securityProviderId, Object batchConfig) {
+ // TODO
+ }
+
+ public void pushDocument(String sourceId, Document doc) {
+ // TODO
+ }
+
+ public void deleteDocument(String sourceId, String documentId, Boolean deleteChildren) {
+ // TODO
+ }
+
+ public void createFileContainer() {
+ // TODO
+ }
+
+ public void uploadContentToFileContainer(String sourceId, Object fileContainer) {
+ // TODO
+ }
+
+ public void pushFileContainerContent(String sourceId, Object fileContainer) {
+ // TODO
+ }
+
+ private String getBaseSourceURL() {
+ return String.format("%s/sources", this.getBasePlatformURL());
+ }
+
+ private String getBasePlatformURL() {
+ return String.format("https://platform.cloud.coveo.com/rest/organizations/%s", this.organizationId);
+ }
+
+ private String[] getHeaders(String[]... headers) {
+ String[] out = new String[]{};
+ for (String[] header : headers) {
+ out = Stream.concat(Arrays.stream(out), Arrays.stream(header))
+ .toArray(String[]::new);
+ }
+ return out;
+ }
+
+ private String[] getAuthorizationHeader() {
+ return new String[]{"Authorization", String.format("Bearer %s", this.apiKey)};
+ }
+
+ private String[] getContentTypeApplicationJSONHeader() {
+ return new String[]{"Content-Type", "application/json", "Accept", "application/json"};
+ }
+
+ private String toJSON(HashMap hashMap) {
+ return new Gson().toJson(hashMap, new TypeToken>() {
+ }.getType());
+ }
+}
diff --git a/src/main/java/com/coveo/pushapiclient/Source.java b/src/main/java/com/coveo/pushapiclient/Source.java
new file mode 100644
index 00000000..656c9472
--- /dev/null
+++ b/src/main/java/com/coveo/pushapiclient/Source.java
@@ -0,0 +1,16 @@
+package com.coveo.pushapiclient;
+
+import java.io.IOException;
+import java.net.http.HttpResponse;
+
+public class Source {
+ PlatformClient platformClient;
+
+ public Source(String apiKey, String organizationId) {
+ this.platformClient = new PlatformClient(apiKey, organizationId);
+ }
+
+ public HttpResponse create(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
+ return this.platformClient.createSource(name, sourceVisibility);
+ }
+}
diff --git a/src/main/java/com/coveo/pushapiclient/SourceVisibility.java b/src/main/java/com/coveo/pushapiclient/SourceVisibility.java
new file mode 100644
index 00000000..84da38d2
--- /dev/null
+++ b/src/main/java/com/coveo/pushapiclient/SourceVisibility.java
@@ -0,0 +1,19 @@
+package com.coveo.pushapiclient;
+
+public enum SourceVisibility {
+ PRIVATE {
+ public String toString() {
+ return "PRIVATE";
+ }
+ },
+ SECURED {
+ public String toString() {
+ return "SECURED";
+ }
+ },
+ SHARED {
+ public String toString() {
+ return "SHARED";
+ }
+ }
+}
diff --git a/src/main/java/com/coveo/testlocally/TestingLocally.java b/src/main/java/com/coveo/testlocally/TestingLocally.java
index 6d5f9bce..f72a694a 100644
--- a/src/main/java/com/coveo/testlocally/TestingLocally.java
+++ b/src/main/java/com/coveo/testlocally/TestingLocally.java
@@ -1,12 +1,29 @@
package com.coveo.testlocally;
import com.coveo.pushapiclient.DocumentBuilder;
+import com.coveo.pushapiclient.Source;
+import com.coveo.pushapiclient.SourceVisibility;
+import io.github.cdimascio.dotenv.Dotenv;
+import java.io.IOException;
+import java.net.http.HttpResponse;
import java.util.Date;
public class TestingLocally {
public static void main(String[] args) {
+ Dotenv dotenv = Dotenv.load();
+
DocumentBuilder doc = new DocumentBuilder("https://perdu.com", "the title").withData("this is searchable").withDate(new Date());
- System.out.println(doc.marshal());
+ Source source = new Source(dotenv.get("API_KEY"), dotenv.get("ORG_ID"));
+ try {
+ HttpResponse res = source.create("testlocaljava", SourceVisibility.SECURED);
+ System.out.println(String.format("Status: %s %s", res.statusCode(), res.body().toString()));
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+
}
}