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

com.lajospolya.spotifyapiwrapper.internal.CompletableFutureAsyncResponse Maven / Gradle / Ivy

Go to download

This project wraps the Spotify public API in order to allow users to intuitively use it

There is a newer version: 3.0.RELEASE
Show newest version
package com.lajospolya.spotifyapiwrapper.internal;

import com.lajospolya.spotifyapiwrapper.response.SpotifyErrorContainer;
import com.lajospolya.spotifyapiwrapper.spotifyexception.SpotifyResponseException;

import java.lang.reflect.Type;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;

public class CompletableFutureAsyncResponse implements ISpotifyAsyncResponse, T>
{
    private final HttpResponseHelper helper;
    private final CompletableFuture> asyncContainer;
    private final Type type;
    // Can be either of type T (on success) or type SpotifyErrorContainer (on error)
    private CompletableFuture serializedValue;

    private Consumer successConsumer = null;
    private Consumer errorConsumer = null;

    public CompletableFutureAsyncResponse(CompletableFuture> asyncContainer, Type type)
    {
        this.helper = new HttpResponseHelper();
        this.asyncContainer = asyncContainer;
        this.type = type;
        validateResponseAsync();
    }

    @Override
    public void block() throws ExecutionException, InterruptedException
    {
        serializedValue.get();
    }

    private void validateResponseAsync()
    {
        serializedValue = asyncContainer.thenApply((response) -> {
            int statusCode = response.statusCode();
            if(helper.isClientErrorStatusCode(statusCode) || helper.isServerErrorStatusCode(statusCode))
            {
                SpotifyErrorContainer body = helper.serializeBody(response, SpotifyErrorContainer.class);
                if(errorConsumer != null)
                {
                    errorConsumer.accept(body);
                }
                return body;
            }
            else
            {
                T body = helper.serializeBody(response, type);

                if(successConsumer != null)
                {
                    successConsumer.accept(body);
                }
                return body;
            }
        });
    }

    @Override
    public CompletableFutureAsyncResponse success(Consumer func) throws SpotifyResponseException
    {
        if(successConsumer != null)
        {
            throw new SpotifyResponseException("Can only have one Success consumer");
        }
        successConsumer = func;
        return this;
    }

    @Override
    public CompletableFutureAsyncResponse error(Consumer func) throws SpotifyResponseException
    {
        if(errorConsumer != null)
        {
            throw new SpotifyResponseException("Can only have one Error consumer");
        }
        errorConsumer = func;
        return this;
    }

    @Override
    public boolean isDone()
    {
        return serializedValue.isDone();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy