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

com.commercetools.sunrise.ctp.client.SphereClientProvider Maven / Gradle / Ivy

The newest version!
package com.commercetools.sunrise.ctp.client;

import com.google.inject.Provider;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.client.SphereClientConfig;
import io.sphere.sdk.client.SphereClientFactory;
import play.inject.ApplicationLifecycle;

import javax.inject.Inject;

import static java.util.concurrent.CompletableFuture.completedFuture;

public final class SphereClientProvider implements Provider {

    private final ApplicationLifecycle applicationLifecycle;
    private final SphereClientConfig sphereClientConfig;

    @Inject
    SphereClientProvider(final ApplicationLifecycle applicationLifecycle, final SphereClientConfig sphereClientConfig) {
        this.applicationLifecycle = applicationLifecycle;
        this.sphereClientConfig = sphereClientConfig;
    }

    @Override
    public SphereClient get() {
        final SphereClient sphereClient = SphereClientFactory.of().createClient(sphereClientConfig);
        applicationLifecycle.addStopHook(() -> {
            sphereClient.close();
            return completedFuture(null);
        });
        return sphereClient;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy