Skip to content

[Java][native] Add ability to add header to specific calls #21495

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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 @@ -77,6 +77,7 @@ public class {{classname}} {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
}

{{#asyncNative}}

private ApiException getApiException(String operationId, HttpResponse<String> response) {
Expand Down Expand Up @@ -128,11 +129,40 @@ public class {{classname}} {
@Deprecated
{{/isDeprecated}}
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 {
{{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}(apiRequest, null);
}

/**
* {{summary}}
* {{notes}}
* @param apiRequest {@link API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request}
* @param headers Optional headers to include in the request
{{#returnType}}
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}{{returnType}}{{#asyncNative}}&gt;{{/asyncNative}}
{{/returnType}}
{{^returnType}}
{{#asyncNative}}
* @return CompletableFuture&lt;Void&gt;
{{/asyncNative}}
{{/returnType}}
* @throws ApiException if fails to make API call
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
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 {
{{#allParams}}
{{>nullable_var_annotations}}
{{{dataType}}} {{paramName}} = apiRequest.{{paramName}}();
{{/allParams}}
{{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
{{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
}

/**
Expand All @@ -153,10 +183,32 @@ public class {{classname}} {
@Deprecated
{{/isDeprecated}}
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest) throws ApiException {
return {{operationId}}WithHttpInfo(apiRequest, null);
}

/**
* {{summary}}
* {{notes}}
* @param apiRequest {@link API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request}
* @param headers Optional headers to include in the request
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}ApiResponse&lt;{{returnType}}{{^returnType}}Void{{/returnType}}&gt;{{#asyncNative}}&gt;{{/asyncNative}}
* @throws ApiException if fails to make API call
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
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 {
{{#allParams}}
{{{dataType}}} {{paramName}} = apiRequest.{{paramName}}();
{{/allParams}}
return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
}

{{/hasParams}}
Expand Down Expand Up @@ -188,15 +240,46 @@ public class {{classname}} {
@Deprecated
{{/isDeprecated}}
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 {
{{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}null);
}

/**
* {{summary}}
* {{notes}}
{{#allParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}
{{/allParams}}
* @param headers Optional headers to include in the request
{{#returnType}}
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}{{returnType}}{{#asyncNative}}&gt;{{/asyncNative}}
{{/returnType}}
{{^returnType}}
{{#asyncNative}}
* @return CompletableFuture&lt;Void&gt;
{{/asyncNative}}
{{/returnType}}
* @throws ApiException if fails to make API call
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
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}}{{#hasParams}}, {{/hasParams}}Map<String, String> headers) throws ApiException {
{{^asyncNative}}
{{#returnType}}ApiResponse<{{{.}}}> localVarResponse = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
{{#returnType}}ApiResponse<{{{.}}}> localVarResponse = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
{{#returnType}}
return localVarResponse.getData();
{{/returnType}}
{{/asyncNative}}
{{#asyncNative}}
try {
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
return memberVarHttpClient.sendAsync(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
Expand Down Expand Up @@ -244,8 +327,32 @@ public class {{classname}} {
@Deprecated
{{/isDeprecated}}
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}null);
}

/**
* {{summary}}
* {{notes}}
{{#allParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}
{{/allParams}}
* @param headers Optional headers to include in the request
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}ApiResponse&lt;{{returnType}}{{^returnType}}Void{{/returnType}}&gt;{{#asyncNative}}&gt;{{/asyncNative}}
* @throws ApiException if fails to make API call
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Map<String, String> headers) throws ApiException {
{{^asyncNative}}
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
try {
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
Expand Down Expand Up @@ -327,7 +434,7 @@ public class {{classname}} {
{{/asyncNative}}
{{#asyncNative}}
try {
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}headers);
return memberVarHttpClient.sendAsync(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
Expand Down Expand Up @@ -364,7 +471,7 @@ public class {{classname}} {
{{/asyncNative}}
}

private HttpRequest.Builder {{operationId}}RequestBuilder({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
private HttpRequest.Builder {{operationId}}RequestBuilder({{#allParams}}{{>nullable_var_annotations}} {{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Map<String, String> headers) throws ApiException {
{{#allParams}}
{{#required}}
// verify the required parameter '{{paramName}}' is set
Expand Down Expand Up @@ -569,6 +676,12 @@ public class {{classname}} {
if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
// Add custom headers if provided
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
localVarRequestBuilder.header(entry.getKey(), entry.getValue());
}
}
if (memberVarInterceptor != null) {
memberVarInterceptor.accept(localVarRequestBuilder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public AuthApi(ApiClient apiClient) {
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
}


protected ApiException getApiException(String operationId, HttpResponse<InputStream> response) throws IOException {
String body = response.body() == null ? null : new String(response.body().readAllBytes());
String message = formatExceptionMessage(operationId, response.statusCode(), body);
Expand All @@ -88,7 +89,18 @@ private String formatExceptionMessage(String operationId, int statusCode, String
* @throws ApiException if fails to make API call
*/
public String testAuthHttpBasic() throws ApiException {
ApiResponse<String> localVarResponse = testAuthHttpBasicWithHttpInfo();
return testAuthHttpBasic(null);
}

/**
* To test HTTP basic authentication
* To test HTTP basic authentication
* @param headers Optional headers to include in the request
* @return String
* @throws ApiException if fails to make API call
*/
public String testAuthHttpBasic(Map<String, String> headers) throws ApiException {
ApiResponse<String> localVarResponse = testAuthHttpBasicWithHttpInfo(headers);
return localVarResponse.getData();
}

Expand All @@ -99,7 +111,18 @@ public String testAuthHttpBasic() throws ApiException {
* @throws ApiException if fails to make API call
*/
public ApiResponse<String> testAuthHttpBasicWithHttpInfo() throws ApiException {
HttpRequest.Builder localVarRequestBuilder = testAuthHttpBasicRequestBuilder();
return testAuthHttpBasicWithHttpInfo(null);
}

/**
* To test HTTP basic authentication
* To test HTTP basic authentication
* @param headers Optional headers to include in the request
* @return ApiResponse&lt;String&gt;
* @throws ApiException if fails to make API call
*/
public ApiResponse<String> testAuthHttpBasicWithHttpInfo(Map<String, String> headers) throws ApiException {
HttpRequest.Builder localVarRequestBuilder = testAuthHttpBasicRequestBuilder(headers);
try {
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
Expand Down Expand Up @@ -135,7 +158,7 @@ public ApiResponse<String> testAuthHttpBasicWithHttpInfo() throws ApiException {
}
}

private HttpRequest.Builder testAuthHttpBasicRequestBuilder() throws ApiException {
private HttpRequest.Builder testAuthHttpBasicRequestBuilder(Map<String, String> headers) throws ApiException {

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

Expand All @@ -149,6 +172,12 @@ private HttpRequest.Builder testAuthHttpBasicRequestBuilder() throws ApiExceptio
if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
// Add custom headers if provided
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
localVarRequestBuilder.header(entry.getKey(), entry.getValue());
}
}
if (memberVarInterceptor != null) {
memberVarInterceptor.accept(localVarRequestBuilder);
}
Expand All @@ -162,7 +191,18 @@ private HttpRequest.Builder testAuthHttpBasicRequestBuilder() throws ApiExceptio
* @throws ApiException if fails to make API call
*/
public String testAuthHttpBearer() throws ApiException {
ApiResponse<String> localVarResponse = testAuthHttpBearerWithHttpInfo();
return testAuthHttpBearer(null);
}

/**
* To test HTTP bearer authentication
* To test HTTP bearer authentication
* @param headers Optional headers to include in the request
* @return String
* @throws ApiException if fails to make API call
*/
public String testAuthHttpBearer(Map<String, String> headers) throws ApiException {
ApiResponse<String> localVarResponse = testAuthHttpBearerWithHttpInfo(headers);
return localVarResponse.getData();
}

Expand All @@ -173,7 +213,18 @@ public String testAuthHttpBearer() throws ApiException {
* @throws ApiException if fails to make API call
*/
public ApiResponse<String> testAuthHttpBearerWithHttpInfo() throws ApiException {
HttpRequest.Builder localVarRequestBuilder = testAuthHttpBearerRequestBuilder();
return testAuthHttpBearerWithHttpInfo(null);
}

/**
* To test HTTP bearer authentication
* To test HTTP bearer authentication
* @param headers Optional headers to include in the request
* @return ApiResponse&lt;String&gt;
* @throws ApiException if fails to make API call
*/
public ApiResponse<String> testAuthHttpBearerWithHttpInfo(Map<String, String> headers) throws ApiException {
HttpRequest.Builder localVarRequestBuilder = testAuthHttpBearerRequestBuilder(headers);
try {
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
Expand Down Expand Up @@ -209,7 +260,7 @@ public ApiResponse<String> testAuthHttpBearerWithHttpInfo() throws ApiException
}
}

private HttpRequest.Builder testAuthHttpBearerRequestBuilder() throws ApiException {
private HttpRequest.Builder testAuthHttpBearerRequestBuilder(Map<String, String> headers) throws ApiException {

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

Expand All @@ -223,6 +274,12 @@ private HttpRequest.Builder testAuthHttpBearerRequestBuilder() throws ApiExcepti
if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
// Add custom headers if provided
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
localVarRequestBuilder.header(entry.getKey(), entry.getValue());
}
}
if (memberVarInterceptor != null) {
memberVarInterceptor.accept(localVarRequestBuilder);
}
Expand Down
Loading