A Coveo Push API client library for Java.
The Coveo push-api-client.java package is stored on GitHub packages.
You will need a personal access token (classic) with at least read:packages scope to install this dependency.
For details, see Authenticating to GitHub Packages.
You can install this GitHub Package with Apache Maven by editing the ~/.m2/settings.xml file:
-
Add a repository definition to the GitHub Package.
<repository> <id>github</id> <url>https://maven.pkg.github.com/coveo/push-api-client.java</url> <snapshots> <enabled>true</enabled> </snapshots> </repository>
-
Add your GitHub personal access token with
read:packagespermission to install packages from GitHub Packages.<servers> <server> <id>github</id> <username>USERNAME</username> <password>TOKEN</password> </server> </servers>
Replace USERNAME with your GitHub username and TOKEN with your personal access token.
Add a Coveo dependency to your Maven project by editing the pom.xml file:
<dependency>
<groupId>com.coveo</groupId>
<artifactId>push-api-client.java</artifactId>
<version>2.6.1</version> <!-- Replace with the version of your choice or the latest version -->
</dependency>Replace 2.6.1 with the version of your choice or the latest version.
To install the updated project files, build the Maven project:
mvn installSee more examples in the
./samplesfolder.
import com.coveo.pushapiclient.DocumentBuilder;
import com.coveo.pushapiclient.Source;
import java.io.IOException;
import java.net.http.HttpResponse;
public class PushOneDocument {
public static void main(String[] args) {
Source pushSource = new PushSource("my_api_key", "my_org_id");
DocumentBuilder documentBuilder = new DocumentBuilder("https://my.document.uri", "My document title")
.withData("these words will be searchable");
try {
HttpResponse<String> response = pushSource.addOrUpdateDocument("my_source_id", documentBuilder);
System.out.println(String.format("Source creation status: %s", response.statusCode()));
System.out.println(String.format("Source creation response: %s", response.body()));
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}By default, the SDK leverages an exponential backoff retry mechanism. Exponential backoff allows for the SDK to make multiple attempts to resolve throttled requests, increasing the amount of time to wait for each subsequent attempt. Outgoing requests will retry when a 429 status code is returned from the platform.
The exponential backoff parameters are as follows:
-
retryAfter- The amount of time, in milliseconds, to wait between throttled request attempts.Optional, will default to 5,000.
-
maxRetries- The maximum number of times to retry throttled requests.Optional, will default to 10.
-
timeMultiple- The multiple by which to increase the wait time between each throttled request attempt.Optional, will default to 2.
You may configure the exponential backoff that will be applied to all outgoing requests. To do so, use the BackoffOptionsBuilder class to create a BackoffOptions object when creating either a PushService, PushSource, or StreamService object:
PushSource pushSource = new PushSource("my_api_key", "my_org_id", new BackoffOptionsBuilder().withMaxRetries(5).withRetryAfter(10000).build());
PushService pushService = new PushService(myPushEnabledSource, new BackoffOptionsBuilder().withMaxRetries(10).build());
StreamService streamService = new StreamService(myStreamEnabledSource, new BackoffOptionsBuilder().withRetryAfter(2000).withTimeMultiple(3).build());By default, requests will retry a maximum of 10 times, waiting 5 seconds after the first attempt, with a time multiple of 2 (which will equate to a maximum execution time of roughly 1.5 hours).
If you want to push multiple documents to your Coveo organization and use a service for that (e.g. PushService, StreamService), you may find it useful to configure a logger to catch error and warning messages.
-
Go to your project's root folder.
-
Update the Apache Log4j2 configuration by editing the
log4j2.xmlfile. The following example will print the log execution to the console.<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Console name="ConsoleAppender" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> </Appenders> <Loggers> <Root level="debug"> <AppenderRef ref="ConsoleAppender" /> </Root> </Loggers> </Configuration>
For more details, see Log4j2 configuration.
This project uses Google Java Format, so make sure your code is properly formatted before opening a pull request.
To enforce code style and formatting rules, run the Maven Spotless plugin:
mvn spotless:applyTo initiate the release process, start by generating a JSON Web Token (JWT) using one of the provided scripts in Generate a JSON Web Token (JWT). Ensure to specify the path where the developer-experience-bot private key is stored (can be found in password manager).
Next, obtain an access token by executing the following command, replacing <JSON_TOKEN> and <RELEASER_INSTALLATION_ID> with the JWT token created in the previous step and the respective releaser installation id.
curl -i -X POST \
-H "Authorization: Bearer <JSON_TOKEN>" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations/<RELEASER_INSTALLATION_ID>/access_tokensUsing the access token generated in the previous step, create a release Pull Request. This PR will automatically include the appropriate version bump and update the changelog. Initially, run the command with the --dry-run flag to ensure the PR is created with the correct version.
release-please release-pr \
--token=<ACCESS_TOKEN> \
--repo-url=coveo/push-api-client.java \
--release-type=maven \
--target-branch=main \In case the command creates a pull request with an incorrect tag, manually create a pull request and perform an empty commit as follows:
git commit --allow-empty -m "chore: release x.x.x" -m "Release-As: x.x.x"This action will prompt release-please to bump to the desired version on the next run. Finally, merge the pull request and repeat Step 1.c.
- Tag the commit according to semantic versioning principles.
git tag -a vx.x.x <COMMIT_SHA> -m "chore(main): release x.x.x (#<PULL_REQUEST_NUMBER>)"
git push --tags- Merge the Pull Request.
Lastly, manually create a release on the GitHub repository. This action will trigger the package deploy workflow action.
Happy coding!