github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/retrofit2/play-common/auth/ApiKeyAuth.mustache (about)

     1  {{>licenseInfo}}
     2  
     3  package {{invokerPackage}}.auth;
     4  
     5  import {{invokerPackage}}.Pair;
     6  
     7  import java.util.Map;
     8  import java.util.List;
     9  
    10  /**
    11   * Holds ApiKey auth info
    12   */
    13  {{>generatedAnnotation}}
    14  public class ApiKeyAuth implements Authentication {
    15    private final String location;
    16    private final String paramName;
    17  
    18    private String apiKey;
    19    private String apiKeyPrefix;
    20  
    21    public ApiKeyAuth(String location, String paramName) {
    22      this.location = location;
    23      this.paramName = paramName;
    24    }
    25  
    26    public String getLocation() {
    27      return location;
    28    }
    29  
    30    public String getParamName() {
    31      return paramName;
    32    }
    33  
    34    public String getApiKey() {
    35      return apiKey;
    36    }
    37  
    38    public void setApiKey(String apiKey) {
    39      this.apiKey = apiKey;
    40    }
    41  
    42    public String getApiKeyPrefix() {
    43      return apiKeyPrefix;
    44    }
    45  
    46    public void setApiKeyPrefix(String apiKeyPrefix) {
    47      this.apiKeyPrefix = apiKeyPrefix;
    48    }
    49  
    50    @Override
    51    public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
    52      if (apiKey == null) {
    53        return;
    54      }
    55      String value;
    56      if (apiKeyPrefix != null) {
    57        value = apiKeyPrefix + " " + apiKey;
    58      } else {
    59        value = apiKey;
    60      }
    61      if ("query".equals(location)) {
    62        queryParams.add(new Pair(paramName, value));
    63      } else if ("header".equals(location)) {
    64        headerParams.put(paramName, value);
    65      } else if ("cookie".equals(location)) {
    66        cookieParams.put(paramName, value);
    67      }
    68    }
    69  }