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
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
API_KEY=[REPLACE]
ORG_ID=[REPLACE]
SOURCE_ID=[REPLACE]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target/
.env
1 change: 1 addition & 0 deletions .idea/saveactions_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/com/coveo/pushapiclient/PlatformClient.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String, Object> hashMap) {
return new Gson().toJson(hashMap, new TypeToken<HashMap<String, Object>>() {
}.getType());
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/coveo/pushapiclient/Source.java
Original file line number Diff line number Diff line change
@@ -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<String> create(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
return this.platformClient.createSource(name, sourceVisibility);
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/coveo/pushapiclient/SourceVisibility.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
}
19 changes: 18 additions & 1 deletion src/main/java/com/coveo/testlocally/TestingLocally.java
Original file line number Diff line number Diff line change
@@ -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();
}


}
}