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

io.sphere.sdk.client.SphereClientConfigUtils Maven / Gradle / Ivy

There is a newer version: 1.0.0-M26
Show newest version
package io.sphere.sdk.client;

import org.apache.commons.lang3.tuple.ImmutablePair;

import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.function.Function;
import java.util.stream.Collectors;

import static io.sphere.sdk.client.SphereClientConfig.*;
import static io.sphere.sdk.client.ClientPackage.*;
import static java.util.Arrays.asList;

final class SphereClientConfigUtils {
    private SphereClientConfigUtils() {
    }

    public static SphereClientConfig ofEnvironmentVariables(final String prefix, final Function getEnvironmentVariable) {
        Objects.requireNonNull(prefix);

        final Map configMap = asList(ENVIRONMENT_VARIABLE_API_URL_SUFFIX, ENVIRONMENT_VARIABLE_AUTH_URL_SUFFIX, ENVIRONMENT_VARIABLE_PROJECT_KEY_SUFFIX, ENVIRONMENT_VARIABLE_CLIENT_ID_SUFFIX, ENVIRONMENT_VARIABLE_CLIENT_SECRET_SUFFIX).stream()
                .map(suffix -> {
                    final String key = buildEnvKey(prefix, suffix);
                    final String nullableValue = getEnvironmentVariable.apply(key);
                    return new ImmutablePair<>(suffix, nullableValue);
                })
                .filter(pair -> null != pair.getRight())
                .collect(Collectors.toMap(ImmutablePair::getLeft, ImmutablePair::getRight));

        final Function throwExceptionOnAbsent = key -> throwEnvException(prefix, buildEnvKey(prefix, key));

        final String projectKey = configMap.computeIfAbsent(ENVIRONMENT_VARIABLE_PROJECT_KEY_SUFFIX, throwExceptionOnAbsent);
        final String clientId = configMap.computeIfAbsent(ENVIRONMENT_VARIABLE_CLIENT_ID_SUFFIX, throwExceptionOnAbsent);
        final String clientSecret = configMap.computeIfAbsent(ENVIRONMENT_VARIABLE_CLIENT_SECRET_SUFFIX, throwExceptionOnAbsent);
        final String apiUrl = configMap.getOrDefault(ENVIRONMENT_VARIABLE_API_URL_SUFFIX, API_URL);
        final String authUrl = configMap.getOrDefault(ENVIRONMENT_VARIABLE_AUTH_URL_SUFFIX, AUTH_URL);

        return SphereClientConfig.of(projectKey, clientId, clientSecret).withApiUrl(apiUrl).withAuthUrl(authUrl);
    }

    private static String throwEnvException(final String prefix, final String missingKey) {
        throw new IllegalArgumentException(
                "Missing environment variable '" + missingKey + "'.\n" +
                        "Usage:\n" +
                        "export " + buildEnvKey(prefix, ENVIRONMENT_VARIABLE_PROJECT_KEY_SUFFIX) + "=\"YOUR project key\"\n" +
                        "export " + buildEnvKey(prefix, ENVIRONMENT_VARIABLE_CLIENT_ID_SUFFIX) + "=\"YOUR client id\"\n" +
                        "export " + buildEnvKey(prefix, ENVIRONMENT_VARIABLE_CLIENT_SECRET_SUFFIX) + "=\"YOUR client secret\"\n" +
                        "#optional:\n" +
                        "export " + buildEnvKey(prefix, ENVIRONMENT_VARIABLE_API_URL_SUFFIX) + "=\"https://api.sphere.io\"\n" +
                        "export " + buildEnvKey(prefix, ENVIRONMENT_VARIABLE_AUTH_URL_SUFFIX) + "=\"https://auth.sphere.io\""
        );
    }

    private static String buildEnvKey(final String prefix, final String suffix) {
        return prefix + "_" + suffix;
    }

    private static String buildPropKey(final String prefix, final String suffix) {
        return prefix + suffix;
    }

    public static SphereClientConfig ofProperties(final Properties properties, final String prefix) {
        final String projectKey = extract(properties, prefix, PROPERTIES_KEY_PROJECT_KEY_SUFFIX);
        final String clientId = extract(properties, prefix, PROPERTIES_KEY_CLIENT_ID_SUFFIX);
        final String clientSecret = extract(properties, prefix, PROPERTIES_KEY_CLIENT_SECRET_SUFFIX);
        final String apiUrl = extract(properties, prefix, PROPERTIES_KEY_API_URL_SUFFIX, API_URL);
        final String authUrl = extract(properties, prefix, PROPERTIES_KEY_AUTH_URL_SUFFIX, AUTH_URL);
        return SphereClientConfig.of(projectKey, clientId, clientSecret).withApiUrl(apiUrl).withAuthUrl(authUrl);
    }

    private static String extract(final Properties properties, final String prefix, final String suffix, final String defaultValue) {
        return properties.getProperty(buildPropKey(prefix, suffix), defaultValue);
    }

    private static String extract(final Properties properties, final String prefix, final String suffix) {
        final String mapKey = buildPropKey(prefix, suffix);
        return properties.computeIfAbsent(mapKey, key -> throwPropertiesException(prefix, mapKey)).toString();
    }

    private static String throwPropertiesException(final String prefix, final String missingKey) {
        throw new IllegalArgumentException(
                "Missing property value '" + missingKey + "'.\n" +
                        "Usage:\n" +
                        "" + buildPropKey(prefix, PROPERTIES_KEY_PROJECT_KEY_SUFFIX) + "=YOUR project key\n" +
                        "" + buildPropKey(prefix, PROPERTIES_KEY_CLIENT_ID_SUFFIX) + "=YOUR client id\n" +
                        "" + buildPropKey(prefix, PROPERTIES_KEY_CLIENT_SECRET_SUFFIX) + "=YOUR client secret\n" +
                        "#optional:\n" +
                        "" + buildPropKey(prefix, PROPERTIES_KEY_API_URL_SUFFIX) + "=https://api.sphere.io\n" +
                        "" + buildPropKey(prefix, PROPERTIES_KEY_AUTH_URL_SUFFIX) + "=https://auth.sphere.io" +
                        "#don't use quotes for the property values\n"
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy