From 93eef65f734484a6bc670e83194d800730030643 Mon Sep 17 00:00:00 2001 From: Houssein Date: Tue, 30 May 2023 17:02:17 -0400 Subject: [PATCH 1/8] LENS-874 add create method in CatalogSource and PushSource --- .../coveo/pushapiclient/CatalogSource.java | 18 +++++++++++++++ .../coveo/pushapiclient/PlatformClient.java | 10 ++++++--- .../com/coveo/pushapiclient/PushSource.java | 17 ++++++++++++++ .../java/com/coveo/pushapiclient/Source.java | 2 +- .../com/coveo/pushapiclient/SourceType.java | 6 +++++ .../pushapiclient/PlatformClientTest.java | 22 +++++++++++++++++-- 6 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/coveo/pushapiclient/SourceType.java diff --git a/src/main/java/com/coveo/pushapiclient/CatalogSource.java b/src/main/java/com/coveo/pushapiclient/CatalogSource.java index 7fc9b08a..8e5c9bf4 100644 --- a/src/main/java/com/coveo/pushapiclient/CatalogSource.java +++ b/src/main/java/com/coveo/pushapiclient/CatalogSource.java @@ -1,5 +1,6 @@ package com.coveo.pushapiclient; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -8,6 +9,23 @@ class CatalogSource implements StreamEnabledSource { private final String apiKey; private final ApiUrl urlExtractor; + + /** + * Creates a Catalog Source in Coveo Org + * + * @param platformUrl + * @param organizationId + * @param apiKey + * @param name + * @param sourceVisibility + * @return + * @throws IOException + * @throws InterruptedException + */ + public static void create(PlatformUrl platformUrl, String organizationId, String apiKey, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { + new PlatformClient(apiKey,organizationId,platformUrl).createSource(name, SourceType.CATALOG.name(), true, true, 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 f54aca7b..dc761921 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -82,17 +82,21 @@ 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 isPushEnabled + * @param isStreamEnabled * @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 String sourceType, final boolean isPushEnabled, final boolean isStreamEnabled, 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); + put("pushEnabled", isPushEnabled); + put("streamEnabled", 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..6aedcabe 100644 --- a/src/main/java/com/coveo/pushapiclient/PushSource.java +++ b/src/main/java/com/coveo/pushapiclient/PushSource.java @@ -33,6 +33,22 @@ public String getApiKey() { return this.apiKey; } + /** + * Creates a push Source in Coveo Org + * + * @param platformUrl + * @param organizationId + * @param apiKey + * @param name + * @param sourceVisibility + * @return + * @throws IOException + * @throws InterruptedException + */ + public static void create(PlatformUrl platformUrl, String organizationId, String apiKey, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { + new PlatformClient(apiKey,organizationId,platformUrl).createSource(name, SourceType.PUSH.name(), true, false, sourceVisibility); + } + /** * Create a Push source instance from its * Push API URL @@ -311,4 +327,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..002f280f 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.name(), true, false, 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..297609a7 --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/SourceType.java @@ -0,0 +1,6 @@ +package com.coveo.pushapiclient; + +public enum SourceType { + PUSH, + CATALOG, +} diff --git a/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java b/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java index 8acf472b..9b039ca1 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.name(), true, false, 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.name(), true, true, 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()); From 28a68696257ef5bda3dd28c4495f7e584136f222 Mon Sep 17 00:00:00 2001 From: Houssein Date: Wed, 31 May 2023 14:32:58 -0400 Subject: [PATCH 2/8] LENS-874 apply review comments --- .../coveo/pushapiclient/CatalogSource.java | 11 +++--- .../coveo/pushapiclient/PlatformClient.java | 10 ++--- .../com/coveo/pushapiclient/PushSource.java | 10 ++--- .../java/com/coveo/pushapiclient/Source.java | 2 +- .../com/coveo/pushapiclient/SourceType.java | 39 +++++++++++++++++-- .../pushapiclient/PlatformClientTest.java | 4 +- 6 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/coveo/pushapiclient/CatalogSource.java b/src/main/java/com/coveo/pushapiclient/CatalogSource.java index 8e5c9bf4..96e09685 100644 --- a/src/main/java/com/coveo/pushapiclient/CatalogSource.java +++ b/src/main/java/com/coveo/pushapiclient/CatalogSource.java @@ -3,6 +3,7 @@ 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 { @@ -11,19 +12,17 @@ class CatalogSource implements StreamEnabledSource { /** - * Creates a Catalog Source in Coveo Org + * Creates a Catalog Source in Coveo Org * - * @param platformUrl - * @param organizationId - * @param apiKey + * @param platformClient * @param name * @param sourceVisibility * @return * @throws IOException * @throws InterruptedException */ - public static void create(PlatformUrl platformUrl, String organizationId, String apiKey, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { - new PlatformClient(apiKey,organizationId,platformUrl).createSource(name, SourceType.CATALOG.name(), true, true, sourceVisibility); + public static HttpResponse create(PlatformClient platformClient, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { + return platformClient.createSource(name, SourceType.CATALOG, sourceVisibility); } /** diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index dc761921..e95e9dca 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -83,20 +83,18 @@ public PlatformClient(String apiKey, String organizationId, Environment environm * * @param name The name of the source to create * @param sourceType - * @param isPushEnabled - * @param isStreamEnabled * @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, final String sourceType, final boolean isPushEnabled, final boolean isStreamEnabled, 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", sourceType); - put("pushEnabled", isPushEnabled); - put("streamEnabled", isStreamEnabled); + 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 6aedcabe..4c5af859 100644 --- a/src/main/java/com/coveo/pushapiclient/PushSource.java +++ b/src/main/java/com/coveo/pushapiclient/PushSource.java @@ -34,19 +34,17 @@ public String getApiKey() { } /** - * Creates a push Source in Coveo Org + * Creates a push Source in Coveo Org * - * @param platformUrl - * @param organizationId - * @param apiKey + * @param platformClient * @param name * @param sourceVisibility * @return * @throws IOException * @throws InterruptedException */ - public static void create(PlatformUrl platformUrl, String organizationId, String apiKey, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { - new PlatformClient(apiKey,organizationId,platformUrl).createSource(name, SourceType.PUSH.name(), true, false, sourceVisibility); + public static HttpResponse create(PlatformClient platformClient, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException { + return platformClient.createSource(name, SourceType.PUSH, sourceVisibility); } /** diff --git a/src/main/java/com/coveo/pushapiclient/Source.java b/src/main/java/com/coveo/pushapiclient/Source.java index 002f280f..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, SourceType.PUSH.name(), true, false, 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 index 297609a7..3251aa46 100644 --- a/src/main/java/com/coveo/pushapiclient/SourceType.java +++ b/src/main/java/com/coveo/pushapiclient/SourceType.java @@ -1,6 +1,39 @@ package com.coveo.pushapiclient; -public enum SourceType { - PUSH, - CATALOG, +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 9b039ca1..3041806e 100644 --- a/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java +++ b/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java @@ -109,7 +109,7 @@ public void setupClient() { @Test public void testCreatePushSource() throws IOException, InterruptedException { - client.createSource("the_name", SourceType.PUSH.name(), true, false, SourceVisibility.SECURED); + client.createSource("the_name", SourceType.PUSH, SourceVisibility.SECURED); verify(httpClient).send(argument.capture(), any(HttpResponse.BodyHandlers.ofString().getClass())); assertEquals("POST", argument.getValue().method()); @@ -126,7 +126,7 @@ public void testCreatePushSource() throws IOException, InterruptedException { @Test public void testCreateCatalogSource() throws IOException, InterruptedException { - client.createSource("the_name", SourceType.CATALOG.name(), true, true, SourceVisibility.SECURED); + client.createSource("the_name", SourceType.CATALOG, SourceVisibility.SECURED); verify(httpClient).send(argument.capture(), any(HttpResponse.BodyHandlers.ofString().getClass())); assertEquals("POST", argument.getValue().method()); From 7fa203430ab01b28c284ffa8026793aa6f24ca30 Mon Sep 17 00:00:00 2001 From: Houssein Dhayne <95109658+hdhayneCoveo@users.noreply.github.com> Date: Thu, 1 Jun 2023 10:22:25 -0400 Subject: [PATCH 3/8] Update src/main/java/com/coveo/pushapiclient/CatalogSource.java Co-authored-by: Yassine --- src/main/java/com/coveo/pushapiclient/CatalogSource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/coveo/pushapiclient/CatalogSource.java b/src/main/java/com/coveo/pushapiclient/CatalogSource.java index 96e09685..b99eeb25 100644 --- a/src/main/java/com/coveo/pushapiclient/CatalogSource.java +++ b/src/main/java/com/coveo/pushapiclient/CatalogSource.java @@ -12,7 +12,7 @@ class CatalogSource implements StreamEnabledSource { /** - * Creates a Catalog Source in Coveo Org + * Creates a Catalog Source in Coveo Org * * @param platformClient * @param name From 144aed1c5ca2715d77f393859fc3b21ec2513836 Mon Sep 17 00:00:00 2001 From: Houssein Dhayne <95109658+hdhayneCoveo@users.noreply.github.com> Date: Thu, 1 Jun 2023 10:49:08 -0400 Subject: [PATCH 4/8] Update src/main/java/com/coveo/pushapiclient/PushSource.java Co-authored-by: Yassine --- src/main/java/com/coveo/pushapiclient/PushSource.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/coveo/pushapiclient/PushSource.java b/src/main/java/com/coveo/pushapiclient/PushSource.java index 4c5af859..b84bf354 100644 --- a/src/main/java/com/coveo/pushapiclient/PushSource.java +++ b/src/main/java/com/coveo/pushapiclient/PushSource.java @@ -38,7 +38,8 @@ public String getApiKey() { * * @param platformClient * @param name - * @param sourceVisibility + * @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 From 5267f5d14068427283a4e1f9f7b9ad732639d8c4 Mon Sep 17 00:00:00 2001 From: Houssein Dhayne <95109658+hdhayneCoveo@users.noreply.github.com> Date: Thu, 1 Jun 2023 10:50:06 -0400 Subject: [PATCH 5/8] Update src/main/java/com/coveo/pushapiclient/PlatformClient.java Co-authored-by: Yassine --- src/main/java/com/coveo/pushapiclient/PlatformClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index 98248b38..258f3576 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -82,7 +82,7 @@ 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 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 From 7a47046de7ed11e8132c2145a06cb4ed32ebffc3 Mon Sep 17 00:00:00 2001 From: Houssein Date: Thu, 1 Jun 2023 11:10:03 -0400 Subject: [PATCH 6/8] LENS-874 apply review comments , re-add the old createSource method with deprecated note --- .../com/coveo/pushapiclient/PlatformClient.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index 258f3576..aa5e2035 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -80,6 +80,22 @@ 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 From b7cd85823305a6973e5fc3c66d24017fdc14f466 Mon Sep 17 00:00:00 2001 From: Houssein Dhayne <95109658+hdhayneCoveo@users.noreply.github.com> Date: Thu, 1 Jun 2023 13:41:43 -0400 Subject: [PATCH 7/8] Update src/main/java/com/coveo/pushapiclient/CatalogSource.java Co-authored-by: Yassine --- src/main/java/com/coveo/pushapiclient/CatalogSource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/coveo/pushapiclient/CatalogSource.java b/src/main/java/com/coveo/pushapiclient/CatalogSource.java index b99eeb25..f9643536 100644 --- a/src/main/java/com/coveo/pushapiclient/CatalogSource.java +++ b/src/main/java/com/coveo/pushapiclient/CatalogSource.java @@ -16,7 +16,7 @@ class CatalogSource implements StreamEnabledSource { * * @param platformClient * @param name - * @param sourceVisibility + * @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 From 75e88f6fe4a1210118815abf88783ce483322aa3 Mon Sep 17 00:00:00 2001 From: Houssein Dhayne <95109658+hdhayneCoveo@users.noreply.github.com> Date: Thu, 1 Jun 2023 13:41:49 -0400 Subject: [PATCH 8/8] Update src/main/java/com/coveo/pushapiclient/CatalogSource.java Co-authored-by: Yassine --- src/main/java/com/coveo/pushapiclient/CatalogSource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/coveo/pushapiclient/CatalogSource.java b/src/main/java/com/coveo/pushapiclient/CatalogSource.java index f9643536..ea723153 100644 --- a/src/main/java/com/coveo/pushapiclient/CatalogSource.java +++ b/src/main/java/com/coveo/pushapiclient/CatalogSource.java @@ -15,7 +15,7 @@ class CatalogSource implements StreamEnabledSource { * Creates a Catalog 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