Setup
The following sections demonstrate how to setup the Language Weaver Client before using the product capabilities.
Setting API Credentials
In order to be able to use the SDK, you need a valid set of Language Weaver API credentials. There are two ways to define the API credentials that will be used by the SDK for authentication:
1. Explicitly Specifying Credentials
An instance of CredentialsConfiguration must be created, that receives the client identifier and the client secret as constructor arguments.
CredentialsConfiguration credentialsConfiguration = new CredentialsConfiguration("clientId", "clientSecret");
2. Using Environment Variables
The following environment variables must be set on the system in order to be used by the SDK:
LW_CLIENT_ID - represents the client identifier
LW_CLIENT_SECRET - represents the client secret
These will be automatically loaded when the LanguageWeaverClient is built.
Note
LW_CLIENT_SECRET is Cloud specific. Edge uses only LW_CLIENT_ID.
See here how the values can be obtained for each product.
Setting ClientConfiguration
Before creating the LanguageWeaverClient, a ClientConfiguration must be defined. This object holds the connection
information: CredentialsConfiguration and RetryConfiguration.
Use the RetryConfiguration to customize the retry mechanism in case of failed requests.
ClientConfiguration clientConfiguration = new ClientConfiguration()
.setCredentialsConfiguration(credentialsConfiguration)
.setRetryConfiguration(new RetryConfiguration()
.setAttempts(3)
.setDelay(5)
.setUnit(TimeUnit.SECONDS));
If CredentialsConfiguration or RetryConfiguration are not specified, the default values will be used:
- Credentials will be automatically loaded from environment variables when the LanguageWeaverClient is built
- In case of failures, the requests are retried 3 times, with a delay of 5 seconds.
Besides the CredentialsConfiguration and RetryConfiguration, the product can also be specified on the ClientConfiguration level.
ClientConfiguration clientConfiguration = new ClientConfiguration()
.setCredentialsConfiguration(credentialsConfiguration)
.setRetryConfiguration(new RetryConfiguration()
.setAttempts(3)
.setDelay(5)
.setUnit(TimeUnit.SECONDS))
.setProduct(Product.CLOUD);
The product can also be defined using environment variables. This offers the possibility to easily switch between products, with no code changes as described here.
Setting API URL
For Cloud product, the URL cannot be overridden. The API is by default set to the Europe region using https://api.languageweaver.com URL.
The connection region can be overridden when using a CloudLanguageWeaverClient or by defining the LW_CLOUD_REGION environment variable with "US" value. For Europe, the environment variable value must be set to "EUROPE".
For Edge product, by default, the URL is set to http://localhost:8001. This can be overridden using one of the following methods:
- Set the LW_EDGE_OVERWRITE_URL environment variable with the needed URL.
- Using a EdgeLanguageWeaverClient and setting the overwriteUrl on the client object.
Building the LanguageWeaverClient
The LanguageWeaverClient is the core object that is used for all interactions with the product. This object requires the previously created ClientConfiguration.
try (LanguageWeaverClient languageWeaverClient = new SdkFactory().getLanguageWeaverClient(new ClientConfiguration())) {
// perform requests
}
By default, the used product is Cloud with the URL set to the Europe region.
Switch between products
Switching from default Cloud product to Edge product, can be done in two ways:
- By defining an environment variable, LW_PRODUCT with "EDGE" value. In this way, without code changes, you can easily switch between implementations. For Cloud, the variable's value must be set to "CLOUD".
- By setting the product directly on the ClientConfiguration object
ClientConfiguration clientConfiguration = new ClientConfiguration()
.setProduct(Product.EDGE);
Product Specific LanguageWeaverClient
More customized operations that are product specific can be done by creating a CloudLanguageWeaverClient or EdgeLanguageWeaverClient.