org.openapitools.client.auth.ApiKeyAuth Java Examples

The following examples show how to use org.openapitools.client.auth.ApiKeyAuth. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 7 votes vote down vote up
protected void init() {
    // Use RFC3339 format for date and datetime.
    // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
    this.dateFormat = new RFC3339DateFormat();

    // Use UTC as the default time zone.
    this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    // Set default User-Agent.
    setUserAgent("Java-SDK");

    // Setup authentications (key: authentication name, value: authentication).
    authentications = new HashMap<String, Authentication>();
    authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
    authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query"));
    authentications.put("http_basic_test", new HttpBasicAuth());
    authentications.put("petstore_auth", new OAuth());
    // Prevent the authentications from being modified.
    authentications = Collections.unmodifiableMap(authentications);
}
 
Example #2
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
protected void init() {
    // Use RFC3339 format for date and datetime.
    // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
    this.dateFormat = new RFC3339DateFormat();

    // Use UTC as the default time zone.
    this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    // Set default User-Agent.
    setUserAgent("Java-SDK");

    // Setup authentications (key: authentication name, value: authentication).
    authentications = new HashMap<String, Authentication>();
    authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
    authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query"));
    authentications.put("http_basic_test", new HttpBasicAuth());
    authentications.put("petstore_auth", new OAuth());
    // Prevent the authentications from being modified.
    authentications = Collections.unmodifiableMap(authentications);
}
 
Example #3
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
public ApiClient() {
  json = new JSON();
  httpClient = buildHttpClient(debugging);

  this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ROOT);

  // Use UTC as the default time zone.
  this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

  this.json.setDateFormat((DateFormat) dateFormat.clone());

  // Set default User-Agent.
  setUserAgent("OpenAPI-Generator/1.0.0/java");

  // Setup authentications (key: authentication name, value: authentication).
  authentications = new HashMap<String, Authentication>();
  authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
  authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query"));
  authentications.put("http_basic_test", new HttpBasicAuth());
  authentications.put("petstore_auth", new OAuth());
  // Prevent the authentications from being modified.
  authentications = Collections.unmodifiableMap(authentications);
}
 
Example #4
Source File: PetApiTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
/**
 * Find pet by ID
 * <p>
 * Returns a single pet
 *
 * @throws ApiException if the Api call fails
 */
@Test
public void getPetByIdTest() throws ApiException {
    ApiClient defaultClient = Configuration.getDefaultApiClient();
    defaultClient.setBasePath("http://petstore.swagger.io:80/v2");

    // Configure API key authorization: api_key
    ApiKeyAuth api_key = (ApiKeyAuth) defaultClient.getAuthentication("api_key");
    api_key.setApiKey("YOUR API KEY");
    // Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
    //api_key.setApiKeyPrefix("Token");

    PetApi apiInstance = new PetApi(defaultClient);
    Long petId = 56L; // Long | ID of pet to return
    try {
        Pet result = apiInstance.getPetById(petId);
        System.out.println(result);
    } catch (ApiException e) {
        System.err.println("Exception when calling PetApi#getPetById");
        System.err.println("Status code: " + e.getCode());
        System.err.println("Reason: " + e.getResponseBody());
        System.err.println("Response headers: " + e.getResponseHeaders());
    }

}
 
Example #5
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to configure the first api key found
 * @param apiKey API key
 * @return ApiClient
 */
public ApiClient setApiKey(String apiKey) {
  for(Interceptor apiAuthorization : apiAuthorizations.values()) {
    if (apiAuthorization instanceof ApiKeyAuth) {
      ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
      keyAuth.setApiKey(apiKey);
      return this;
    }
  }
  return this;
}
 
Example #6
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key value for the first API key authentication.
 * @param apiKey the API key
 */
public void setApiKey(String apiKey) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKey(apiKey);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #7
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key prefix for the first API key authentication.
 * @param apiKeyPrefix API key prefix
 */
public void setApiKeyPrefix(String apiKeyPrefix) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #8
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key value for the first API key authentication.
 * @param apiKey the API key
 */
public void setApiKey(String apiKey) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKey(apiKey);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #9
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key prefix for the first API key authentication.
 * @param apiKeyPrefix API key prefix
 */
public void setApiKeyPrefix(String apiKeyPrefix) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #10
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to configure the first api key found
 * @param apiKey API key
 * @return ApiClient
 */
public ApiClient setApiKey(String apiKey) {
  for(Interceptor apiAuthorization : apiAuthorizations.values()) {
    if (apiAuthorization instanceof ApiKeyAuth) {
      ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
      keyAuth.setApiKey(apiKey);
      return this;
    }
  }
  return this;
}
 
Example #11
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public ApiClient() {
    init();
    initHttpClient();

    // Setup authentications (key: authentication name, value: authentication).
    authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
    authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query"));
    authentications.put("http_basic_test", new HttpBasicAuth());
    authentications.put("petstore_auth", new OAuth());
    // Prevent the authentications from being modified.
    authentications = Collections.unmodifiableMap(authentications);
}
 
Example #12
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public ApiClient(OkHttpClient client) {
    init();

    httpClient = client;

    // Setup authentications (key: authentication name, value: authentication).
    authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
    authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query"));
    authentications.put("http_basic_test", new HttpBasicAuth());
    authentications.put("petstore_auth", new OAuth());
    // Prevent the authentications from being modified.
    authentications = Collections.unmodifiableMap(authentications);
}
 
Example #13
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public ApiClient(String basePath, String clientId, String clientSecret, Map<String, String> parameters) {
    init();
    if (basePath != null) {
        this.basePath = basePath;
    }

    String tokenUrl = "";
    if (!"".equals(tokenUrl) && !URI.create(tokenUrl).isAbsolute()) {
        URI uri = URI.create(getBasePath());
        tokenUrl = uri.getScheme() + ":" +
            (uri.getAuthority() != null ? "//" + uri.getAuthority() : "") +
            tokenUrl;
        if (!URI.create(tokenUrl).isAbsolute()) {
            throw new IllegalArgumentException("OAuth2 token URL must be an absolute URL");
        }
    }
    RetryingOAuth retryingOAuth = new RetryingOAuth(tokenUrl, clientId, OAuthFlow.implicit, clientSecret, parameters);
    authentications.put(
            "petstore_auth",
            retryingOAuth
    );
    initHttpClient(Collections.<Interceptor>singletonList(retryingOAuth));
    // Setup authentications (key: authentication name, value: authentication).
    authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
    authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query"));
    authentications.put("http_basic_test", new HttpBasicAuth());

    // Prevent the authentications from being modified.
    authentications = Collections.unmodifiableMap(authentications);
}
 
Example #14
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key value for the first API key authentication.
 *
 * @param apiKey API key
 */
public void setApiKey(String apiKey) {
    for (Authentication auth : authentications.values()) {
        if (auth instanceof ApiKeyAuth) {
            ((ApiKeyAuth) auth).setApiKey(apiKey);
            return;
        }
    }
    throw new RuntimeException("No API key authentication configured!");
}
 
Example #15
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key prefix for the first API key authentication.
 *
 * @param apiKeyPrefix API key prefix
 */
public void setApiKeyPrefix(String apiKeyPrefix) {
    for (Authentication auth : authentications.values()) {
        if (auth instanceof ApiKeyAuth) {
            ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
            return;
        }
    }
    throw new RuntimeException("No API key authentication configured!");
}
 
Example #16
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key value for the first API key authentication.
 *
 * @param apiKey API key
 */
public void setApiKey(String apiKey) {
    for (Authentication auth : authentications.values()) {
        if (auth instanceof ApiKeyAuth) {
            ((ApiKeyAuth) auth).setApiKey(apiKey);
            return;
        }
    }
    throw new RuntimeException("No API key authentication configured!");
}
 
Example #17
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public ApiClient() {
  objectMapper = new ObjectMapper();
  objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
  objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
  objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
  objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
  ThreeTenModule module = new ThreeTenModule();
  module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT);
  module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME);
  module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME);
  objectMapper.registerModule(module);
  objectMapper.setDateFormat(ApiClient.buildDefaultDateFormat());

  dateFormat = ApiClient.buildDefaultDateFormat();

  // Set default User-Agent.
  setUserAgent("OpenAPI-Generator/1.0.0/java");

  // Setup authentications (key: authentication name, value: authentication).
  authentications = new HashMap<String, Authentication>();
  authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
  authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query"));
  authentications.put("http_basic_test", new HttpBasicAuth());
  authentications.put("petstore_auth", new OAuth());
  // Prevent the authentications from being modified.
  authentications = Collections.unmodifiableMap(authentications);

  rebuildHttpClient();
}
 
Example #18
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key value for the first API key authentication.
 * @param apiKey the API key
 */
public void setApiKey(String apiKey) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKey(apiKey);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #19
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key prefix for the first API key authentication.
 * @param apiKeyPrefix API key prefix
 */
