Skip to content

Commit fbe1c49

Browse files
committed
made header assignment more explicit, per each method
1 parent e6e570c commit fbe1c49

File tree

1 file changed

+118
-34
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/native

1 file changed

+118
-34
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache

Lines changed: 118 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ public class {{classname}} {
6464
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
6565
private final Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;
6666
67-
// Custom headers to be sent with every request from this API client
68-
private final Map<String, String> extraHeaders = new java.util.HashMap<>();
69-
7067
public {{classname}}() {
7168
this(Configuration.getDefaultApiClient());
7269
}
@@ -81,27 +78,6 @@ public class {{classname}} {
8178
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
8279
}
8380

84-
/**
85-
* Add a custom header to be sent with every request from this API client.
86-
* @param name Header name
87-
* @param value Header value
88-
* @return this
89-
*/
90-
public {{classname}} addHeader(String name, String value) {
91-
this.extraHeaders.put(name, value);
92-
return this;
93-
}
94-
95-
/**
96-
* Remove a custom header.
97-
* @param name Header name
98-
* @return this
99-
*/
100-
public {{classname}} removeHeader(String name) {
101-
this.extraHeaders.remove(name);
102-
return this;
103-
}
104-
10581
{{#asyncNative}}
10682

10783
private ApiException getApiException(String operationId, HttpResponse<String> response) {
@@ -153,11 +129,40 @@ public class {{classname}} {
153129
@Deprecated
154130
{{/isDeprecated}}
155131
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest) throws ApiException {
132+
return {{operationId}}(apiRequest, null);
133+
}
134+
135+
/**
136+
* {{summary}}
137+
* {{notes}}
138+
* @param apiRequest {@link API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request}
139+
* @param headers Optional headers to include in the request
140+
{{#returnType}}
141+
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}{{returnType}}{{#asyncNative}}&gt;{{/asyncNative}}
142+
{{/returnType}}
143+
{{^returnType}}
144+
{{#asyncNative}}
145+
* @return CompletableFuture&lt;Void&gt;
146+
{{/asyncNative}}
147+
{{/returnType}}
148+
* @throws ApiException if fails to make API call
149+
{{#isDeprecated}}
150+
* @deprecated
151+
{{/isDeprecated}}
152+
{{#externalDocs}}
153+
* {{description}}
154+
* @see <a href="{{url}}">{{summary}} Documentation</a>
155+
{{/externalDocs}}
156+
*/
157+
{{#isDeprecated}}
158+
@Deprecated
159+
{{/isDeprecated}}
160+
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest, Map<String, String> headers) throws ApiException {
156161
{{#allParams}}
157162
{{>nullable_var_annotations}}
158163
{{{dataType}}} {{paramName}} = apiRequest.{{paramName}}();
159164
{{/allParams}}
160-
{{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
165+
{{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams}}, {{/allParams}}headers);
161166
}
162167

163168
/**
@@ -178,10 +183,32 @@ public class {{classname}} {
178183
@Deprecated
179184
{{/isDeprecated}}
180185
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest) throws ApiException {
186+
return {{operationId}}WithHttpInfo(apiRequest, null);
187+
}
188+
189+
/**
190+
* {{summary}}
191+
* {{notes}}
192+
* @param apiRequest {@link API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request}
193+
* @param headers Optional headers to include in the request
194+
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}ApiResponse&lt;{{returnType}}{{^returnType}}Void{{/returnType}}&gt;{{#asyncNative}}&gt;{{/asyncNative}}
195+
* @throws ApiException if fails to make API call
196+
{{#isDeprecated}}
197+
* @deprecated
198+
{{/isDeprecated}}
199+
{{#externalDocs}}
200+
* {{description}}
201+
* @see <a href="{{url}}">{{summary}} Documentation</a>
202+
{{/externalDocs}}
203+
*/
204+
{{#isDeprecated}}
205+
@Deprecated
206+
{{/isDeprecated}}
207+
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest, Map<String, String> headers) throws ApiException {
181208
{{#allParams}}
182209
{{{dataType}}} {{paramName}} = apiRequest.{{paramName}}();
183210
{{/allParams}}
184-
return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
211+
return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams}}, {{/allParams}}headers);
185212
}
186213

187214
{{/hasParams}}
@@ -213,15 +240,46 @@ public class {{classname}} {
213240
@Deprecated
214241
{{/isDeprecated}}
215242
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
243+
return {{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}, null);
244+
}
245+
246+
/**
247+
* {{summary}}
248+
* {{notes}}
249+
{{#allParams}}
250+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}
251+
{{/allParams}}
252+
* @param headers Optional headers to include in the request
253+
{{#returnType}}
254+
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}{{returnType}}{{#asyncNative}}&gt;{{/asyncNative}}
255+
{{/returnType}}
256+
{{^returnType}}
257+
{{#asyncNative}}
258+
* @return CompletableFuture&lt;Void&gt;
259+
{{/asyncNative}}
260+
{{/returnType}}
261+
* @throws ApiException if fails to make API call
262+
{{#isDeprecated}}
263+
* @deprecated
264+
{{/isDeprecated}}
265+
{{#externalDocs}}
266+
* {{description}}
267+
* @see <a href="{{url}}">{{summary}} Documentation</a>
268+
{{/externalDocs}}
269+
*/
270+
{{#isDeprecated}}
271+
@Deprecated
272+
{{/isDeprecated}}
273+
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams}}, {{/allParams}}Map<String, String> headers) throws ApiException {
216274
{{^asyncNative}}
217-
{{#returnType}}ApiResponse<{{{.}}}> localVarResponse = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
275+
{{#returnType}}ApiResponse<{{{.}}}> localVarResponse = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams}}, {{/allParams}}headers);
218276
{{#returnType}}
219277
return localVarResponse.getData();
220278
{{/returnType}}
221279
{{/asyncNative}}
222280
{{#asyncNative}}
223281
try {
224-
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
282+
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams}}, {{/allParams}}headers);
225283
return memberVarHttpClient.sendAsync(
226284
localVarRequestBuilder.build(),
227285
HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
@@ -269,8 +327,32 @@ public class {{classname}} {
269327
@Deprecated
270328
{{/isDeprecated}}
271329
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
330+
return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}, null);
331+
}
332+
333+
/**
334+
* {{summary}}
335+
* {{notes}}
336+
{{#allParams}}
337+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}
338+
{{/allParams}}
339+
* @param headers Optional headers to include in the request
340+
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}ApiResponse&lt;{{returnType}}{{^returnType}}Void{{/returnType}}&gt;{{#asyncNative}}&gt;{{/asyncNative}}
341+
* @throws ApiException if fails to make API call
342+
{{#isDeprecated}}
343+
* @deprecated
344+
{{/isDeprecated}}
345+
{{#externalDocs}}
346+
* {{description}}
347+
* @see <a href="{{url}}">{{summary}} Documentation</a>
348+
{{/externalDocs}}
349+
*/
350+
{{#isDeprecated}}
351+
@Deprecated
352+
{{/isDeprecated}}
353+
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams}}, {{/allParams}}Map<String, String> headers) throws ApiException {
272354
{{^asyncNative}}
273-
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
355+
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams}}, {{/allParams}}headers);
274356
try {
275357
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
276358
localVarRequestBuilder.build(),
@@ -352,7 +434,7 @@ public class {{classname}} {
352434
{{/asyncNative}}
353435
{{#asyncNative}}
354436
try {
355-
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
437+
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams}}, {{/allParams}}headers);
356438
return memberVarHttpClient.sendAsync(
357439
localVarRequestBuilder.build(),
358440
HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
@@ -389,7 +471,7 @@ public class {{classname}} {
389471
{{/asyncNative}}
390472
}
391473

392-
private HttpRequest.Builder {{operationId}}RequestBuilder({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
474+
private HttpRequest.Builder {{operationId}}RequestBuilder({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams}}, {{/allParams}}Map<String, String> headers) throws ApiException {
393475
{{#allParams}}
394476
{{#required}}
395477
// verify the required parameter '{{paramName}}' is set
@@ -594,9 +676,11 @@ public class {{classname}} {
594676
if (memberVarReadTimeout != null) {
595677
localVarRequestBuilder.timeout(memberVarReadTimeout);
596678
}
597-
// Add custom headers
598-
for (Map.Entry<String, String> entry : extraHeaders.entrySet()) {
599-
localVarRequestBuilder.header(entry.getKey(), entry.getValue());
679+
// Add custom headers if provided
680+
if (headers != null) {
681+
for (Map.Entry<String, String> entry : headers.entrySet()) {
682+
localVarRequestBuilder.header(entry.getKey(), entry.getValue());
683+
}
600684
}
601685
if (memberVarInterceptor != null) {
602686
memberVarInterceptor.accept(localVarRequestBuilder);

0 commit comments

Comments
 (0)