All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Java.libraries.native.api.mustache Maven / Gradle / Ivy

There is a newer version: 7.10.0
Show newest version
{{>licenseInfo}}
package {{package}};

import {{invokerPackage}}.ApiClient;
import {{invokerPackage}}.ApiException;
import {{invokerPackage}}.ApiResponse;
import {{invokerPackage}}.Pair;

{{#imports}}
import {{import}};
{{/imports}}

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.function.Consumer;

{{^fullJavaUtil}}
import java.util.ArrayList;
import java.util.StringJoiner;
import java.util.List;
import java.util.Map;
{{/fullJavaUtil}}
{{#asyncNative}}

import java.util.concurrent.CompletableFuture;
{{/asyncNative}}

{{>generatedAnnotation}}
{{#operations}}
public class {{classname}} {
  private final HttpClient memberVarHttpClient;
  private final ObjectMapper memberVarObjectMapper;
  private final String memberVarBaseUri;
  private final Consumer memberVarInterceptor;
  private final Duration memberVarReadTimeout;
  private final Consumer> memberVarResponseInterceptor;

  public {{classname}}() {
    this(new ApiClient());
  }

  public {{classname}}(ApiClient apiClient) {
    memberVarHttpClient = apiClient.getHttpClient();
    memberVarObjectMapper = apiClient.getObjectMapper();
    memberVarBaseUri = apiClient.getBaseUri();
    memberVarInterceptor = apiClient.getRequestInterceptor();
    memberVarReadTimeout = apiClient.getReadTimeout();
    memberVarResponseInterceptor = apiClient.getResponseInterceptor();
  }
  {{#asyncNative}}

  private ApiException getApiException(String operationId, HttpResponselocalVarResponse) {
    return new ApiException(localVarResponse.statusCode(),
        operationId + " call received non-success response",
        localVarResponse.headers(),
        localVarResponse.body());
  }
  {{/asyncNative}}

  {{#operation}}
  {{#vendorExtensions.x-group-parameters}}
  {{#hasParams}}
  /**
   * {{summary}}
   * {{notes}}
   * @param apiRequest {@link API{{operationId}}Request}
   {{#returnType}}
   * @return {{#asyncNative}}CompletableFuture<{{/asyncNative}}{{returnType}}{{#asyncNative}}>{{/asyncNative}}
   {{/returnType}}
   * @throws ApiException if fails to make API call
   {{#isDeprecated}}
   * @deprecated
   {{/isDeprecated}}
   {{#externalDocs}}
   * {{description}}
   * @see {{summary}} Documentation
   {{/externalDocs}}
   */
  {{#isDeprecated}}
  @Deprecated
  {{/isDeprecated}}
  public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}(API{{operationId}}Request apiRequest) throws ApiException {
    {{#allParams}}
    {{{dataType}}} {{paramName}} = apiRequest.{{paramName}}();
    {{/allParams}}
    {{#returnType}}return {{/returnType}}{{^returnType}}{{#asyncNative}}return {{/asyncNative}}{{/returnType}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
  }

  /**
   * {{summary}}
   * {{notes}}
   * @param apiRequest {@link API{{operationId}}Request}
   * @return {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}}
   * @throws ApiException if fails to make API call
   {{#isDeprecated}}
   * @deprecated
   {{/isDeprecated}}
   {{#externalDocs}}
   * {{description}}
   * @see {{summary}} Documentation
   {{/externalDocs}}
   */
  {{#isDeprecated}}
  @Deprecated
  {{/isDeprecated}}
  public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo(API{{operationId}}Request apiRequest) throws ApiException {
    {{#allParams}}
    {{{dataType}}} {{paramName}} = apiRequest.{{paramName}}();
    {{/allParams}}
    return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
  }

  {{/hasParams}}
  {{/vendorExtensions.x-group-parameters}}
  /**
   * {{summary}}
   * {{notes}}
   {{#allParams}}
   * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/isContainer}}{{/required}}
   {{/allParams}}
   {{#returnType}}
   * @return {{#asyncNative}}CompletableFuture<{{/asyncNative}}{{returnType}}{{#asyncNative}}>{{/asyncNative}}
   {{/returnType}}
   * @throws ApiException if fails to make API call
   {{#isDeprecated}}
   * @deprecated
   {{/isDeprecated}}
   {{#externalDocs}}
   * {{description}}
   * @see {{summary}} Documentation
   {{/externalDocs}}
   */
  {{#isDeprecated}}
  @Deprecated
  {{/isDeprecated}}
  public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
    {{^asyncNative}}
    {{#returnType}}ApiResponse<{{{returnType}}}> localVarResponse = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
    {{#returnType}}
    return localVarResponse.getData();
    {{/returnType}}
    {{/asyncNative}}
    {{#asyncNative}}
    try {
      HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
      return memberVarHttpClient.sendAsync(
          localVarRequestBuilder.build(),
          HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
            if (localVarResponse.statusCode()/ 100 != 2) {
              return CompletableFuture.failedFuture(getApiException("{{operationId}}", localVarResponse));
            }
            {{#returnType}}
            try {
              return CompletableFuture.completedFuture(
                  memberVarObjectMapper.readValue(localVarResponse.body(), new TypeReference<{{{returnType}}}>() {})
              );
            } catch (IOException e) {
              return CompletableFuture.failedFuture(new ApiException(e));
            }
            {{/returnType}}
            {{^returnType}}
            return CompletableFuture.completedFuture(null);
            {{/returnType}}
      });
    }
    catch (ApiException e) {
      return CompletableFuture.failedFuture(e);
    }
    {{/asyncNative}}
  }

  /**
   * {{summary}}
   * {{notes}}
   {{#allParams}}
   * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/isContainer}}{{/required}}
   {{/allParams}}
   * @return {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}}
   * @throws ApiException if fails to make API call
   {{#isDeprecated}}
   * @deprecated
   {{/isDeprecated}}
   {{#externalDocs}}
   * {{description}}
   * @see {{summary}} Documentation
   {{/externalDocs}}
   */
  {{#isDeprecated}}
  @Deprecated
  {{/isDeprecated}}
  public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
    {{^asyncNative}}
    HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
    try {
      HttpResponse localVarResponse = memberVarHttpClient.send(
          localVarRequestBuilder.build(),
          HttpResponse.BodyHandlers.ofInputStream());
      if (memberVarResponseInterceptor != null) {
        memberVarResponseInterceptor.accept(localVarResponse);
      }
      if (localVarResponse.statusCode()/ 100 != 2) {
        throw new ApiException(localVarResponse.statusCode(),
            "{{operationId}} call received non-success response",
            localVarResponse.headers(),
            localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
      }
      return new ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>(
          localVarResponse.statusCode(),
          localVarResponse.headers().map(),
          {{#returnType}}memberVarObjectMapper.readValue(localVarResponse.body(), new TypeReference<{{{returnType}}}>() {}){{/returnType}}{{^returnType}}null{{/returnType}}
        );
    } catch (IOException e) {
      throw new ApiException(e);
    }
    catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new ApiException(e);
    }
    {{/asyncNative}}
    {{#asyncNative}}
    try {
      HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
      return memberVarHttpClient.sendAsync(
          localVarRequestBuilder.build(),
          HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
            if (localVarResponse.statusCode()/ 100 != 2) {
              return CompletableFuture.failedFuture(getApiException("{{operationId}}", localVarResponse));
            }
            {{#returnType}}
            try {
              return CompletableFuture.completedFuture(
                  new ApiResponse<{{{returnType}}}>(
                      localVarResponse.statusCode(),
                      localVarResponse.headers().map(),
                      memberVarObjectMapper.readValue(localVarResponse.body(), new TypeReference<{{{returnType}}}>() {}))
              );
            } catch (IOException e) {
              return CompletableFuture.failedFuture(new ApiException(e));
            }
            {{/returnType}}
            {{^returnType}}
            return CompletableFuture.completedFuture(
                new ApiResponse(localVarResponse.statusCode(), localVarResponse.headers().map(), null)
            );
            {{/returnType}}
        }
      );
    }
    catch (ApiException e) {
      return CompletableFuture.failedFuture(e);
    }
    {{/asyncNative}}
  }

  private HttpRequest.Builder {{operationId}}RequestBuilder({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException {
    {{#allParams}}
    {{#required}}
    // verify the required parameter '{{paramName}}' is set
    if ({{paramName}} == null) {
      throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}");
    }
    {{/required}}
    {{/allParams}}

    HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

    {{! Switch delimiters for baseName so we can write constants like "{query}" }}
    String localVarPath = "{{{path}}}"{{#pathParams}}
        .replace({{=<% %>=}}"{<%baseName%>}"<%={{ }}=%>, ApiClient.urlEncode({{{paramName}}}.toString())){{/pathParams}};

    {{#hasQueryParams}}
    {{javaUtilPrefix}}List localVarQueryParams = new {{javaUtilPrefix}}ArrayList<>();
    {{#queryParams}}
      {{#collectionFormat}}
    localVarQueryParams.addAll(ApiClient.parameterToPairs("{{{collectionFormat}}}", "{{baseName}}", {{paramName}}));
      {{/collectionFormat}}
      {{^collectionFormat}}
    localVarQueryParams.addAll(ApiClient.parameterToPairs("{{baseName}}", {{paramName}}));
      {{/collectionFormat}}
    {{/queryParams}}

    if (!localVarQueryParams.isEmpty()) {
      {{javaUtilPrefix}}StringJoiner queryJoiner = new StringJoiner("&");
      localVarQueryParams.forEach(p -> queryJoiner.add(p.getName() + '=' + p.getValue()));
      localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + queryJoiner.toString()));
    } else {
      localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
    }
    {{/hasQueryParams}}
    {{^hasQueryParams}}
    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
    {{/hasQueryParams}}

    {{#headerParams}}
    if ({{paramName}} != null) {
      localVarRequestBuilder.header("{{baseName}}", {{paramName}}.toString());
    }
    {{/headerParams}}
    {{#bodyParam}}
    localVarRequestBuilder.header("Content-Type", "application/json");
    {{/bodyParam}}
    localVarRequestBuilder.header("Accept", "application/json");

    {{#bodyParam}}
    try {
      byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes({{paramName}});
      localVarRequestBuilder.method("{{httpMethod}}", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
    } catch (IOException e) {
      throw new ApiException(e);
    }
    {{/bodyParam}}
    {{^bodyParam}}
    localVarRequestBuilder.method("{{httpMethod}}", HttpRequest.BodyPublishers.noBody());
    {{/bodyParam}}
    if (memberVarReadTimeout != null) {
      localVarRequestBuilder.timeout(memberVarReadTimeout);
    }
    if (memberVarInterceptor != null) {
      memberVarInterceptor.accept(localVarRequestBuilder);
    }
    return localVarRequestBuilder;
  }
  {{#vendorExtensions.x-group-parameters}}
  {{#hasParams}}

  public static final class API{{operationId}}Request {
    {{#requiredParams}}
    private {{{dataType}}} {{paramName}}; // {{description}} (required)
    {{/requiredParams}}
    {{#optionalParams}}
    private {{{dataType}}} {{paramName}}; // {{description}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/isContainer}}
    {{/optionalParams}}

    private API{{operationId}}Request(Builder builder) {
      {{#requiredParams}}
      this.{{paramName}} = builder.{{paramName}};
      {{/requiredParams}}
      {{#optionalParams}}
      this.{{paramName}} = builder.{{paramName}};
      {{/optionalParams}}
    }
    {{#allParams}}
    public {{{dataType}}} {{paramName}}() {
      return {{paramName}};
    }
    {{/allParams}}
    public static Builder newBuilder() {
      return new Builder();
    }

    public static class Builder {
      {{#requiredParams}}
      private {{{dataType}}} {{paramName}};
      {{/requiredParams}}
      {{#optionalParams}}
      private {{{dataType}}} {{paramName}};
      {{/optionalParams}}

      {{#allParams}}
      public Builder {{paramName}}({{{dataType}}} {{paramName}}) {
        this.{{paramName}} = {{paramName}};
        return this;
      }
      {{/allParams}}
      public API{{operationId}}Request build() {
        return new API{{operationId}}Request(this);
      }
    }
  }

  {{/hasParams}}
  {{/vendorExtensions.x-group-parameters}}
  {{/operation}}
}
{{/operations}}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy