Skip to content

Commit 10014c2

Browse files
committed
feat(java): endpoint level timeout
1 parent e5289c8 commit 10014c2

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/config/RequestOptions.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.time.Duration;
55
import java.util.HashMap;
66
import java.util.Map;
7+
import org.jetbrains.annotations.Nullable;
78

89
/**
910
* Request options are used to pass extra parameters, headers, timeout to the request. Parameters
@@ -15,6 +16,7 @@ public final class RequestOptions {
1516
private final Map<String, String> queryParameters = new HashMap<>();
1617
private Duration readTimeout;
1718
private Duration writeTimeout;
19+
private Duration connectTimeout;
1820

1921
public RequestOptions addExtraHeader(String key, Object value) {
2022
if (value == null) return this;
@@ -62,6 +64,32 @@ public RequestOptions setWriteTimeout(Duration writeTimeout) {
6264
return this;
6365
}
6466

67+
public Duration getConnectTimeout() {
68+
return connectTimeout;
69+
}
70+
71+
public RequestOptions setConnectTimeout(Duration connectTimeout) {
72+
this.connectTimeout = connectTimeout;
73+
return this;
74+
}
75+
76+
// `this` will be merged in `other`. Values in `other` will take precedence over `this`'s.
77+
public RequestOptions mergeRight(@Nullable RequestOptions other) {
78+
if (other == null) {
79+
return this;
80+
}
81+
82+
RequestOptions requestOptions = new RequestOptions();
83+
requestOptions.headers.putAll(this.headers);
84+
requestOptions.headers.putAll(other.headers);
85+
requestOptions.queryParameters.putAll(this.queryParameters);
86+
requestOptions.queryParameters.putAll(other.queryParameters);
87+
requestOptions.readTimeout = other.readTimeout != null ? other.readTimeout : this.readTimeout;
88+
requestOptions.writeTimeout = other.writeTimeout != null ? other.writeTimeout : this.writeTimeout;
89+
requestOptions.connectTimeout = other.connectTimeout != null ? other.connectTimeout : this.connectTimeout;
90+
return requestOptions;
91+
}
92+
6593
@Override
6694
public String toString() {
6795
return (
@@ -74,6 +102,8 @@ public String toString() {
74102
readTimeout +
75103
", writeTimeout=" +
76104
writeTimeout +
105+
", connectTimeout=" +
106+
connectTimeout +
77107
'}'
78108
);
79109
}

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/internal/HttpRequester.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ private OkHttpClient getOkHttpClient(RequestOptions requestOptions) {
172172
if (requestOptions.getWriteTimeout() != null) {
173173
builder.writeTimeout(requestOptions.getWriteTimeout());
174174
}
175+
if (requestOptions.getConnectTimeout() != null) {
176+
builder.connectTimeout(requestOptions.getConnectTimeout());
177+
}
175178
return builder.build();
176179
}
177180

templates/java/api.mustache

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {{invokerPackage}}.ApiClient;
66
import {{invokerPackage}}.config.ClientOptions;
77

88
import com.fasterxml.jackson.core.type.TypeReference;
9+
import org.jetbrains.annotations.Nullable;
910

1011
import okhttp3.Call;
1112
import okhttp3.Request;
@@ -138,7 +139,7 @@ public class {{classname}} extends ApiClient {
138139
{{#vendorExtensions}}{{#x-is-generic}}* @param innerType The class held by the index, could be your custom class or {@link Object}.{{/x-is-generic}}{{/vendorExtensions}}
139140
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
140141
{{> api_javadoc}}
141-
public {{> return_type}} {{operationId}}({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#hasRequiredParams}}{{#hasOptionalParams}},{{/hasOptionalParams}}{{/hasRequiredParams}}{{#optionalParams}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/optionalParams}}{{#vendorExtensions}}{{#x-is-generic}}, Class<T> innerType{{/x-is-generic}}{{/vendorExtensions}}{{#hasParams}}, {{/hasParams}}RequestOptions requestOptions) throws AlgoliaRuntimeException {
142+
public {{> return_type}} {{operationId}}({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#hasRequiredParams}}{{#hasOptionalParams}},{{/hasOptionalParams}}{{/hasRequiredParams}}{{#optionalParams}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/optionalParams}}{{#vendorExtensions}}{{#x-is-generic}}, Class<T> innerType{{/x-is-generic}}{{/vendorExtensions}}{{#hasParams}}, {{/hasParams}}@Nullable RequestOptions requestOptions) throws AlgoliaRuntimeException {
142143
{{#returnType}}return {{/returnType}}LaunderThrowable.await({{operationId}}Async({{#allParams}}{{paramName}}, {{/allParams}}{{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}requestOptions));
143144
{{^returnType}}return ;{{/returnType}}
144145
}
@@ -161,7 +162,7 @@ public class {{classname}} extends ApiClient {
161162
{{#vendorExtensions}}{{#x-is-generic}}* @param innerType The class held by the index, could be your custom class or {@link Object}.{{/x-is-generic}}{{/vendorExtensions}}
162163
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
163164
{{> api_javadoc}}
164-
public {{> return_type}} {{operationId}}({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}, {{/requiredParams}}{{#vendorExtensions}}{{#x-is-generic}}Class<T> innerType, {{/x-is-generic}}{{/vendorExtensions}}RequestOptions requestOptions) throws AlgoliaRuntimeException {
165+
public {{> return_type}} {{operationId}}({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}, {{/requiredParams}}{{#vendorExtensions}}{{#x-is-generic}}Class<T> innerType, {{/x-is-generic}}{{/vendorExtensions}} @Nullable RequestOptions requestOptions) throws AlgoliaRuntimeException {
165166
{{#returnType}}return {{/returnType}}this.{{operationId}}({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#requiredParams.0}},{{/requiredParams.0}}{{#optionalParams}}null{{^-last}},{{/-last}}{{/optionalParams}}, {{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}requestOptions);
166167
}
167168
{{/optionalParams.0}}
@@ -185,7 +186,7 @@ public class {{classname}} extends ApiClient {
185186
* @param innerType The class held by the index, could be your custom class or {@link Object}.{{/x-is-generic}}{{/vendorExtensions}}
186187
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
187188
{{> api_javadoc}}
188-
public {{> return_type_async}} {{operationId}}Async({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}},{{/requiredParams}}{{#optionalParams}}{{{dataType}}} {{paramName}}, {{/optionalParams}}{{#vendorExtensions}}{{#x-is-generic}}Class<T> innerType, {{/x-is-generic}}{{/vendorExtensions}}RequestOptions requestOptions) throws AlgoliaRuntimeException {
189+
public {{> return_type_async}} {{operationId}}Async({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}},{{/requiredParams}}{{#optionalParams}}{{{dataType}}} {{paramName}}, {{/optionalParams}}{{#vendorExtensions}}{{#x-is-generic}}Class<T> innerType, {{/x-is-generic}}{{/vendorExtensions}}@Nullable RequestOptions requestOptions) throws AlgoliaRuntimeException {
189190
{{#allParams}}{{#required}}
190191
Parameters.requireNonNull({{paramName}}, "Parameter `{{paramName}}` is required when calling `{{operationId}}`.");
191192
{{/required}}{{/allParams}}
@@ -198,7 +199,7 @@ public class {{classname}} extends ApiClient {
198199
{{#headerParams}}.addHeader("{{baseName}}", {{paramName}}){{/headerParams}}
199200
{{#vendorExtensions}}{{#queryParams}}{{^x-is-custom-request}}.addQueryParameter("{{baseName}}", {{paramName}}){{/x-is-custom-request}}{{#x-is-custom-request}}.addQueryParameters(parameters){{/x-is-custom-request}}{{/queryParams}}{{/vendorExtensions}}
200201
.build();
201-
return executeAsync(request, requestOptions, {{#vendorExtensions}}{{#x-is-generic}}{{{returnType}}}.class, innerType{{/x-is-generic}}{{/vendorExtensions}}{{^vendorExtensions.x-is-generic}}{{^returnType}}null{{/returnType}}{{#returnType}}new TypeReference<{{{.}}}>(){}{{/returnType}}{{/vendorExtensions.x-is-generic}});
202+
return executeAsync(request, {{#vendorExtensions.x-timeouts}}new RequestOptions().setReadTimeout(Duration.ofMillis({{{read}}}L)).setWriteTimeout(Duration.ofMillis({{{write}}}L)).setConnectTimeout(Duration.ofMillis({{{connect}}}L)).mergeRight({{/vendorExtensions.x-timeouts}}requestOptions{{#vendorExtensions.x-timeouts}}){{/vendorExtensions.x-timeouts}}, {{#vendorExtensions}}{{#x-is-generic}}{{{returnType}}}.class, innerType{{/x-is-generic}}{{/vendorExtensions}}{{^vendorExtensions.x-is-generic}}{{^returnType}}null{{/returnType}}{{#returnType}}new TypeReference<{{{.}}}>(){}{{/returnType}}{{/vendorExtensions.x-is-generic}});
202203
}
203204

204205
{{! This case only sets `requestOptions` as optional }}
@@ -221,7 +222,7 @@ public class {{classname}} extends ApiClient {
221222
* @param innerType The class held by the index, could be your custom class or {@link Object}.{{/x-is-generic}}{{/vendorExtensions}}
222223
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
223224
{{> api_javadoc}}
224-
public {{> return_type_async}} {{operationId}}Async({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}, {{/requiredParams}}{{#vendorExtensions}}{{#x-is-generic}}Class<T> innerType, {{/x-is-generic}}{{/vendorExtensions}}RequestOptions requestOptions) throws AlgoliaRuntimeException {
225+
public {{> return_type_async}} {{operationId}}Async({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}, {{/requiredParams}}{{#vendorExtensions}}{{#x-is-generic}}Class<T> innerType, {{/x-is-generic}}{{/vendorExtensions}}@Nullable RequestOptions requestOptions) throws AlgoliaRuntimeException {
225226
return this.{{operationId}}Async({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#requiredParams.0}},{{/requiredParams.0}}{{#optionalParams}}null{{^-last}},{{/-last}}{{/optionalParams}}, {{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}requestOptions);
226227
}
227228
{{/optionalParams.0}}

0 commit comments

Comments
 (0)