github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/retrofit2/play24/ApiClient.mustache (about)

     1  package {{invokerPackage}};
     2  
     3  import java.io.IOException;
     4  import java.lang.annotation.Annotation;
     5  import java.lang.reflect.Type;
     6  import java.util.*;
     7  
     8  import retrofit2.Retrofit;
     9  import retrofit2.converter.scalars.ScalarsConverterFactory;
    10  import retrofit2.converter.jackson.JacksonConverterFactory;
    11  import com.fasterxml.jackson.databind.ObjectMapper;
    12  import org.openapitools.jackson.nullable.JsonNullableModule;
    13  
    14  import play.libs.Json;
    15  import play.libs.ws.WSClient;
    16  
    17  import {{invokerPackage}}.Play24CallAdapterFactory;
    18  import {{invokerPackage}}.Play24CallFactory;
    19  
    20  import okhttp3.Interceptor;
    21  import {{invokerPackage}}.auth.ApiKeyAuth;
    22  import {{invokerPackage}}.auth.Authentication;
    23  
    24  /**
    25   * API client
    26   */
    27  public class ApiClient {
    28  
    29      /** Underlying HTTP-client */
    30      private WSClient wsClient;
    31  
    32      /** Supported auths */
    33      private Map<String, Authentication> authentications;
    34  
    35      /** API base path */
    36      private String basePath = "{{{basePath}}}";
    37  
    38      public ApiClient(WSClient wsClient) {
    39          this();
    40          this.wsClient = wsClient;
    41      }
    42  
    43      public ApiClient() {
    44          // Setup authentications (key: authentication name, value: authentication).
    45          authentications = new HashMap<{{#supportJava6}}String, Authentication{{/supportJava6}}>();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
    46          // authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}}
    47          // authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}"));{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}
    48          authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
    49          // authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
    50          // Prevent the authentications from being modified.
    51          authentications = Collections.unmodifiableMap(authentications);
    52  
    53      }
    54  
    55      /**
    56       * Creates a retrofit2 client for given API interface
    57       */
    58      public <S> S createService(Class<S> serviceClass) {
    59          if(!basePath.endsWith("/")) {
    60              basePath = basePath + "/";
    61          }
    62  
    63          Map<String, String> extraHeaders = new HashMap<{{#supportJava6}}String, String{{/supportJava6}}>();
    64          Map<String, String> extraCookies = new HashMap<{{#supportJava6}}String, String{{/supportJava6}}>();
    65          List<Pair> extraQueryParams = new ArrayList<{{#supportJava6}}Pair{{/supportJava6}}>();
    66  
    67          for (String authName : authentications.keySet()) {
    68              Authentication auth = authentications.get(authName);
    69              if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
    70  
    71              auth.applyToParams(extraQueryParams, extraHeaders, extraCookies);
    72          }
    73  
    74          ObjectMapper mapper = Json.mapper();
    75          JsonNullableModule jnm = new JsonNullableModule();
    76          mapper.registerModule(jnm);
    77  
    78          return new Retrofit.Builder()
    79                         .baseUrl(basePath)
    80                         .addConverterFactory(ScalarsConverterFactory.create())
    81                         .addConverterFactory(JacksonConverterFactory.create(mapper))
    82                         .callFactory(new Play24CallFactory(wsClient, extraHeaders, extraCookies, extraQueryParams))
    83                         .addCallAdapterFactory(new Play24CallAdapterFactory())
    84                         .build()
    85                         .create(serviceClass);
    86      }
    87  
    88      /**
    89       * Helper method to set API base path
    90       */
    91      public ApiClient setBasePath(String basePath) {
    92          this.basePath = basePath;
    93          return this;
    94      }
    95  
    96      /**
    97       * Get authentications (key: authentication name, value: authentication).
    98       */
    99      public Map<String, Authentication> getAuthentications() {
   100          return authentications;
   101      }
   102  
   103      /**
   104       * Get authentication for the given name.
   105       *
   106       * @param authName The authentication name
   107       * @return The authentication, null if not found
   108       */
   109      public Authentication getAuthentication(String authName) {
   110          return authentications.get(authName);
   111      }
   112  
   113      /**
   114       * Helper method to set API key value for the first API key authentication.
   115       */
   116      public ApiClient setApiKey(String apiKey) {
   117          for (Authentication auth : authentications.values()) {
   118              if (auth instanceof ApiKeyAuth) {
   119                  ((ApiKeyAuth) auth).setApiKey(apiKey);
   120                  return this;
   121              }
   122          }
   123  
   124          throw new RuntimeException("No API key authentication configured!");
   125      }
   126  
   127      /**
   128       * Helper method to set API key prefix for the first API key authentication.
   129       */
   130      public ApiClient setApiKeyPrefix(String apiKeyPrefix) {
   131          for (Authentication auth : authentications.values()) {
   132              if (auth instanceof ApiKeyAuth) {
   133                  ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
   134                  return this;
   135              }
   136          }
   137  
   138          throw new RuntimeException("No API key authentication configured!");
   139      }
   140  
   141  
   142  }