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

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

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

import io.sphere.sdk.http.HttpClient;

import java.util.concurrent.CompletionStage;

import static io.sphere.sdk.client.SphereAuth.*;

final class OnDemandSphereAccessTokenSupplier extends AutoCloseableService implements SphereAccessTokenSupplier {
    private final TokensSupplier tokensSupplier;
    private boolean isClosed = false;

    private OnDemandSphereAccessTokenSupplier(final SphereAuthConfig config, final HttpClient httpClient, final boolean closeHttpClient) {
        tokensSupplier = TokensSupplierImpl.of(config, httpClient, closeHttpClient);
    }

    @Override
    protected synchronized void internalClose() {
        if (!isClosed) {
            tokensSupplier.close();
            isClosed = true;
        }
    }

    @Override
    public CompletionStage get() {
        return tokensSupplier.get().thenApply(Tokens::getAccessToken);
    }

    public static OnDemandSphereAccessTokenSupplier of(final SphereAuthConfig config, final HttpClient httpClient, final boolean closeHttpClient) {
        return new OnDemandSphereAccessTokenSupplier(config, httpClient, closeHttpClient);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy