-
Notifications
You must be signed in to change notification settings - Fork 5
feat: implement StreamService class
#32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e0296ef
create `PushSource` and `catalogSource` classes
y-lakhdar 419d177
add boilerplate for stream service
y-lakhdar 4e8528c
Merge branch 'main' of github.com:coveo/push-api-client.java into LEN…
y-lakhdar 1c5adec
revert master merge
y-lakhdar f5a2557
add required POST calls
y-lakhdar 673b76a
update streamService
y-lakhdar 5ef6772
apply ApiUrl corrections
y-lakhdar 93c9867
Merge branch 'LEN-839' of github.com:coveo/push-api-client.java into …
y-lakhdar 8e1e3e7
document StreamService
y-lakhdar de36933
Merge branch 'main' of github.com:coveo/push-api-client.java into LEN…
y-lakhdar 7f9ad5e
Update src/main/java/com/coveo/pushapiclient/StreamService.java
y-lakhdar 3499404
Update src/main/java/com/coveo/pushapiclient/StreamService.java
y-lakhdar f3ec845
Apply suggestions from code review
y-lakhdar 508564e
refactor unit tests
y-lakhdar 954ad44
Merge branch 'LEN-838' of github.com:coveo/push-api-client.java into …
y-lakhdar 0e9acac
Merge branch 'main' into LEN-838
y-lakhdar c90446c
apply corrections
y-lakhdar cd43eb9
Merge branch 'LEN-838' of github.com:coveo/push-api-client.java into …
y-lakhdar 933be43
Update codeql.yml
louis-bompart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/com/coveo/pushapiclient/DocumentUploadQueue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.coveo.pushapiclient; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| // TODO: LENS-851 - Make public | ||
| class DocumentUploadQueue { | ||
| private final UploadStrategy uploader; | ||
|
|
||
| public DocumentUploadQueue(UploadStrategy uploader) { | ||
| this.uploader = uploader; | ||
| } | ||
|
|
||
| public void flush() throws IOException, InterruptedException { | ||
| throw new UnsupportedOperationException("Unimplemented method (TODO: LENS-856)"); | ||
| } | ||
|
|
||
| public void add(DocumentBuilder document) throws IOException, InterruptedException { | ||
| throw new UnsupportedOperationException("Unimplemented method (TODO: LENS-856)"); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.coveo.pushapiclient; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class StreamResponse { | ||
| public String uploadUri; | ||
| public String fileId; | ||
| public String streamId; | ||
| public Map<String, String> requiredHeaders; | ||
|
|
||
| } |
116 changes: 116 additions & 0 deletions
116
src/main/java/com/coveo/pushapiclient/StreamService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package com.coveo.pushapiclient; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.http.HttpResponse; | ||
|
|
||
| import com.coveo.pushapiclient.exceptions.NoOpenStreamException; | ||
| import com.google.gson.Gson; | ||
|
|
||
| // TODO: LENS-851 - Make public | ||
| class StreamService { | ||
| private final StreamEnabledSource source; | ||
| private final PlatformClient platformClient; | ||
| private StreamServiceInternal service; | ||
| private String streamId; | ||
| private DocumentUploadQueue queue; | ||
|
|
||
| /** | ||
| * Creates a service to stream your documents to the provided source by | ||
| * interacting with the Stream API. | ||
| * | ||
| * <p> | ||
| * To perform <a href="https://docs.coveo.com/en/l62e0540">full document | ||
| * updates</a>, use the {@PushService}, since pushing documents with the | ||
| * {@StreamService} is equivalent to triggering a full source rebuild. The | ||
| * {@StreamService} can also be used for an initial catalog upload. | ||
| * | ||
| * @param source The source to which you want to send your documents. | ||
| */ | ||
| public StreamService(StreamEnabledSource source) { | ||
| String apiKey = source.getApiKey(); | ||
| String organizationId = source.getOrganizationId(); | ||
| PlatformUrl platformUrl = source.getPlatformUrl(); | ||
| UploadStrategy uploader = this.getUploadStrategy(); | ||
|
|
||
| this.source = source; | ||
| this.queue = new DocumentUploadQueue(uploader); | ||
| this.platformClient = new PlatformClient(apiKey, organizationId, platformUrl); | ||
| this.service = new StreamServiceInternal(this.source, this.queue, this.platformClient); | ||
| } | ||
|
|
||
| /** | ||
| * Adds documents to the previously specified source. | ||
| * This function will open a stream before uploading documents into it. | ||
| * | ||
| * <p> | ||
| * If called several times, the service will automatically batch documents and | ||
| * create new stream chunks whenever the data payload exceeds the | ||
| * <a href="https://docs.coveo.com/en/lb4a0344#stream-api-limits">batch size limit</a> | ||
| * set for the Stream API. | ||
| * | ||
| * <p> | ||
| * Once there are no more documents to add, it is important to call the {@link StreamService#close} function | ||
| * in order to send any buffered documents and close the open stream. | ||
| * Otherwise, changes will not be reflected in the index. | ||
| * | ||
| * <p> | ||
| * <pre> | ||
| * {@code | ||
| * //... | ||
| * StreamService service = new StreamService(source)); | ||
| * for (DocumentBuilder document : fictionalDocumentList) { | ||
| * service.add(document); | ||
| * } | ||
| * service.close(document); | ||
| * </pre> | ||
| * | ||
| * <p> | ||
| * For more code samples, visit <a href="TODO: LENS-840">Stream data to your catalog source</a> | ||
| * | ||
| * @param document The documentBuilder to add to your source | ||
| * @throws InterruptedException | ||
| * @throws IOException | ||
| */ | ||
| public void add(DocumentBuilder document) throws IOException, InterruptedException { | ||
| this.service.add(document); | ||
| } | ||
|
|
||
| /** | ||
| * Sends any buffered documents and <a href="https://docs.coveo.com/en/lb4a0344#step-3-close-the-stream">closes the stream</a>. | ||
| * | ||
| * <p> | ||
| * Upon invoking this method, any indexed items not added through this {@link StreamService} instance will be removed. | ||
| * All documents added from the initialization of the service until the invocation of the {@link StreamService#close} function | ||
| * will completely replace the previous content of the source. | ||
| * | ||
| * <p> | ||
| * When you upload a catalog into a source, it will replace the previous content | ||
| * of the source completely. Expect a 15-minute delay for the removal of the old | ||
| * items from the index. | ||
| * | ||
| * @return | ||
| * @throws IOException | ||
| * @throws InterruptedException | ||
| * @throws NoOpenStreamException | ||
| */ | ||
| public HttpResponse<String> close() throws IOException, InterruptedException, NoOpenStreamException { | ||
| return this.service.close(); | ||
| } | ||
|
|
||
| private UploadStrategy getUploadStrategy() { | ||
| return (batchUpdate) -> { | ||
| String sourceId = this.getSourceId(); | ||
| HttpResponse<String> resFileContainer = this.platformClient.requireStreamChunk(sourceId, this.streamId); | ||
| FileContainer fileContainer = new Gson().fromJson(resFileContainer.body(), FileContainer.class); | ||
| String batchUpdateJson = new Gson().toJson(batchUpdate.marshal()); | ||
| return this.platformClient.uploadContentToFileContainer(fileContainer, | ||
| batchUpdateJson); | ||
|
|
||
| }; | ||
| } | ||
|
|
||
| private String getSourceId() { | ||
| return this.source.getId(); | ||
| } | ||
|
|
||
| } |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/coveo/pushapiclient/StreamServiceInternal.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.coveo.pushapiclient; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.http.HttpResponse; | ||
|
|
||
| import com.coveo.pushapiclient.exceptions.NoOpenStreamException; | ||
| import com.google.gson.Gson; | ||
|
|
||
| /** | ||
| * For internal use only. Made to easily test the service without having to use PowerMock | ||
| */ | ||
| class StreamServiceInternal { | ||
| private final StreamEnabledSource source; | ||
| private final PlatformClient platformClient; | ||
| private String streamId; | ||
| private DocumentUploadQueue queue; | ||
|
|
||
| public StreamServiceInternal(StreamEnabledSource source, DocumentUploadQueue queue, PlatformClient platformClient) { | ||
| this.source = source; | ||
| this.queue = queue; | ||
| this.platformClient = platformClient; | ||
| } | ||
|
|
||
| public void add(DocumentBuilder document) throws IOException, InterruptedException { | ||
| if (this.streamId == null) { | ||
| this.streamId = this.getStreamId(); | ||
| } | ||
| queue.add(document); | ||
| } | ||
|
|
||
| public HttpResponse<String> close() throws IOException, InterruptedException, NoOpenStreamException { | ||
| if (this.streamId == null) { | ||
| throw new NoOpenStreamException( | ||
| "No open stream detected. A stream will automatically be opened once you start adding documents."); | ||
| } | ||
| queue.flush(); | ||
| String sourceId = this.getSourceId(); | ||
| return this.platformClient.closeStream(sourceId, this.streamId); | ||
| } | ||
|
|
||
| private String getStreamId() throws IOException, InterruptedException { | ||
| String sourceId = this.getSourceId(); | ||
| HttpResponse<String> response = this.platformClient.openStream(sourceId); | ||
| StreamResponse streamResponse = new Gson().fromJson(response.body(), StreamResponse.class); | ||
| return streamResponse.streamId; | ||
| } | ||
|
|
||
| private String getSourceId() { | ||
| return this.source.getId(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.coveo.pushapiclient; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.http.HttpResponse; | ||
|
|
||
| @FunctionalInterface | ||
| public interface UploadStrategy { | ||
| HttpResponse<String> apply(BatchUpdate batchUpdate) throws IOException, InterruptedException; | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/coveo/pushapiclient/exceptions/NoOpenStreamException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.coveo.pushapiclient.exceptions; | ||
|
|
||
| public class NoOpenStreamException extends Exception { | ||
| public NoOpenStreamException(String errorMessage) { | ||
| super(errorMessage); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.