public void setApiKeyPrefix(String apiKeyPrefix) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #20
Source File: ApiInvoker.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public static void initializeInstance(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery, int connectionTimeout) {
  INSTANCE = new ApiInvoker(cache, network, threadPoolSize, delivery, connectionTimeout);
  setUserAgent("OpenAPI-Generator/1.0.0/android");

  // Setup authentications (key: authentication name, value: authentication).
  INSTANCE.authentications = new HashMap<String, Authentication>();
  INSTANCE.authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
  // TODO: comment out below as OAuth does not exist
  //INSTANCE.authentications.put("petstore_auth", new OAuth());
  // Prevent the authentications from being modified.
  INSTANCE.authentications = Collections.unmodifiableMap(INSTANCE.authentications);
}
 
Example #21
Source File: ApiInvoker.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key value for the first API key authentication.
 */
public void setApiKey(String apiKey) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKey(apiKey);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #22
Source File: ApiInvoker.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key prefix for the first API key authentication.
 */
public void setApiKeyPrefix(String apiKeyPrefix) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #23
Source File: ApiInvoker.java    From swaggy-jenkins with MIT License 5 votes vote down vote up
public static void initializeInstance(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery, int connectionTimeout) {
  INSTANCE = new ApiInvoker(cache, network, threadPoolSize, delivery, connectionTimeout);
  setUserAgent("OpenAPI-Generator/1.0.0/android");

  // Setup authentications (key: authentication name, value: authentication).
  INSTANCE.authentications = new HashMap<String, Authentication>();
  INSTANCE.authentications.put("jenkins_auth", new HttpBasicAuth());
  INSTANCE.authentications.put("jwt_auth", new ApiKeyAuth("header", "Authorization"));
  // Prevent the authentications from being modified.
  INSTANCE.authentications = Collections.unmodifiableMap(INSTANCE.authentications);
}
 
Example #24
Source File: ApiInvoker.java    From swaggy-jenkins with MIT License 5 votes vote down vote up
/**
 * Helper method to set API key value for the first API key authentication.
 */
public void setApiKey(String apiKey) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKey(apiKey);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #25
Source File: ApiInvoker.java    From swaggy-jenkins with MIT License 5 votes vote down vote up
/**
 * Helper method to set API key prefix for the first API key authentication.
 */
public void setApiKeyPrefix(String apiKeyPrefix) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #26
Source File: ApiInvoker.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key value for the first API key authentication.
 */
public void setApiKey(String apiKey) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKey(apiKey);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #27
Source File: ApiInvoker.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key prefix for the first API key authentication.
 */
public void setApiKeyPrefix(String apiKeyPrefix) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
      return;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #28
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to configure authentications which respects aliases of API keys.
 *
 * @param secrets Hash map from authentication name to its secret.
 */
public ApiClient configureApiKeys(HashMap<String, String> secrets) {
  for (Map.Entry<String, Authentication> authEntry : authentications.entrySet()) {
    Authentication auth = authEntry.getValue();
    if (auth instanceof ApiKeyAuth) {
      String name = authEntry.getKey();
      // respect x-auth-id-alias property
      name = authenticationLookup.containsKey(name) ? authenticationLookup.get(name) : name;
      if (secrets.containsKey(name)) {
        ((ApiKeyAuth) auth).setApiKey(secrets.get(name));
      }
    }
  }
  return this;
}
 
Example #29
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to set API key value for the first API key authentication.
 *
 * @param apiKey API key
 */
public ApiClient setApiKey(String apiKey) {
  for (Authentication auth : authentications.values()) {
    if (auth instanceof ApiKeyAuth) {
      ((ApiKeyAuth) auth).setApiKey(apiKey);
      return this;
    }
  }
  throw new RuntimeException("No API key authentication configured!");
}
 
Example #30
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to configure authentications which respects aliases of API keys.
 *
 * @param secrets Hash map from authentication name to its secret.
 */
public ApiClient configureApiKeys(HashMap<String, String> secrets) {
  for (Map.Entry<String, Authentication> authEntry : authentications.entrySet()) {
    Authentication auth = authEntry.getValue();
    if (auth instanceof ApiKeyAuth) {
      String name = authEntry.getKey();
      // respect x-auth-id-alias property
      name = authenticationLookup.containsKey(name) ? authenticationLookup.get(name) : name;
      if (secrets.containsKey(name)) {
        ((ApiKeyAuth) auth).setApiKey(secrets.get(name));
      }
    }
  }
  return this;
}