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 algoliasearch.config.ClientOptions
import algoliasearch.api.SearchClient
val options = ClientOptions
.builder()
.build()
@main
def main(): Unit = {
val client = SearchClient(
appId = "ALGOLIA_APPLICATION_ID",
apiKey = "ALGOLIA_API_KEY",
clientOptions = options
)
}
Logging
To adjust the log level, use the ClientOptions.withLogging
method.
1
2
3
4
5
6
7
8
9
10
// Additional imports
import algoliasearch.config.{ClientOptions, Logging}
// Additional code omitted
var options = ClientOptions
.builder()
// Adjuts the log level
.withLogging(Logging.Full)
.build()
Timeouts
To change the timeouts for all requests,
use the appropriate methods of the ClientOptions
class:
1
2
3
4
5
6
7
8
9
10
11
12
// Additional imports
import scala.concurrent.duration.DurationInt
// Additional code omitted
val options = ClientOptions
.builder()
// Adjust timeouts
.withConnectTimeout(100.seconds)
.withReadTimeout(100.seconds)
.withWriteTimeout(100.seconds)
.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.withAgentSegment
method:
1
2
3
4
5
6
7
// Additional code omitted
val options = ClientOptions
.builder()
// Add user agent information
.withAgentSegment("custom scala client", "optional version")
.build();
This appends “custom scala client (optional version)” to the User-Agent
header.
Headers
To add headers to all requests,
use the ClientOptions.withDefaultHeaders
method.
1
2
3
4
5
6
// Additional code omitted
var options = ClientOptions
.builder()
.withDefaultHeaders("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
// Additional imports
import algoliasearch.config.{CallType, ClientOptions, Host}
// Additional code omitted
val customHosts = Seq(Host("YOUR_SERVER_URL", Set(CallType.Read, CallType.Write)))
var options = ClientOptions
.builder()
.withHosts(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: Option[Int]
default: None
Port where your server accepts requests if deviating from the default. |