Skip to content

fix(clients): use the dsn with the read transporter #3781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import okhttp3.*;
import okhttp3.internal.http.HttpMethod;
import okio.BufferedSink;
import com.algolia.utils.UseReadTransporter;

/**
* HttpRequester is responsible for making HTTP requests using the OkHttp client. It provides a
Expand Down Expand Up @@ -72,7 +73,12 @@ private <T> T execute(@Nonnull HttpRequest httpRequest, RequestOptions requestOp
RequestBody requestBody = createRequestBody(httpRequest);

// Build the HTTP request.
Request request = new Request.Builder().url(url).headers(headers).method(httpRequest.getMethod(), requestBody).build();
Request.Builder requestBuilder = new Request.Builder().url(url).headers(headers).method(httpRequest.getMethod(), requestBody);
if (httpRequest.isRead()) {
requestBuilder.tag(new UseReadTransporter());
}

Request request = requestBuilder.build();

// Get or adjust the HTTP client according to request options.
OkHttpClient client = getOkHttpClient(requestOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import algoliasearch.config._
import algoliasearch.exception.{AlgoliaApiException, AlgoliaClientException}
import algoliasearch.internal.interceptor.{GzipRequestInterceptor, HeaderInterceptor, LogInterceptor}
import algoliasearch.internal.util.escape
import algoliasearch.internal.util.UseReadTransporter
import okhttp3._
import okhttp3.internal.http.HttpMethod
import okio.BufferedSink
Expand Down Expand Up @@ -140,11 +141,12 @@ private[algoliasearch] class HttpRequester private (
val headers = createHeaders(httpRequest, requestOptions)
val requestBody = createRequestBody(httpRequest)
// Build the HTTP request.
val request = new Request.Builder()
val requestBuilder = new Request.Builder()
.url(url)
.headers(headers)
.method(httpRequest.method, requestBody)
.build
if (httpRequest.read) requestBuilder.tag(UseReadTransporter)
val request = requestBuilder.build
// Get or adjust the HTTP client according to request options.
val client = okHttpClient(requestOptions)
// Execute the request.
Expand Down
6 changes: 3 additions & 3 deletions templates/java/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public class {{classname}} extends ApiClient {
hosts.add(new Host(appId + ".algolia.net", EnumSet.of(CallType.WRITE)));

List<Host> commonHosts = new ArrayList<>();
hosts.add(new Host(appId + "-1.algolianet.net", EnumSet.of(CallType.READ, CallType.WRITE)));
hosts.add(new Host(appId + "-2.algolianet.net", EnumSet.of(CallType.READ, CallType.WRITE)));
hosts.add(new Host(appId + "-3.algolianet.net", EnumSet.of(CallType.READ, CallType.WRITE)));
commonHosts.add(new Host(appId + "-1.algolianet.net", EnumSet.of(CallType.READ, CallType.WRITE)));
commonHosts.add(new Host(appId + "-2.algolianet.net", EnumSet.of(CallType.READ, CallType.WRITE)));
commonHosts.add(new Host(appId + "-3.algolianet.net", EnumSet.of(CallType.READ, CallType.WRITE)));

Collections.shuffle(commonHosts, new Random());

Expand Down
24 changes: 24 additions & 0 deletions tests/CTS/client/search/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@
}
]
},
{
"testName": "read transporter with POST method",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "test-app-id",
"apiKey": "test-api-key"
}
},
{
"type": "method",
"method": "searchSingleIndex",
"parameters": {
"indexName": "indexName"
},
"expected": {
"type": "host",
"match": "test-app-id-dsn.algolia.net"
}
}
]
},
{
"testName": "calls api with correct write host",
"autoCreateClient": false,
Expand Down
Loading