Customize your Java client
You can customize the behavior of the API clients by creating a custom configuration This lets you change timeouts, or add HTTP headers.
To modify all requests made with a client, create a custom configuration. To modify individual requests, pass custom request options.
We released a new version of the JavaScript API client in public beta. Read the beta documentation for more information.
We released a new version of the PHP API client in public beta. Read the beta documentation for more information.
We released a new version of the Java API client in public beta. Read the beta documentation for more information.
Use a custom host
You can change the default hosts to which the API client connects:
1
2
3
4
5
6
7
8
SearchClient client =
DefaultSearchClient.create(
new SearchConfig.Builder("applicationID", "apiKey")
.setHosts(
Arrays.asList(
new StatefulHost("yourapplication.example.net", EnumSet.of(CallType.READ)),
new StatefulHost("yourapplication.example.net", EnumSet.of(CallType.WRITE))))
.build());
Changing the hosts can be useful if you want to proxy the search requests through another server, for example, to process the request or response, or to perform custom analytics.
Add HTTP headers to every request
Adding HTTP headers to your configuration allow you to set parameters for every request, for example, a user identifier or an IP address. This can be useful for analytics, geo search, or to implement API key rate limits.
For an overview of available HTTP headers, see Add HTTP headers to your requests
1
2
3
4
5
6
SearchConfig configuration =
new SearchConfig.Builder("YourApplicationID", "YourWriteAPIKey")
.addExtraHeaders("NAME-OF-HEADER", "value-of-header")
.build();
SearchClient client = DefaultSearchClient.create(configuration);
Change timeouts for all requests
Network connections and DNS resolution can be slow. That’s why the API clients come with default timeouts.
You shouldn’t change the default timeouts, unless you have a good reason.
1
2
3
4
5
6
7
8
9
SearchConfig configuration =
new SearchConfig.Builder("YourApplicationID", "YourWriteAPIKey")
.setConnectTimeOut(2000) // connection timeout in milliseconds
.setReadTimeOut(5000) // read timeout in milliseconds
.setWriteTimeOut(30000) // write timeout in milliseconds
.setHostDownDelay(300000) // delay before retrying a host we know was down, in milliseconds
.build();
SearchClient client = DefaultSearchClient.create(configuration);