Customize clients
If you want to change all requests, you can customize the API clients, for example, by using a custom HTTP client, changing the user agent information, or changing the default timeouts.
To customize individual requests, see Request options.
To customize the client, add ClientOptions
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package org.example;
import com.algolia.api.SearchClient;
import com.algolia.config.ClientOptions;
public class Main {
public static void main(String[] args) {
// Add options through method calls as shown in the other examples on this page
var options = ClientOptions.builder().build();
var client = new SearchClient(
"ALGOLIA_APPLICATION_ID",
"ALGOLIA_API_KEY",
options
);
}
}
Logging
To provide a custom logger, use the ClientOptions.setLogger
method.
To adjust the log level, use the ClientOptions.setLogLevel
method.
1
2
3
4
5
6
7
8
9
10
11
12
// Additional imports
import com.algolia.config.LogLevel;
// Additional code omitted
var options = ClientOptions
.builder()
// Print everything to stdout
.setLogger(message -> System.out.println(message))
// Adjuts the log level (print all request headers)
.setLogLevel(LogLevel.HEADERS)
.build();
Timeouts
To change the timeouts for all requests,
use the appropriate setter methods of the ClientOptions
class:
1
2
3
4
5
6
7
8
9
10
11
12
// Additional imports
import java.time.Duration;
// Additional code omitted
var options = ClientOptions
.builder()
// Adjust timeouts
.setConnectTimeout(Duration.ofSeconds(100))
.setReadTimeout(Duration.ofSeconds(100))
.setWriteTimeout(Duration.ofSeconds(100))
.build();
connectTimeout
|
type: Duration
default: 2
Maximum number of seconds to wait for the connection to be established. |
readTimeout
|
type: Duration
default: 5
Maximum number of seconds to wait for a response from the server for read requests. |
writeTimeout
|
type: Duration
default: 30
Maximum number of seconds to wait for a response from the server for write requests. |
User agent information
The API clients send user agent information as User-Agent
header.
To append your own information to this parameter,
use the ClientOptions.addAlgoliaAgentSegment
method:
1
2
3
4
5
6
// Additional code omitted
var options = ClientOptions
.builder()
.addAlgoliaAgentSegment("custom java client", "optional version")
.build();
This appends “custom java client (optional version)” to the User-Agent
header.
Custom HTTP requesters
You can provide your own client for making HTTP requests.
Create a class that implements com.algolia.utils.Requester
and pass it to the ClientOptions.setRequester()
method:
1
2
3
4
5
6
// Additional code omitted
var options = ClientOptions
.builder()
.setRequester(new MyCustomRequester())
.build()
Headers
To add headers to all requests,
use the ClientOptions.addDefaultHeader
method.
1
2
3
4
5
6
// Additional code omitted
var options = ClientOptions
.builder()
.addDefaultHeader("extra-header", "greetings")
.build();
Custom hosts
If you want to proxy your API requests through another server,
use the ClientOptions.setHosts
method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Additional imports
import algolia.config.CallType;
import algolia.config.Host;
import java.util.ArrayList;
import java.util.EnumSet;
// Additional code omitted
var customHosts = new ArrayList<Host>();
hosts.add(
new Host("YOUR_SERVER_URL", EnumSet.of(CallType.READ, CallType.WRITE))
);
var options = ClientOptions
.builder()
.setHosts(customHosts)
.build();
Each instance of Host
accepts the following parameters.
url
|
type: String
Required
URL of your custom server (without scheme). |
callType
|
type: Set<CallType>
Required
Whether this server can be used for read requests, write requests, or both. |
scheme
|
type: String
default: https
Scheme for the URL. |
port
|
type: int
Port where your server accepts requests if deviating from the default. |