Skip to content

Commit b98e6e0

Browse files
committed
add java
1 parent 5ad13a6 commit b98e6e0

File tree

19 files changed

+80
-75
lines changed

19 files changed

+80
-75
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.algolia.utils;
2+
3+
public class UseReadTransporter {}

clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/retry/RetryStrategy.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.algolia.utils.retry;
22

33
import com.algolia.exceptions.*;
4+
import com.algolia.utils.UseReadTransporter;
45
import com.algolia.utils.Utils;
56
import java.io.IOException;
67
import java.net.SocketTimeoutException;
@@ -28,8 +29,11 @@ public Interceptor getRetryInterceptor() {
2829
@Override
2930
public Response intercept(Chain chain) throws IOException {
3031
Request request = chain.request();
32+
UseReadTransporter useReadTransporter = (UseReadTransporter) request.tag();
3133
Iterator<StatefulHost> hostsIter = getTryableHosts(
32-
request.method().equals("GET") ? CallType.READ : CallType.WRITE
34+
(useReadTransporter != null || request.method().equals("GET"))
35+
? CallType.READ
36+
: CallType.WRITE
3337
)
3438
.iterator();
3539
while (hostsIter.hasNext()) {

clients/algoliasearch-client-javascript/packages/client-common/src/transporter/createTransporter.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function createTransporter({
8989
requestOptions: RequestOptions
9090
): Promise<TResponse> {
9191
const stackTrace: StackFrame[] = [];
92-
const isRead = request?.useReadMethod || request.method === 'GET';
92+
const isRead = request?.useReadTransporter || request.method === 'GET';
9393

9494
/**
9595
* First we prepare the payload that do not depend from hosts.
@@ -99,12 +99,13 @@ export function createTransporter({
9999
const method = request.method;
100100

101101
// On `GET`, the data is proxied to query parameters.
102-
const dataQueryParameters: Record<string, any> = isRead
103-
? {
104-
...request.data,
105-
...requestOptions.data,
106-
}
107-
: {};
102+
const dataQueryParameters: Record<string, any> =
103+
request.method === 'GET'
104+
? {
105+
...request.data,
106+
...requestOptions.data,
107+
}
108+
: {};
108109

109110
const queryParameters: QueryParameters = {
110111
'x-algolia-agent': algoliaAgent.value,

clients/algoliasearch-client-javascript/packages/client-common/src/types/Requester.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type Request = {
1111
* Some POST methods in the Algolia REST API uses the `read` transporter.
1212
* This information is defined at the spec level.
1313
*/
14-
useReadMethod?: boolean;
14+
useReadTransporter?: boolean;
1515
};
1616

1717
export type EndRequest = {

playground/javascript/node/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const searchIndex = process.env.SEARCH_INDEX || 'test_index';
1212
const searchQuery = process.env.SEARCH_QUERY || 'test_query';
1313

1414
// Init client with appId and apiKey
15-
const client = searchClient(appId, apiKey, { requester: echoRequester() });
15+
const client = searchClient(appId, apiKey);
1616

1717
client.addAlgoliaAgent('Node playground', '0.0.1');
1818

specs/recommend/paths/getRecommendations.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ post:
22
tags:
33
- recommendations
44
operationId: getRecommendations
5-
x-use-read-method: true
5+
x-use-read-transporter: true
66
summary: Get results.
77
description: Returns recommendations or trending results, for a specific model and `objectID`.
88
requestBody:

specs/search/paths/dictionaries/searchDictionaryEntries.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ post:
22
tags:
33
- Dictionaries
44
operationId: searchDictionaryEntries
5-
x-use-read-method: true
5+
x-use-read-transporter: true
66
description: Search the dictionary entries.
77
summary: Search a dictionary entries.
88
parameters:

specs/search/paths/multiclusters/searchUserIds.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ post:
22
tags:
33
- Clusters
44
operationId: searchUserIds
5-
x-use-read-method: true
5+
x-use-read-transporter: true
66
summary: Search userID.
77
description: >
88
Search for userIDs.

specs/search/paths/objects/multipleGetObjects.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ post:
22
tags:
33
- Records
44
operationId: getObjects
5-
x-use-read-method: true
5+
x-use-read-transporter: true
66
summary: Retrieve one or more objects.
77
description: Retrieve one or more objects, potentially from different indices, in a single API call.
88
requestBody:

specs/search/paths/rules/searchRules.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ post:
22
tags:
33
- Rules
44
operationId: searchRules
5-
x-use-read-method: true
5+
x-use-read-transporter: true
66
summary: Search for rules.
77
description: Search for rules matching various criteria.
88
parameters:

specs/search/paths/search/search.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ post:
22
tags:
33
- Search
44
operationId: search
5-
x-use-read-method: true
5+
x-use-read-transporter: true
66
summary: Search multiple indices.
77
description: Perform a search operation targeting one or many indices.
88
requestBody:

specs/search/paths/search/searchForFacetValues.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ post:
22
tags:
33
- Search
44
operationId: searchForFacetValues
5-
x-use-read-method: true
5+
x-use-read-transporter: true
66
summary: Search for values of a given facet.
77
description: Search for values of a given facet, optionally restricting the returned values to those contained in objects matching other search criteria.
88
parameters:

specs/search/paths/search/searchSingleIndex.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ post:
22
tags:
33
- Search
44
operationId: searchSingleIndex
5-
x-use-read-method: true
5+
x-use-read-transporter: true
66
summary: Search in a single index.
77
description: Perform a search operation targeting one specific index.
88
parameters:

specs/search/paths/synonyms/searchSynonyms.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ post:
22
tags:
33
- Synonyms
44
operationId: searchSynonyms
5-
x-use-read-method: true
5+
x-use-read-transporter: true
66
summary: Search synonyms.
77
description: Search or browse all synonyms, optionally filtering them by type.
88
parameters:

templates/java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package {{invokerPackage}};
22

3-
import com.algolia.utils.Requester;
43
import com.algolia.exceptions.*;
5-
import com.algolia.utils.AlgoliaAgent;
6-
import com.algolia.utils.JSON;
7-
import com.algolia.utils.RequestOptions;
4+
import com.algolia.utils.*;
85

96
import okhttp3.*;
107
import okhttp3.internal.http.HttpMethod;
@@ -270,11 +267,12 @@ public class ApiClient {
270267
* @param body The request body object
271268
* @param headerParams The header parameters
272269
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
270+
* @param useReadTransporter Some POST methods in the Algolia REST API uses the `read` transporter. This information is defined at the spec level.
273271
* @return The HTTP call
274272
* @throws AlgoliaRuntimeException If fail to serialize the request body object
275273
*/
276-
public Call buildCall(String path, String method, Map<String, Object> queryParameters, Object body, Map<String, String> headerParams, RequestOptions requestOptions) throws AlgoliaRuntimeException {
277-
Request request = buildRequest(path, method, queryParameters, body, headerParams, requestOptions);
274+
public Call buildCall(String path, String method, Map<String, Object> queryParameters, Object body, Map<String, String> headerParams, RequestOptions requestOptions, Boolean useReadTransporter) throws AlgoliaRuntimeException {
275+
Request request = buildRequest(path, method, queryParameters, body, headerParams, requestOptions, useReadTransporter);
278276
279277
return requester.newCall(request);
280278
}
@@ -288,39 +286,52 @@ public class ApiClient {
288286
* @param body The request body object
289287
* @param headerParams The header parameters
290288
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
289+
* @param useReadTransporter Some POST methods in the Algolia REST API uses the `read` transporter. This information is defined at the spec level.
291290
* @return The HTTP request
292291
* @throws AlgoliaRuntimeException If fail to serialize the request body object
293292
*/
294-
public Request buildRequest(String path, String method, Map<String, Object> queryParameters, Object body, Map<String, String> headerParams, RequestOptions requestOptions) throws AlgoliaRuntimeException {
295-
boolean hasRequestOptions = requestOptions != null;
296-
final String url = buildUrl(
297-
path,
298-
queryParameters,
299-
hasRequestOptions ? requestOptions.getExtraQueryParameters() : null
300-
);
301-
final Request.Builder reqBuilder = new Request.Builder().url(url);
302-
processHeaderParams(
303-
headerParams,
304-
hasRequestOptions ? requestOptions.getExtraHeaders() : null,
305-
reqBuilder
306-
);
293+
public Request buildRequest(
294+
String path,
295+
String method,
296+
Map<String, Object> queryParameters,
297+
Object body,
298+
Map<String, String> headerParams,
299+
RequestOptions requestOptions,
300+
Boolean useReadTransporter
301+
) throws AlgoliaRuntimeException {
302+
boolean hasRequestOptions = requestOptions != null;
303+
final String url = buildUrl(
304+
path,
305+
queryParameters,
306+
hasRequestOptions ? requestOptions.getExtraQueryParameters() : null
307+
);
308+
final Request.Builder reqBuilder = new Request.Builder().url(url);
309+
processHeaderParams(
310+
headerParams,
311+
hasRequestOptions ? requestOptions.getExtraHeaders() : null,
312+
reqBuilder
313+
);
307314
308-
RequestBody reqBody;
309-
if (!HttpMethod.permitsRequestBody(method)) {
310-
reqBody = null;
311-
} else if (body == null) {
312-
if ("DELETE".equals(method)) {
313-
// allow calling DELETE without sending a request body
314-
reqBody = null;
315-
} else {
316-
// use an empty request body (for POST, PUT and PATCH)
317-
reqBody = RequestBody.create("", MediaType.parse(this.contentType));
318-
}
319-
} else {
320-
reqBody = serialize(body);
321-
}
315+
RequestBody reqBody;
316+
if (!HttpMethod.permitsRequestBody(method)) {
317+
reqBody = null;
318+
} else if (body == null) {
319+
if ("DELETE".equals(method)) {
320+
// allow calling DELETE without sending a request body
321+
reqBody = null;
322+
} else {
323+
// use an empty request body (for POST, PUT and PATCH)
324+
reqBody = RequestBody.create("", MediaType.parse(this.contentType));
325+
}
326+
} else {
327+
reqBody = serialize(body);
328+
}
322329
323-
return reqBuilder.method(method, reqBody).build();
330+
if (useReadTransporter) {
331+
reqBuilder.tag(new UseReadTransporter());
332+
}
333+
334+
return reqBuilder.method(method, reqBody).build();
324335
}
325336
326337
/**
@@ -401,18 +412,4 @@ public class ApiClient {
401412
}
402413
}
403414
}
404-
405-
/**
406-
* Build a form-encoding request body with the given form parameters.
407-
*
408-
* @param formParams Form parameters in the form of Map
409-
* @return RequestBody
410-
*/
411-
public RequestBody buildRequestBodyFormEncoding(Map<String, Object> formParams) {
412-
okhttp3.FormBody.Builder formBuilder = new okhttp3.FormBody.Builder();
413-
for (Entry<String, Object> param : formParams.entrySet()) {
414-
formBuilder.add(param.getKey(), parameterToString(param.getValue()));
415-
}
416-
return formBuilder.build();
417-
}
418415
}

templates/java/libraries/okhttp-gson/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public class {{classname}} extends ApiClient {
192192
}
193193

194194
{{/headerParams}}
195-
Call call = this.buildCall(requestPath, "{{httpMethod}}", queryParameters, bodyObj, headers, requestOptions);
195+
Call call = this.buildCall(requestPath, "{{httpMethod}}", queryParameters, bodyObj, headers, requestOptions, {{#vendorExtensions.x-use-read-transporter}}true{{/vendorExtensions.x-use-read-transporter}}{{^vendorExtensions.x-use-read-transporter}}false{{/vendorExtensions.x-use-read-transporter}});
196196
Type returnType = new TypeToken<{{{returnType}}}>() {}.getType();
197197
return this.executeAsync(call, returnType);
198198
}

templates/javascript/api-single.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR
218218
{{#bodyParam}}
219219
data: {{paramName}},
220220
{{/bodyParam}}
221-
{{#vendorExtensions.x-use-read-method}}
222-
useReadMethod: true,
223-
{{/vendorExtensions.x-use-read-method}}
221+
{{#vendorExtensions.x-use-read-transporter}}
222+
useReadTransporter: true,
223+
{{/vendorExtensions.x-use-read-transporter}}
224224
};
225225

226226
return transporter.request(request, {

tests/CTS/client/recommend/api.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"expected": {
1515
"match": {
1616
"objectContaining": {
17-
"host": "test-app-id.algolia.net"
17+
"host": "test-app-id-dsn.algolia.net"
1818
}
1919
}
2020
}
@@ -58,7 +58,7 @@
5858
"match": {
5959
"objectContaining": {
6060
"connectTimeout": 2,
61-
"responseTimeout": 30
61+
"responseTimeout": 5
6262
}
6363
}
6464
}

tests/CTS/client/search/api.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"expected": {
1919
"match": {
2020
"objectContaining": {
21-
"host": "test-app-id.algolia.net"
21+
"host": "test-app-id-dsn.algolia.net"
2222
}
2323
}
2424
}
@@ -70,7 +70,7 @@
7070
"match": {
7171
"objectContaining": {
7272
"connectTimeout": 2,
73-
"responseTimeout": 30
73+
"responseTimeout": 5
7474
}
7575
}
7676
}

0 commit comments

Comments
 (0)