Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions src/main/java/com/coveo/pushapiclient/CatalogSource.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
package com.coveo.pushapiclient;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.http.HttpResponse;

// TODO: LENS-851 - Make public when ready
class CatalogSource implements StreamEnabledSource {
private final String apiKey;
private final ApiUrl urlExtractor;


/**
* Creates a <a href='https://docs.coveo.com/en/3295/index-content/add-or-edit-a-catalog-source'>Catalog Source</a> in Coveo Org
*
* @param platformClient
* @param name
* @param sourceVisibility
* @return
* @throws IOException
* @throws InterruptedException
*/
public static HttpResponse<String> create(PlatformClient platformClient, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
return platformClient.createSource(name, SourceType.CATALOG, sourceVisibility);
}

/**
* Create a Catalog source instance from its
* <a href="https://docs.coveo.com/en/3295#stream-api-url">Stream API URL</a>
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/coveo/pushapiclient/PlatformClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,19 @@ public PlatformClient(String apiKey, String organizationId, Environment environm
* Create a new push source
*
* @param name The name of the source to create
* @param sourceType
* @param sourceVisibility The security option that should be applied to the content of the source. See [Content Security](https://docs.coveo.com/en/1779).
* @return
* @throws IOException
* @throws InterruptedException
*/
public HttpResponse<String> createSource(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
public HttpResponse<String> createSource(String name, final SourceType sourceType, 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("sourceType", sourceType.toString());
put("pushEnabled", sourceType.isPushEnabled());
put("streamEnabled", sourceType.isStreamEnabled());
put("name", name);
put("sourceVisibility", sourceVisibility);
}});
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/coveo/pushapiclient/PushSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ public String getApiKey() {
return this.apiKey;
}

/**
* Creates a <a href="https://docs.coveo.com/en/94/index-content/create-a-push-source">push Source </a> in Coveo Org
*
* @param platformClient
* @param name
* @param sourceVisibility
* @return
* @throws IOException
* @throws InterruptedException
*/
public static HttpResponse<String> create(PlatformClient platformClient, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
return platformClient.createSource(name, SourceType.PUSH, sourceVisibility);
}

/**
* Create a Push source instance from its
* <a href="https://docs.coveo.com/en/1546#push-api-url">Push API URL</a>
Expand Down Expand Up @@ -311,4 +325,5 @@ public HttpResponse<String> deleteDocument(String documentId, Boolean deleteChil
return this.platformClient.deleteDocument(this.getId(), documentId, deleteChildren);
}


}
2 changes: 1 addition & 1 deletion src/main/java/com/coveo/pushapiclient/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Source(String apiKey, String organizationId, Environment environment) {
* @throws InterruptedException
*/
public HttpResponse<String> create(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
return this.platformClient.createSource(name, sourceVisibility);
return this.platformClient.createSource(name, SourceType.PUSH, sourceVisibility);
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/coveo/pushapiclient/SourceType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.coveo.pushapiclient;

public enum SourceType implements SourceTypeInterface{
PUSH{
public String toString() {
return "PUSH";
}
public boolean isPushEnabled(){ return true;}

@Override
public boolean isStreamEnabled() {
return false;
}

},
CATALOG{
public String toString() {
return "CATALOG";
}

@Override
public boolean isPushEnabled() {
return true;
}

@Override
public boolean isStreamEnabled() {
return true;
}
},
}

interface SourceTypeInterface {

String toString();
boolean isPushEnabled();
boolean isStreamEnabled();

}
22 changes: 20 additions & 2 deletions src/test/java/com/coveo/pushapiclient/PlatformClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public void setupClient() {
}

@Test
public void testCreateSource() throws IOException, InterruptedException {
client.createSource("the_name", SourceVisibility.SECURED);
public void testCreatePushSource() throws IOException, InterruptedException {
client.createSource("the_name", SourceType.PUSH, SourceVisibility.SECURED);
verify(httpClient).send(argument.capture(), any(HttpResponse.BodyHandlers.ofString().getClass()));

assertEquals("POST", argument.getValue().method());
Expand All @@ -124,6 +124,24 @@ public void testCreateSource() throws IOException, InterruptedException {
assertEquals(true, requestBody.get("pushEnabled"));
}

@Test
public void testCreateCatalogSource() throws IOException, InterruptedException {
client.createSource("the_name", SourceType.CATALOG, SourceVisibility.SECURED);
verify(httpClient).send(argument.capture(), any(HttpResponse.BodyHandlers.ofString().getClass()));

assertEquals("POST", argument.getValue().method());
assertTrue(argument.getValue().uri().getPath().contains("the_org_id/sources"));
assertAuthorizationHeader();
assertApplicationJsonHeader();

Map requestBody = StringSubscriber.toMap(argument.getValue().bodyPublisher());
assertEquals("the_name", requestBody.get("name"));
assertEquals(SourceVisibility.SECURED.toString(), requestBody.get("sourceVisibility"));
assertEquals("CATALOG", requestBody.get("sourceType"));
assertEquals(true, requestBody.get("pushEnabled"));
assertEquals(true, requestBody.get("streamEnabled"));
}

@Test
public void testCreateOrUpdateSecurityIdentity() throws IOException, InterruptedException {
client.createOrUpdateSecurityIdentity("my_provider", securityIdentityModel());
Expand Down