diff --git a/src/main/java/com/coveo/pushapiclient/CatalogSource.java b/src/main/java/com/coveo/pushapiclient/CatalogSource.java index 7fc9b08a..ea723153 100644 --- a/src/main/java/com/coveo/pushapiclient/CatalogSource.java +++ b/src/main/java/com/coveo/pushapiclient/CatalogSource.java @@ -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 Catalog Source 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 create(PlatformClient platformClient, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { + return platformClient.createSource(name, SourceType.CATALOG, sourceVisibility); + } + /** * Create a Catalog source instance from its * Stream API URL diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index 7f0a1705..aa5e2035 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -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 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 createSource(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { + public HttpResponse 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); }}); diff --git a/src/main/java/com/coveo/pushapiclient/PushSource.java b/src/main/java/com/coveo/pushapiclient/PushSource.java index 4a17f9a5..b84bf354 100644 --- a/src/main/java/com/coveo/pushapiclient/PushSource.java +++ b/src/main/java/com/coveo/pushapiclient/PushSource.java @@ -33,6 +33,21 @@ public String getApiKey() { return this.apiKey; } + /** + * Creates a push Source 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 create(PlatformClient platformClient, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { + return platformClient.createSource(name, SourceType.PUSH, sourceVisibility); + } + /** * Create a Push source instance from its * Push API URL @@ -311,4 +326,5 @@ public HttpResponse deleteDocument(String documentId, Boolean deleteChil return this.platformClient.deleteDocument(this.getId(), documentId, deleteChildren); } + } diff --git a/src/main/java/com/coveo/pushapiclient/Source.java b/src/main/java/com/coveo/pushapiclient/Source.java index 2288d944..475d3e5c 100644 --- a/src/main/java/com/coveo/pushapiclient/Source.java +++ b/src/main/java/com/coveo/pushapiclient/Source.java @@ -53,7 +53,7 @@ public Source(String apiKey, String organizationId, Environment environment) { * @throws InterruptedException */ public HttpResponse create(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { - return this.platformClient.createSource(name, sourceVisibility); + return this.platformClient.createSource(name, SourceType.PUSH, sourceVisibility); } /** diff --git a/src/main/java/com/coveo/pushapiclient/SourceType.java b/src/main/java/com/coveo/pushapiclient/SourceType.java new file mode 100644 index 00000000..3251aa46 --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/SourceType.java @@ -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(); + +} diff --git a/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java b/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java index b8b3d779..20eda8af 100644 --- a/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java +++ b/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java @@ -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()); @@ -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());