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

io.linguarobot.aws.cdk.maven.context.AwsClientProviderBuilder Maven / Gradle / Ivy

package io.linguarobot.aws.cdk.maven.context;

import com.google.common.collect.ImmutableMap;
import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder;
import software.amazon.awssdk.core.SdkClient;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;


public class AwsClientProviderBuilder {

    private final Map, Function> factories;

    public AwsClientProviderBuilder() {
        this.factories = new HashMap<>();
    }

    public , C extends SdkClient> AwsClientProviderBuilder withClientFactory(Class clientType,
                                                                                                              Function factory) {
        factories.put(clientType, factory);
        return this;
    }

    public AwsClientProvider build() {
        Map, Function> clientFactories = ImmutableMap.copyOf(factories);

        return new AwsClientProvider() {

            @Override
            public  T getClient(Class clientType, String environment) {
                Function clientFactory = clientFactories.get(clientType);
                if (clientFactory == null) {
                    throw new IllegalArgumentException("There's no factory registered for " + clientType.getSimpleName() + " client");
                }

                return clientType.cast(clientFactory.apply(environment));
            }

        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy