Skip to content

Commit f2e93a2

Browse files
hdhayneCoveoy-lakhdar
authored andcommitted
feat: add create method in CatalogSource and PushSource (LENS-874) (#35)
1 parent ed0f754 commit f2e93a2

File tree

6 files changed

+114
-6
lines changed

6 files changed

+114
-6
lines changed

src/main/java/com/coveo/pushapiclient/CatalogSource.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
package com.coveo.pushapiclient;
22

3+
import java.io.IOException;
34
import java.net.MalformedURLException;
45
import java.net.URL;
6+
import java.net.http.HttpResponse;
57

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

13+
14+
/**
15+
* Creates a <a href='https://docs.coveo.com/en/3295'>Catalog Source</a> in Coveo Org
16+
*
17+
* @param platformClient
18+
* @param name The name of the source to create
19+
* @param sourceVisibility The security option that should be applied to the content of the source. See [Content Security](https://docs.coveo.com/en/1779).
20+
* @return
21+
* @throws IOException
22+
* @throws InterruptedException
23+
*/
24+
public static HttpResponse<String> create(PlatformClient platformClient, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
25+
return platformClient.createSource(name, SourceType.CATALOG, sourceVisibility);
26+
}
27+
1128
/**
1229
* Create a Catalog source instance from its
1330
* <a href="https://docs.coveo.com/en/3295#stream-api-url">Stream API URL</a>

src/main/java/com/coveo/pushapiclient/PlatformClient.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,37 @@ public PlatformClient(String apiKey, String organizationId, Environment environm
8080

8181
/**
8282
* Create a new push source
83+
* @deprecated
84+
* Please use {@link PlatformClient#createSource(String, SourceType, SourceVisibility)} instead
85+
*
86+
* @param name
87+
* @param sourceVisibility
88+
* @return
89+
* @throws IOException
90+
* @throws InterruptedException
91+
*/
92+
@Deprecated
93+
public HttpResponse<String> createSource(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
94+
return createSource(name,SourceType.PUSH,sourceVisibility);
95+
}
96+
97+
/**
98+
* Create a new source
8399
*
84100
* @param name The name of the source to create
101+
* @param sourceType The type of the source to create
85102
* @param sourceVisibility The security option that should be applied to the content of the source. See [Content Security](https://docs.coveo.com/en/1779).
86103
* @return
87104
* @throws IOException
88105
* @throws InterruptedException
89106
*/
90-
public HttpResponse<String> createSource(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
107+
public HttpResponse<String> createSource(String name, final SourceType sourceType, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
91108
String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
92109

93110
String json = this.toJSON(new HashMap<>() {{
94-
put("sourceType", "PUSH");
95-
put("pushEnabled", true);
111+
put("sourceType", sourceType.toString());
112+
put("pushEnabled", sourceType.isPushEnabled());
113+
put("streamEnabled", sourceType.isStreamEnabled());
96114
put("name", name);
97115
put("sourceVisibility", sourceVisibility);
98116
}});

src/main/java/com/coveo/pushapiclient/PushSource.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ public String getApiKey() {
3333
return this.apiKey;
3434
}
3535

36+
/**
37+
* Creates a <a href="https://docs.coveo.com/en/94/index-content/create-a-push-source">push Source </a> in Coveo Org
38+
*
39+
* @param platformClient
40+
* @param name
41+
* @param name The name of the source to create
42+
* @param sourceVisibility The security option that should be applied to the content of the source. See [Content Security](https://docs.coveo.com/en/1779).
43+
* @return
44+
* @throws IOException
45+
* @throws InterruptedException
46+
*/
47+
public static HttpResponse<String> create(PlatformClient platformClient, String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
48+
return platformClient.createSource(name, SourceType.PUSH, sourceVisibility);
49+
}
50+
3651
/**
3752
* Create a Push source instance from its
3853
* <a href="https://docs.coveo.com/en/1546#push-api-url">Push API URL</a>
@@ -311,4 +326,5 @@ public HttpResponse<String> deleteDocument(String documentId, Boolean deleteChil
311326
return this.platformClient.deleteDocument(this.getId(), documentId, deleteChildren);
312327
}
313328

329+
314330
}

src/main/java/com/coveo/pushapiclient/Source.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Source(String apiKey, String organizationId, Environment environment) {
5353
* @throws InterruptedException
5454
*/
5555
public HttpResponse<String> create(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
56-
return this.platformClient.createSource(name, sourceVisibility);
56+
return this.platformClient.createSource(name, SourceType.PUSH, sourceVisibility);
5757
}
5858

5959
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.coveo.pushapiclient;
2+
3+
public enum SourceType implements SourceTypeInterface{
4+
PUSH{
5+
public String toString() {
6+
return "PUSH";
7+
}
8+
public boolean isPushEnabled(){ return true;}
9+
10+
@Override
11+
public boolean isStreamEnabled() {
12+
return false;
13+
}
14+
15+
},
16+
CATALOG{
17+
public String toString() {
18+
return "CATALOG";
19+
}
20+
21+
@Override
22+
public boolean isPushEnabled() {
23+
return true;
24+
}
25+
26+
@Override
27+
public boolean isStreamEnabled() {
28+
return true;
29+
}
30+
},
31+
}
32+
33+
interface SourceTypeInterface {
34+
35+
String toString();
36+
boolean isPushEnabled();
37+
boolean isStreamEnabled();
38+
39+
}

src/test/java/com/coveo/pushapiclient/PlatformClientTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ public void setupClient() {
108108
}
109109

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

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

127+
@Test
128+
public void testCreateCatalogSource() throws IOException, InterruptedException {
129+
client.createSource("the_name", SourceType.CATALOG, SourceVisibility.SECURED);
130+
verify(httpClient).send(argument.capture(), any(HttpResponse.BodyHandlers.ofString().getClass()));
131+
132+
assertEquals("POST", argument.getValue().method());
133+
assertTrue(argument.getValue().uri().getPath().contains("the_org_id/sources"));
134+
assertAuthorizationHeader();
135+
assertApplicationJsonHeader();
136+
137+
Map requestBody = StringSubscriber.toMap(argument.getValue().bodyPublisher());
138+
assertEquals("the_name", requestBody.get("name"));
139+
assertEquals(SourceVisibility.SECURED.toString(), requestBody.get("sourceVisibility"));
140+
assertEquals("CATALOG", requestBody.get("sourceType"));
141+
assertEquals(true, requestBody.get("pushEnabled"));
142+
assertEquals(true, requestBody.get("streamEnabled"));
143+
}
144+
127145
@Test
128146
public void testCreateOrUpdateSecurityIdentity() throws IOException, InterruptedException {
129147
client.createOrUpdateSecurityIdentity("my_provider", securityIdentityModel());

0 commit comments

Comments
 (0)