Skip to main content

Customized client usage

You might want to customize how the API client works, by providing additional information to your request, adding user-agents or use your own HTTP requester.

Setting a logger

You can set a custom logger on the client to enable more or less debug output depending on your need.

java
import com.algolia.api.SearchClient;
import com.algolia.utils.LogLevel;
SearchClient client = new SearchClient("<YOUR_INDEX_NAME>", "<YOUR_API_KEY>");
// No logs.
client.setLogLevel(LogLevel.NONE);
// Logs request and response lines.
client.setLogLevel(LogLevel.BASIC);
// Logs request and response lines and their respective headers.
client.setLogLevel(LogLevel.HEADERS);
// Logs request and response lines and their respective headers and bodies (if present).
client.setLogLevel(LogLevel.BODY);
java
import com.algolia.api.SearchClient;
import com.algolia.utils.LogLevel;
SearchClient client = new SearchClient("<YOUR_INDEX_NAME>", "<YOUR_API_KEY>");
// No logs.
client.setLogLevel(LogLevel.NONE);
// Logs request and response lines.
client.setLogLevel(LogLevel.BASIC);
// Logs request and response lines and their respective headers.
client.setLogLevel(LogLevel.HEADERS);
// Logs request and response lines and their respective headers and bodies (if present).
client.setLogLevel(LogLevel.BODY);

Send additional information in your request with requestOptions

The requestOptions parameter allows you to merge additional information with the client transporter, such as headers or query parameters.

Note: requestOptions overrides the default parameters that were previously set in the method and client.

java
import com.algolia.model.search.*;
import com.algolia.utils.RequestOptions;
client.search(
new SearchMethodParams()
.addRequests(SearchQuery.ofSearchForHits(
new SearchForHits()
.setIndexName("<YOUR_INDEX_NAME>")
.setQuery("<YOUR_QUERY>")
.setHitsPerPage(50)
)
),
new RequestOptions()
// This header is added to the request
.addExtraHeader("additional-header", "hello")
// As we re-define `hitsPerPage`, it will override the value defined in the method's parameters.
.addExtraQueryParameters("hitsPerPage", 100)
);
java
import com.algolia.model.search.*;
import com.algolia.utils.RequestOptions;
client.search(
new SearchMethodParams()
.addRequests(SearchQuery.ofSearchForHits(
new SearchForHits()
.setIndexName("<YOUR_INDEX_NAME>")
.setQuery("<YOUR_QUERY>")
.setHitsPerPage(50)
)
),
new RequestOptions()
// This header is added to the request
.addExtraHeader("additional-header", "hello")
// As we re-define `hitsPerPage`, it will override the value defined in the method's parameters.
.addExtraQueryParameters("hitsPerPage", 100)
);

Provide your own user-agent

The API clients offers a method for you to append data to the current user-agent.

java
import com.algolia.utils.ClientOptions;
import com.algolia.api.SearchClient;
// This will append `; my user agent (optional version)` to the current value of `x-algolia-agent` for every requests
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
new ClientOptions().addAlgoliaAgentSegments("my user agent", "optional version")
);
java
import com.algolia.utils.ClientOptions;
import com.algolia.api.SearchClient;
// This will append `; my user agent (optional version)` to the current value of `x-algolia-agent` for every requests
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
new ClientOptions().addAlgoliaAgentSegments("my user agent", "optional version")
);

Use your own HTTP requester

You can override the default requester behavior by providing your requester logic at the initialization of the client.

Create your own requester by creating a class that implements com.algolia.utils.Requester, and use it by passing it to the client constructor.

java
import com.algolia.api.SearchClient;
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
new ClientOptions().setRequester(new MyOwnRequester())
);
java
import com.algolia.api.SearchClient;
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
new ClientOptions().setRequester(new MyOwnRequester())
);