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
package org.example
import com.algolia.client.api.SearchClient
import com.algolia.client.configuration.ClientOptions
fun main() {
val options = ClientOptions(
// Custom client options
)
val client = SearchClient(
appId = "ALGOLIA_APPLICATION_ID",
apiKey = "ALGOLIA_API_KEY",
options = options
)
}
Logging
You can provide a custom logger to the ClientOptions
class
and adjust the log level for more or less information.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Additional imports
import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.LogLevel
// Additional code omitted
val options = ClientOptions(
// Adjust the log level
logLevel = LogLevel.ALL,
// Print everything to stdout
logger = object : Logger {
override fun log(message: String) {
println(message)
}
},
)
Timeouts
To change the timeouts for all requests,
use the timeouts parameters of the ClientOptions
object:
1
2
3
4
5
6
7
8
9
10
11
// Additional imports
import kotlin.time.DurationUnit
import kotlin.time.toDuration
// Additional code omitted
val options = ClientOptions(
connectTimeout = 10.toDuration(DurationUnit.SECONDS),
readTimeout = 10.toDuration(DurationUnit.SECONDS),
writeTimeout = 10.toDuration(DurationUnit.SECONDS)
)
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. |
Custom HTTP requesters
You can create your own client for making HTTP requests and pass it to the ClientOptions
object.
1
2
3
4
5
// Additional code omitted
val options = ClientOptions(
requester = CustomRequester()
)
Headers
To add headers to all requests,
use the defaultHeaders
parameter of the ClientOptions
object:
1
2
3
4
5
// Additional code omitted
val options = ClientOptions(
defaultHeaders = mapOf("extra-header" to "greetings")
)
Custom hosts
If you want to proxy your API requests through another server,
use the hosts
parameter of the ClientOptions
object:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Additional imports
import algolia.config.CallType;
import algolia.config.Host;
// Additional code omitted
val hosts = listOf(
Host(
url = "YOUR_SERVER_URL",
callType = CallType.Read
),
// Add the same server again
// if you want to use it for both read and write requests
Host(
url = "YOUR_SERVER_URL",
callType = CallType.Write
)
)
val options = ClientOptions(
hosts = hosts
)
Each instance of Host
accepts the following parameters.
url
|
type: String
Required
URL of your custom server (without scheme). |
callType
|
type: CallType?
default: null
Whether this server can be used for read or write requests.
You can add the same server twice with different |
scheme
|
type: String
default: https
Scheme for the URL. |
port
|
type: Int?
default: null
Port where your server accepts requests if deviating from the default. |