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

io.blockfrost.sdk.impl.EpochServiceImpl Maven / Gradle / Ivy

package io.blockfrost.sdk.impl;

import io.blockfrost.sdk.api.EpochService;
import io.blockfrost.sdk.api.exception.APIException;
import io.blockfrost.sdk.api.exception.RuntimeAPIException;
import io.blockfrost.sdk.api.model.Epoch;
import io.blockfrost.sdk.api.model.EpochParam;
import io.blockfrost.sdk.api.model.Stake;
import io.blockfrost.sdk.api.util.ConfigHelper;
import io.blockfrost.sdk.api.util.OrderEnum;
import io.blockfrost.sdk.impl.retrofit.EpochsApi;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class EpochServiceImpl extends BaseService implements EpochService {

    EpochsApi epochsApi;

    public EpochServiceImpl(String baseUrl, String projectId) {
        super(baseUrl, projectId);
        epochsApi = getRetrofit().create(EpochsApi.class);
    }

    @Override
    public Epoch getLatestEpoch() throws APIException {
        Call latestEpochCall = epochsApi.epochsLatestGet(getProjectId());

        try {
            Response latestEpochResponse = latestEpochCall.execute();
            return processResponse(latestEpochResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching latest epoch", exp);
        }
    }

    @Override
    public EpochParam getLatestEpochParam() throws APIException {
        Call latestEpochParamCall = epochsApi.epochsLatestParametersGet(getProjectId());

        try {
            Response latestEpochParamResponse = latestEpochParamCall.execute();
            return processResponse(latestEpochParamResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching latest epoch parameters", exp);
        }
    }

    @Override
    public Epoch getEpoch(int number) throws APIException {

        Call epochCall = epochsApi.epochsNumberGet(getProjectId(), number);

        try {
            Response epochResponse = epochCall.execute();
            return processResponse(epochResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching epoch for epoch number: " + number, exp);
        }
    }

    @Override
    public List getNextEpochs(int number, int count, int page) throws APIException {

        Call> nextEpochsCall = epochsApi.epochsNumberNextGet(getProjectId(), number, count, page);

        try {
            Response> nextEpochsResponse = nextEpochsCall.execute();
            return processResponse(nextEpochsResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching next epochs for epoch number: " + number, exp);
        }
    }

    @Override
    public List getAllNextEpochs(int number) throws APIException {

        List responseList = new ArrayList<>();
        boolean stopExecution = false;
        int currentPageCount = 1;
        int numThreads = ConfigHelper.INSTANCE.getThreadCount();

        while (!stopExecution) {

            List>> completableFutures = new ArrayList<>();

            for (int i = 0; i < numThreads; i++) {

                int finalCurrentPageCount = currentPageCount + i;

                completableFutures.add(CompletableFuture.supplyAsync(() -> {
                    try {
                        return getNextEpochs(number, getDefaultFetchSize(), finalCurrentPageCount);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching all next epochs for epoch number: " + number, e );
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

    @Override
    public List getPreviousEpochs(int number, int count, int page) throws APIException {

        Call> previousEpochsCall = epochsApi.epochsNumberPreviousGet(getProjectId(), number, count, page);

        try {
            Response> previousEpochsResponse = previousEpochsCall.execute();
            return processResponse(previousEpochsResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching previous epochs for epoch number: " + number, exp);
        }
    }

    @Override
    public List getAllPreviousEpochs(int number) throws APIException {

        List responseList = new ArrayList<>();
        boolean stopExecution = false;
        int currentPageCount = 1;
        int numThreads = ConfigHelper.INSTANCE.getThreadCount();

        while (!stopExecution) {

            List>> completableFutures = new ArrayList<>();

            for (int i = 0; i < numThreads; i++) {

                int finalCurrentPageCount = currentPageCount + i;

                completableFutures.add(CompletableFuture.supplyAsync(() -> {
                    try {
                        return getPreviousEpochs(number, getDefaultFetchSize(), finalCurrentPageCount);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching all previous epochs for epoch number: " + number, e );
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

    @Override
    public List getActiveStakesForEpoch(int number, int count, int page) throws APIException {

        Call> activeStakesCall = epochsApi.epochsNumberStakesGet(getProjectId(), number, count, page);

        try {
            Response> activeStakesResponse = activeStakesCall.execute();
            return processResponse(activeStakesResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching active stakes for epoch number: " + number, exp);
        }
    }

    @Override
    public List getAllActiveStakesForEpoch(int number) throws APIException {

        List responseList = new ArrayList<>();
        boolean stopExecution = false;
        int currentPageCount = 1;
        int numThreads = ConfigHelper.INSTANCE.getThreadCount();

        while (!stopExecution) {

            List>> completableFutures = new ArrayList<>();

            for (int i = 0; i < numThreads; i++) {

                int finalCurrentPageCount = currentPageCount + i;

                completableFutures.add(CompletableFuture.supplyAsync(() -> {
                    try {
                        return getActiveStakesForEpoch(number, getDefaultFetchSize(), finalCurrentPageCount);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching all active stakes for epoch number: " + number, e );
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

    @Override
    public List getActiveStakesForEpochAndPool(int number, String poolId, int count, int page) throws APIException {

        if (poolId == null || poolId.equals("")) {
            throw new APIException("PoolId cannot be null or empty");
        }

        Call> activeStakesCall = epochsApi.epochsNumberStakesPoolIdGet(getProjectId(), number, poolId, count, page);

        try {
            Response> activeStakesResponse = activeStakesCall.execute();
            return processResponse(activeStakesResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching active stakes for epoch number: " + number + " and poolId: " + poolId, exp);
        }
    }

    @Override
    public List getAllActiveStakesForEpochAndPool(int number, String poolId) throws APIException {

        List responseList = new ArrayList<>();
        boolean stopExecution = false;
        int currentPageCount = 1;
        int numThreads = ConfigHelper.INSTANCE.getThreadCount();

        while (!stopExecution) {

            List>> completableFutures = new ArrayList<>();

            for (int i = 0; i < numThreads; i++) {

                int finalCurrentPageCount = currentPageCount + i;

                completableFutures.add(CompletableFuture.supplyAsync(() -> {
                    try {
                        return getActiveStakesForEpochAndPool(number, poolId, getDefaultFetchSize(), finalCurrentPageCount);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching all active stakes for epoch number: " + number + " and poolId: " + poolId, e );
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

    @Override
    public EpochParam getEpochParam(int number) throws APIException {
        Call epochParamCall = epochsApi.epochsNumberParametersGet(getProjectId(), number);

        try {
            Response epochParamResponse = epochParamCall.execute();
            return processResponse(epochParamResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching epoch parameters for epoch: " + number, exp);
        }
    }

    @Override
    public List getBlocksForEpoch(int number, int count, int page, OrderEnum order) throws APIException {
        Call> blocksCall = epochsApi.epochsNumberBlocksGet(getProjectId(), number, count, page, order.name());

        try {
            Response> blocksResponse = blocksCall.execute();
            return processResponse(blocksResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching blocks for epoch: " + number, exp);
        }
    }

    @Override
    public List getBlocksForEpoch(int number, int count, int page) throws APIException {
        return getBlocksForEpoch(number, count, page, OrderEnum.asc);
    }

 /*   @Override
    public List getBlocksForEpoch(int number, OrderEnum order) throws APIException {

        List responseList = new ArrayList<>();
        boolean stopExecution = false;
        int currentPageCount = 1;
        int numThreads = ConfigHelper.threadCount();

        while (!stopExecution) {

            List>> completableFutures = new ArrayList<>();

            for (int i = 0; i < numThreads; i++) {

                int finalCurrentPageCount = currentPageCount + i;

                completableFutures.add(CompletableFuture.supplyAsync(() -> {
                    try {
                        return getBlocksForEpoch(number, getDefaultFetchSize(), finalCurrentPageCount, order);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching all blocks for epoch number: " + number, e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }*/

    //@Override
    //TODO not used
//    public List getBlocksForEpoch(int number) throws APIException {
//        return getBlocksForEpoch(number, OrderEnum.asc);
//    }

    @Override
    public List getBlocksForEpochAndPool(int number, String poolId, int count, int page, OrderEnum order) throws APIException {
        Call> blocksForEpochAndPoolCall = epochsApi.epochsNumberBlocksPoolIdGet(getProjectId(), number, poolId, count, page, order.name());

        try {
            Response> blocksForEpochAndPoolResponse = blocksForEpochAndPoolCall.execute();
            return processResponse(blocksForEpochAndPoolResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching blocks for epoch: " + number + " and poolId: " + poolId, exp);
        }
    }

    @Override
    public List getBlocksForEpochAndPool(int number, String poolId, int count, int page) throws APIException {
        return getBlocksForEpochAndPool(number, poolId, count, page, OrderEnum.asc);
    }

    @Override
    public List getAllBlocksForEpochAndPool(int number, String poolId, OrderEnum order) throws APIException {

        List responseList = new ArrayList<>();
        boolean stopExecution = false;
        int currentPageCount = 1;
        int numThreads = ConfigHelper.INSTANCE.getThreadCount();

        while (!stopExecution) {

            List>> completableFutures = new ArrayList<>();

            for (int i = 0; i < numThreads; i++) {

                int finalCurrentPageCount = currentPageCount + i;

                completableFutures.add(CompletableFuture.supplyAsync(() -> {
                    try {
                        return getBlocksForEpochAndPool(number, poolId, getDefaultFetchSize(), finalCurrentPageCount);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching all blocks for epoch number: " + number + " and poolId: " + poolId, e );
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

    @Override
    public List getAllBlocksForEpochAndPool(int number, String poolId) throws APIException {
        return getAllBlocksForEpochAndPool(number, poolId, OrderEnum.asc);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy