github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/google-api-client/api.mustache (about)

     1  package {{package}};
     2  
     3  import {{invokerPackage}}.ApiClient;
     4  
     5  {{#imports}}import {{import}};
     6  {{/imports}}
     7  
     8  import com.fasterxml.jackson.core.type.TypeReference;
     9  import com.google.api.client.http.EmptyContent;
    10  import com.google.api.client.http.GenericUrl;
    11  import com.google.api.client.http.HttpContent;
    12  import com.google.api.client.http.InputStreamContent;
    13  import com.google.api.client.http.HttpMethods;
    14  import com.google.api.client.http.HttpResponse;
    15  import com.google.api.client.json.Json;
    16  
    17  import javax.ws.rs.core.UriBuilder;
    18  import java.io.IOException;
    19  import java.util.Collection;
    20  import java.util.HashMap;
    21  import java.util.Map;
    22  import java.util.List;
    23  import java.util.ArrayList;
    24  
    25  {{>generatedAnnotation}}
    26  {{#operations}}
    27  public class {{classname}} {
    28      private ApiClient apiClient;
    29  
    30      public {{classname}}() {
    31          this(new ApiClient());
    32      }
    33  
    34      public {{classname}}(ApiClient apiClient) {
    35          this.apiClient = apiClient;
    36      }
    37  
    38      public ApiClient getApiClient() {
    39          return apiClient;
    40      }
    41  
    42      public void setApiClient(ApiClient apiClient) {
    43          this.apiClient = apiClient;
    44      }
    45  
    46      {{#operation}}
    47    /**{{#summary}}
    48      * {{summary}}{{/summary}}{{#notes}}
    49      * {{notes}}{{/notes}}{{#responses}}
    50      * <p><b>{{code}}</b>{{#message}} - {{message}}{{/message}}{{/responses}}{{#allParams}}
    51      * @param {{paramName}} {{description}}{{^description}}The {{paramName}} parameter{{/description}}{{/allParams}}{{#returnType}}
    52      * @return {{returnType}}{{/returnType}}
    53      * @throws IOException if an error occurs while attempting to invoke the API{{#externalDocs}}
    54      * {{description}}
    55      * @see <a href="{{url}}">{{summary}} Documentation</a>
    56      {{/externalDocs}}
    57  
    58      **/
    59      public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws IOException {
    60          {{#returnType}}HttpResponse response = {{/returnType}}{{operationId}}ForHttpResponse({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});{{#returnType}}
    61          TypeReference<{{{returnType}}}> typeRef = new TypeReference<{{{returnType}}}>() {};
    62          return apiClient.getObjectMapper().readValue(response.getContent(), typeRef);{{/returnType}}
    63      }
    64  
    65    /**{{#summary}}
    66      * {{summary}}{{/summary}}{{#notes}}
    67      * {{notes}}{{/notes}}{{#responses}}
    68      * <p><b>{{code}}</b>{{#message}} - {{message}}{{/message}}{{/responses}}{{#requiredParams}}
    69      * @param {{paramName}} {{description}}{{^description}}The {{paramName}} parameter{{/description}}{{/requiredParams}}
    70      * @param params Map of query params. A collection will be interpreted as passing in multiple instances of the same query param.{{#returnType}}
    71      * @return {{returnType}}{{/returnType}}
    72      * @throws IOException if an error occurs while attempting to invoke the API{{#externalDocs}}
    73      * {{description}}
    74      * @see <a href="{{url}}">{{summary}} Documentation</a>
    75      {{/externalDocs}}
    76  
    77      **/
    78      public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#bodyParam}}{{^required}}{{{dataType}}} {{paramName}}, {{/required}}{{/bodyParam}}{{#requiredParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#hasRequiredParams}}, {{/hasRequiredParams}}Map<String, Object> params) throws IOException {
    79          {{#returnType}}HttpResponse response = {{/returnType}}{{operationId}}ForHttpResponse({{#bodyParam}}{{^required}}{{paramName}}, {{/required}}{{/bodyParam}}{{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#hasRequiredParams}}, {{/hasRequiredParams}}params);{{#returnType}}
    80          TypeReference<{{{returnType}}}> typeRef = new TypeReference<{{{returnType}}}>() {};
    81          return apiClient.getObjectMapper().readValue(response.getContent(), typeRef);{{/returnType}}
    82      }
    83  
    84      public HttpResponse {{operationId}}ForHttpResponse({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws IOException {
    85          {{#allParams}}{{#required}}// verify the required parameter '{{paramName}}' is set
    86          if ({{paramName}} == null) {
    87              throw new IllegalArgumentException("Missing the required parameter '{{paramName}}' when calling {{operationId}}");
    88          }{{/required}}{{/allParams}}
    89          {{#hasPathParams}}
    90          // create a map of path variables
    91          final Map<String, Object> uriVariables = new HashMap<String, Object>();{{#pathParams}}
    92          uriVariables.put("{{baseName}}", {{{paramName}}});{{/pathParams}}
    93          {{/hasPathParams}}
    94          UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "{{{path}}}");{{#hasQueryParams}}
    95          {{#queryParams}}
    96          if ({{paramName}} != null) {
    97              String key = "{{baseName}}";
    98              Object value = {{paramName}};
    99              if (value instanceof Collection) {
   100                  uriBuilder = uriBuilder.queryParam(key, ((Collection) value).toArray());
   101              } else if (value instanceof Object[]) {
   102                  uriBuilder = uriBuilder.queryParam(key, (Object[]) value);
   103              } else {
   104                  uriBuilder = uriBuilder.queryParam(key, value);
   105              }
   106          }{{/queryParams}}{{/hasQueryParams}}
   107  
   108          String localVarUrl = uriBuilder{{#hasPathParams}}.buildFromMap(uriVariables).toString();{{/hasPathParams}}{{^hasPathParams}}.build().toString();{{/hasPathParams}}
   109          GenericUrl genericUrl = new GenericUrl(localVarUrl);
   110  
   111          HttpContent content = {{#isBodyAllowed}}{{#bodyParam}}apiClient.new JacksonJsonHttpContent({{paramName}}){{/bodyParam}}{{^bodyParam}}new EmptyContent(){{/bodyParam}}{{/isBodyAllowed}}{{^isBodyAllowed}}null{{/isBodyAllowed}};
   112          return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
   113      }{{#bodyParam}}
   114  
   115        public HttpResponse {{operationId}}ForHttpResponse({{#allParams}}{{#isBodyParam}}java.io.InputStream {{paramName}}{{/isBodyParam}}{{^isBodyParam}}{{{dataType}}} {{paramName}}{{/isBodyParam}}{{^-last}}, {{/-last}}{{/allParams}}, String mediaType) throws IOException {
   116            {{#allParams}}{{#required}}// verify the required parameter '{{paramName}}' is set
   117                if ({{paramName}} == null) {
   118                throw new IllegalArgumentException("Missing the required parameter '{{paramName}}' when calling {{operationId}}");
   119                }{{/required}}{{/allParams}}
   120            {{#hasPathParams}}
   121                    // create a map of path variables
   122                    final Map<String, Object> uriVariables = new HashMap<String, Object>();{{#pathParams}}
   123                        uriVariables.put("{{baseName}}", {{{paramName}}});{{/pathParams}}
   124            {{/hasPathParams}}
   125                UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "{{{path}}}");{{#hasQueryParams}}
   126                {{#queryParams}}
   127                if ({{paramName}} != null) {
   128                    String key = "{{baseName}}";
   129                    Object value = {{paramName}};
   130                    if (value instanceof Collection) {
   131                      uriBuilder = uriBuilder.queryParam(key, ((Collection) value).toArray());
   132                    } else if (value instanceof Object[]) {
   133                      uriBuilder = uriBuilder.queryParam(key, (Object[]) value);
   134                    } else {
   135                      uriBuilder = uriBuilder.queryParam(key, value);
   136                    }
   137                }{{/queryParams}}{{/hasQueryParams}}
   138  
   139                String localVarUrl = uriBuilder{{#hasPathParams}}.buildFromMap(uriVariables).toString();{{/hasPathParams}}{{^hasPathParams}}.build().toString();{{/hasPathParams}}
   140                GenericUrl genericUrl = new GenericUrl(localVarUrl);
   141  
   142                HttpContent content = {{#bodyParam}}{{paramName}} == null ?
   143                  apiClient.new JacksonJsonHttpContent(null) :
   144                  new InputStreamContent(mediaType == null ? Json.MEDIA_TYPE : mediaType, {{paramName}}){{/bodyParam}};
   145                return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
   146        }{{/bodyParam}}
   147  
   148      public HttpResponse {{operationId}}ForHttpResponse({{#bodyParam}}{{^required}}{{{dataType}}} {{paramName}}, {{/required}}{{/bodyParam}}{{#requiredParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#hasRequiredParams}}, {{/hasRequiredParams}}Map<String, Object> params) throws IOException {
   149          {{#allParams}}{{#required}}// verify the required parameter '{{paramName}}' is set
   150          if ({{paramName}} == null) {
   151              throw new IllegalArgumentException("Missing the required parameter '{{paramName}}' when calling {{operationId}}");
   152          }{{/required}}{{/allParams}}
   153          {{#hasPathParams}}
   154          // create a map of path variables
   155          final Map<String, Object> uriVariables = new HashMap<String, Object>();{{#pathParams}}
   156          uriVariables.put("{{baseName}}", {{{paramName}}});{{/pathParams}}
   157          {{/hasPathParams}}
   158          UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "{{{path}}}");
   159  
   160          // Copy the params argument if present, to allow passing in immutable maps
   161          Map<String, Object> allParams = params == null ? new HashMap<String, Object>() : new HashMap<String, Object>(params);{{#queryParams}}{{#required}}
   162          // Add the required query param '{{paramName}}' to the map of query params
   163          allParams.put("{{paramName}}", {{paramName}});{{/required}}{{/queryParams}}
   164  
   165          for (Map.Entry<String, Object> entry: allParams.entrySet()) {
   166              String key = entry.getKey();
   167              Object value = entry.getValue();
   168  
   169              if (key != null && value != null) {
   170                  if (value instanceof Collection) {
   171                      uriBuilder = uriBuilder.queryParam(key, ((Collection) value).toArray());
   172                  } else if (value instanceof Object[]) {
   173                      uriBuilder = uriBuilder.queryParam(key, (Object[]) value);
   174                  } else {
   175                      uriBuilder = uriBuilder.queryParam(key, value);
   176                  }
   177              }
   178          }
   179  
   180          String localVarUrl = uriBuilder{{#hasPathParams}}.buildFromMap(uriVariables).toString();{{/hasPathParams}}{{^hasPathParams}}.build().toString();{{/hasPathParams}}
   181          GenericUrl genericUrl = new GenericUrl(localVarUrl);
   182  
   183          HttpContent content = {{#isBodyAllowed}}{{#bodyParam}}apiClient.new JacksonJsonHttpContent({{paramName}}){{/bodyParam}}{{^bodyParam}}new EmptyContent(){{/bodyParam}}{{/isBodyAllowed}}{{^isBodyAllowed}}null{{/isBodyAllowed}};
   184          return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
   185      }
   186  
   187  
   188      {{/operation}}
   189  }
   190  {{/operations}}