Skip to content

Commit a59c5ef

Browse files
feat(java): Add WithHTTPInfo method derivatives (#5712)
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
1 parent 4ce8b7d commit a59c5ef

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ private <T> T execute(@Nonnull HttpRequest httpRequest, RequestOptions requestOp
9797
return null; // No need to deserialize, either no content or no type provided
9898
}
9999

100+
// Returns the raw response when using `*WithHTTPInfo` methods.
101+
if (returnType.hasRawClass(Response.class)) {
102+
return (T) response;
103+
}
104+
100105
// Deserialize and return the response.
101106
return serializer.deserialize(response.body().byteStream(), returnType);
102107
} catch (IOException exception) {

templates/java/api.mustache

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import javax.annotation.Nullable;
1111

1212
import okhttp3.Call;
1313
import okhttp3.Request;
14+
import okhttp3.Response;
1415

1516
import {{invokerPackage}}.utils.*;
1617
import {{modelPackage}}.*;
@@ -163,6 +164,17 @@ public class {{classname}} extends ApiClient {
163164
{{^returnType}}return ;{{/returnType}}
164165
}
165166

167+
{{! This case is used for `WithHTTPInfo` methods }}
168+
/**
169+
* {{{notes}}}{{#allParams}}
170+
* @param {{paramName}} {{{description}}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}
171+
{{#vendorExtensions}}{{#x-is-generic}}* @param innerType The class for an HTTP response.{{/x-is-generic}}{{/vendorExtensions}}
172+
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
173+
{{> api_javadoc}}
174+
public Response {{operationId}}WithHTTPInfo({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#hasRequiredParams}}{{#hasOptionalParams}},{{/hasOptionalParams}}{{/hasRequiredParams}}{{#optionalParams}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/optionalParams}}{{#vendorExtensions}}{{#x-is-generic}}, Class<Response> innerType{{/x-is-generic}}{{/vendorExtensions}}{{#hasParams}}, {{/hasParams}}@Nullable RequestOptions requestOptions) throws AlgoliaRuntimeException {
175+
return LaunderThrowable.await({{operationId}}WithHTTPInfoAsync({{#allParams}}{{paramName}}, {{/allParams}}{{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}requestOptions));
176+
}
177+
166178
{{! This case only sets `requestOptions` as optional }}
167179
/**
168180
* {{{notes}}}{{#allParams}}
@@ -173,6 +185,16 @@ public class {{classname}} extends ApiClient {
173185
{{#returnType}}return {{/returnType}}this.{{operationId}}({{#allParams}}{{paramName}}, {{/allParams}}{{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}null);
174186
}
175187

188+
{{! This case sets `requestOptions` as optional and is used for`WithHTTPInfo`}}
189+
/**
190+
* {{{notes}}}{{#allParams}}
191+
* @param {{paramName}} {{{description}}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}
192+
{{#vendorExtensions}}{{#x-is-generic}}* @param innerType The class for an HTTP response.{{/x-is-generic}}{{/vendorExtensions}}
193+
{{> api_javadoc}}
194+
public Response {{operationId}}WithHTTPInfo({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#hasRequiredParams}}{{#hasOptionalParams}},{{/hasOptionalParams}}{{/hasRequiredParams}}{{#optionalParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/optionalParams}}{{#vendorExtensions}}{{#x-is-generic}}, Class<Response> innerType{{/x-is-generic}}{{/vendorExtensions}}) throws AlgoliaRuntimeException {
195+
return this.{{operationId}}WithHTTPInfo({{#allParams}}{{paramName}}, {{/allParams}}{{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}null);
196+
}
197+
176198
{{! This case sets `requiredParams` + `requestOptions` }}
177199
{{#optionalParams.0}}
178200
/**
@@ -186,6 +208,19 @@ public class {{classname}} extends ApiClient {
186208
}
187209
{{/optionalParams.0}}
188210

211+
{{! This case sets `requiredParams` + `requestOptions` and is used for`WithHTTPInfo`}}
212+
{{#optionalParams.0}}
213+
/**
214+
* {{{notes}}}{{#requiredParams}}
215+
* @param {{paramName}} {{{description}}} (required){{/requiredParams}}
216+
{{#vendorExtensions}}{{#x-is-generic}}* @param innerType The class for an HTTP response.{{/x-is-generic}}{{/vendorExtensions}}
217+
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
218+
{{> api_javadoc}}
219+
public Response {{operationId}}WithHTTPInfo({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}, {{/requiredParams}}{{#vendorExtensions}}{{#x-is-generic}}Class<Response> innerType, {{/x-is-generic}}{{/vendorExtensions}} @Nullable RequestOptions requestOptions) throws AlgoliaRuntimeException {
220+
return this.{{operationId}}WithHTTPInfo({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#requiredParams.0}},{{/requiredParams.0}}{{#optionalParams}}null{{^-last}},{{/-last}}{{/optionalParams}}, {{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}requestOptions);
221+
}
222+
{{/optionalParams.0}}
223+
189224
{{! This case only sets `requiredParams` }}
190225
{{#optionalParams.0}}
191226
/**
@@ -198,6 +233,18 @@ public class {{classname}} extends ApiClient {
198233
}
199234
{{/optionalParams.0}}
200235

236+
{{! This case sets `requiredParams` and is used for`WithHTTPInfo`}}
237+
{{#optionalParams.0}}
238+
/**
239+
* {{{notes}}}{{#requiredParams}}
240+
* @param {{paramName}} {{{description}}} (required){{/requiredParams}}
241+
{{#vendorExtensions}}{{#x-is-generic}}* @param innerType The class for an HTTP response.{{/x-is-generic}}{{/vendorExtensions}}
242+
{{> api_javadoc}}
243+
public Response {{operationId}}WithHTTPInfo({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#vendorExtensions}}{{#x-is-generic}}, Class<Response> innerType{{/x-is-generic}}{{/vendorExtensions}}) throws AlgoliaRuntimeException {
244+
return this.{{operationId}}WithHTTPInfo({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#requiredParams.0}},{{/requiredParams.0}}{{#optionalParams}}null{{^-last}},{{/-last}}{{/optionalParams}}, {{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}null);
245+
}
246+
{{/optionalParams.0}}
247+
201248
/**
202249
* (asynchronously)
203250
* {{{notes}}}{{#allParams}}
@@ -221,6 +268,29 @@ public class {{classname}} extends ApiClient {
221268
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}});
222269
}
223270

271+
/**
272+
* (asynchronously)
273+
* {{{notes}}}{{#allParams}}
274+
* @param {{paramName}} {{{description}}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}{{#vendorExtensions}}{{#x-is-generic}}
275+
* @param innerType The class for an HTTP response.{{/x-is-generic}}{{/vendorExtensions}}
276+
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
277+
{{> api_javadoc}}
278+
public CompletableFuture<Response> {{operationId}}WithHTTPInfoAsync({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}},{{/requiredParams}}{{#optionalParams}}{{{dataType}}} {{paramName}}, {{/optionalParams}}{{#vendorExtensions}}{{#x-is-generic}}Class<Response> innerType, {{/x-is-generic}}{{/vendorExtensions}}@Nullable RequestOptions requestOptions) throws AlgoliaRuntimeException {
279+
{{#allParams}}{{#required}}
280+
Parameters.requireNonNull({{paramName}}, "Parameter `{{paramName}}` is required when calling `{{operationId}}`.");
281+
{{/required}}{{/allParams}}
282+
283+
HttpRequest request = HttpRequest.builder()
284+
{{#vendorExtensions}}{{#x-is-custom-request}}.setPathEncoded("{{{path}}}", path){{/x-is-custom-request}}{{^x-is-custom-request}}.setPath("{{{path}}}"{{#pathParams}}, {{{paramName}}}{{/pathParams}}){{/x-is-custom-request}}{{/vendorExtensions}}
285+
.setMethod("{{httpMethod}}")
286+
{{#bodyParam}}.setBody({{paramName}}){{/bodyParam}}
287+
{{#vendorExtensions.x-use-read-transporter}}.setRead(true){{/vendorExtensions.x-use-read-transporter}}
288+
{{#headerParams}}.addHeader("{{baseName}}", {{paramName}}){{/headerParams}}
289+
{{#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}}
290+
.build();
291+
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<Response>(){}{{/returnType}}{{/vendorExtensions.x-is-generic}});
292+
}
293+
224294
{{! This case only sets `requestOptions` as optional }}
225295
/**
226296
* (asynchronously)
@@ -232,6 +302,17 @@ public class {{classname}} extends ApiClient {
232302
return this.{{operationId}}Async({{#allParams}}{{paramName}}, {{/allParams}}{{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}null);
233303
}
234304

305+
{{! This case sets `requestOptions` as optional and is used for`WithHTTPInfo`}}
306+
/**
307+
* (asynchronously)
308+
* {{{notes}}}{{#allParams}}
309+
* @param {{paramName}} {{{description}}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}{{#vendorExtensions}}{{#x-is-generic}}
310+
* @param innerType The class for an HTTP response.{{/x-is-generic}}{{/vendorExtensions}}
311+
{{> api_javadoc}}
312+
public CompletableFuture<Response> {{operationId}}WithHTTPInfoAsync({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#hasRequiredParams}}{{#hasOptionalParams}},{{/hasOptionalParams}}{{/hasRequiredParams}}{{#optionalParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/optionalParams}}{{#vendorExtensions}}{{#x-is-generic}}, Class<Response> innerType{{/x-is-generic}}{{/vendorExtensions}}) throws AlgoliaRuntimeException {
313+
return this.{{operationId}}WithHTTPInfoAsync({{#allParams}}{{paramName}}, {{/allParams}}{{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}null);
314+
}
315+
235316
{{! This case sets `requiredParams` + `requestOptions` }}
236317
{{#optionalParams.0}}
237318
/**
@@ -246,6 +327,20 @@ public class {{classname}} extends ApiClient {
246327
}
247328
{{/optionalParams.0}}
248329

330+
{{! This case sets `requiredParams` + `requestOptions` and is used for`WithHTTPInfo`}}
331+
{{#optionalParams.0}}
332+
/**
333+
* (asynchronously)
334+
* {{{notes}}}{{#requiredParams}}
335+
* @param {{paramName}} {{{description}}} (required){{/requiredParams}}{{#vendorExtensions}}{{#x-is-generic}}
336+
* @param innerType The class for an HTTP response.{{/x-is-generic}}{{/vendorExtensions}}
337+
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
338+
{{> api_javadoc}}
339+
public CompletableFuture<Response> {{operationId}}WithHTTPInfoAsync({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}, {{/requiredParams}}{{#vendorExtensions}}{{#x-is-generic}}Class<Response> innerType, {{/x-is-generic}}{{/vendorExtensions}}@Nullable RequestOptions requestOptions) throws AlgoliaRuntimeException {
340+
return this.{{operationId}}WithHTTPInfoAsync({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#requiredParams.0}},{{/requiredParams.0}}{{#optionalParams}}null{{^-last}},{{/-last}}{{/optionalParams}}, {{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}requestOptions);
341+
}
342+
{{/optionalParams.0}}
343+
249344
{{! This case only sets `requiredParams` }}
250345
{{#optionalParams.0}}
251346
/**
@@ -258,7 +353,20 @@ public class {{classname}} extends ApiClient {
258353
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}}null);
259354
}
260355
{{/optionalParams.0}}
261-
{{/operation}}
356+
357+
{{! This case sets `requiredParams` and is used for`WithHTTPInfo`}}
358+
{{#optionalParams.0}}
359+
/**
360+
* (asynchronously)
361+
* {{{notes}}}{{#requiredParams}}
362+
* @param {{paramName}} {{{description}}} (required){{/requiredParams}}{{#vendorExtensions}}{{#x-is-generic}}
363+
* @param innerType The class for an HTTP response.{{/x-is-generic}}{{/vendorExtensions}}
364+
{{> api_javadoc}}
365+
public CompletableFuture<Response> {{operationId}}WithHTTPInfoAsync({{#requiredParams}}@Nonnull {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#vendorExtensions}}{{#x-is-generic}}, Class<Response> innerType{{/x-is-generic}}{{/vendorExtensions}}) throws AlgoliaRuntimeException {
366+
return this.{{operationId}}WithHTTPInfoAsync({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#requiredParams.0}},{{/requiredParams.0}}{{#optionalParams}}null{{^-last}},{{/-last}}{{/optionalParams}}, {{#vendorExtensions}}{{#x-is-generic}}innerType, {{/x-is-generic}}{{/vendorExtensions}}null);
367+
}
368+
{{/optionalParams.0}}
369+
{{/operation}}
262370

263371
{{> api_helpers}}
264372
}

0 commit comments

Comments
 (0)