Skip to content

Commit aee3434

Browse files
authored
feat: enable push on catalog source (#82)
https://coveord.atlassian.net/browse/CDX-1464
1 parent fd7372e commit aee3434

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

samples/StreamDocuments.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ public static void main(String[] args) throws IOException, InterruptedException,
1111
PlatformUrl platformUrl = new PlatformUrlBuilder().withEnvironment(Environment.PRODUCTION).withRegion(Region.US).build();
1212
CatalogSource catalogSource = CatalogSource.fromPlatformUrl("my_api_key","my_org_id","my_source_id", platformUrl);
1313

14+
// Using the Stream Service will act as a source rebuild, therefore any currently indexed items not contained in the payload will be deleted.
1415
StreamService streamService = new StreamService(catalogSource);
16+
// To perform full document updates, use the PushService instead.
17+
// For more info, visit: https://docs.coveo.com/en/l62e0540/coveo-for-commerce/how-to-update-your-catalog#full-document-updates
1518

1619
DocumentBuilder document1 = new DocumentBuilder("https://my.document.uri", "My document title")
1720
.withData("these words will be searchable")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.net.URL;
66
import java.net.http.HttpResponse;
77

8-
public class CatalogSource implements StreamEnabledSource {
8+
public class CatalogSource implements StreamEnabledSource, PushEnabledSource {
99
private final String apiKey;
1010
private final ApiUrl urlExtractor;
1111

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,29 @@ public HttpResponse<String> pushFileContainerContent(String sourceId, FileContai
444444
return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(""));
445445
}
446446

447+
/**
448+
* Push a file container into a stream source. See [Push the File Container into a Stream
449+
* Source](https://docs.coveo.com/en/l62e0540/coveo-for-commerce/how-to-update-your-catalog#step-3-send-the-file-container-to-update-your-catalog).
450+
*
451+
* @param sourceId
452+
* @param fileContainer
453+
* @return
454+
* @throws IOException
455+
* @throws InterruptedException
456+
*/
457+
public HttpResponse<String> pushFileContainerContentToStreamSource(
458+
String sourceId, FileContainer fileContainer) throws IOException, InterruptedException {
459+
String[] headers =
460+
this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
461+
URI uri =
462+
URI.create(
463+
this.getBasePushURL()
464+
+ String.format(
465+
"/sources/%s/stream/update?fileId=%s", sourceId, fileContainer.fileId));
466+
467+
return this.api.put(uri, headers, HttpRequest.BodyPublishers.ofString(""));
468+
}
469+
447470
/**
448471
* Push a binary to a File Container.
449472
*

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,25 @@ public void testPushFileContainerContent() throws IOException, InterruptedExcept
387387
assertAuthorizationHeader();
388388
}
389389

390+
@Test
391+
public void testPushFileContainerContentToStream() throws IOException, InterruptedException {
392+
client.pushFileContainerContentToStreamSource("my_source", fileContainer());
393+
verify(httpClient)
394+
.send(argument.capture(), any(HttpResponse.BodyHandlers.ofString().getClass()));
395+
396+
assertEquals("PUT", argument.getValue().method());
397+
assertTrue(
398+
argument.getValue().uri().getPath().contains("the_org_id/sources/my_source/stream/update"));
399+
assertTrue(
400+
argument
401+
.getValue()
402+
.uri()
403+
.getQuery()
404+
.contains(String.format("fileId=%s", fileContainer().fileId)));
405+
assertApplicationJsonHeader();
406+
assertAuthorizationHeader();
407+
}
408+
390409
@Test
391410
public void testOpenStream() throws IOException, InterruptedException {
392411
client.openStream("my_source");

0 commit comments

Comments
 (0)