Skip to content

Commit

Permalink
feat: add create method in CatalogSource and PushSource (LENS-874) (
Browse files Browse the repository at this point in the history
  • Loading branch information
hdhayneCoveo authored and y-lakhdar committed Jun 20, 2023
1 parent ed0f754 commit f2e93a2
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 6 deletions.
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'>Catalog Source</a> in Coveo Org
*
* @param platformClient
* @param name The name of the source to create
* @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 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
24 changes: 21 additions & 3 deletions src/main/java/com/coveo/pushapiclient/PlatformClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,37 @@ public PlatformClient(String apiKey, String organizationId, Environment environm

/**
* Create a new push source
* @deprecated
* Please use {@link PlatformClient#createSource(String, SourceType, SourceVisibility)} instead
*
* @param name
* @param sourceVisibility
* @return
* @throws IOException
* @throws InterruptedException
*/
@Deprecated
public HttpResponse<String> createSource(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
return createSource(name,SourceType.PUSH,sourceVisibility);
}

/**
* Create a new source
*
* @param name The name of the source to create
* @param sourceType The type of the source to create
* @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
16 changes: 16 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,21 @@ 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 name The name of the source to create
* @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 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 +326,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

0 comments on commit f2e93a2

Please sign in to comment.