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

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

package io.blockfrost.sdk.impl;

import io.blockfrost.sdk.api.PoolService;
import io.blockfrost.sdk.api.exception.APIException;
import io.blockfrost.sdk.api.exception.RuntimeAPIException;
import io.blockfrost.sdk.api.model.*;
import io.blockfrost.sdk.api.util.ConfigHelper;
import io.blockfrost.sdk.api.util.OrderEnum;
import io.blockfrost.sdk.impl.helper.ValidationHelper;
import io.blockfrost.sdk.impl.retrofit.PoolsApi;
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 PoolServiceImpl extends BaseService implements PoolService {

    PoolsApi poolsApi;

    public PoolServiceImpl(String baseUrl, String projectId) {
        super(baseUrl, projectId);
        poolsApi = getRetrofit().create(PoolsApi.class);
    }

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

    @Override
    public List getPools(int count, int page, OrderEnum order) throws APIException {

        ValidationHelper.validateCount(count);

        Call> poolsCall = poolsApi.poolsGet(getProjectId(), count, page, order.name());

        try {
            Response> poolsResponse = poolsCall.execute();
            return processResponse(poolsResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching pools ", exp);
        }
    }

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

    @Override
    public List getAllPools(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 getPools(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 pools", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;
    }

    @Override
    public List getAllPools() throws APIException {
        return getAllPools(OrderEnum.asc);
    }

    @Override
    public List getRetiredPools(int count, int page, OrderEnum order) throws APIException {

        ValidationHelper.validateCount(count);

        Call> poolsCall = poolsApi.poolsRetiredGet(getProjectId(), count, page, order.name());

        try {
            Response> poolsResponse = poolsCall.execute();
            return processResponse(poolsResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching retired pools ", exp);
        }
    }

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

    @Override
    public List getAllRetiredPools(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 getRetiredPools(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 retired pools", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;
    }

    @Override
    public List getAllRetiredPools() throws APIException {
        return getAllRetiredPools(OrderEnum.asc);
    }

    @Override
    public List getRetiringPools(int count, int page, OrderEnum order) throws APIException {

        ValidationHelper.validateCount(count);

        Call> poolsCall = poolsApi.poolsRetiringGet(getProjectId(), count, page, order.name());

        try {
            Response> poolsResponse = poolsCall.execute();
            return processResponse(poolsResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching retiring pools ", exp);
        }

    }

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

    @Override
    public List getAllRetiringPools(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 getRetiringPools(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 retiring pools", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

    @Override
    public List getAllRetiringPools() throws APIException {
        return getAllRetiringPools(OrderEnum.asc);
    }

    @Override
    public Pool getPool(String poolId) throws APIException {

        validatePoolId(poolId);

        Call poolCall = poolsApi.poolsPoolIdGet(getProjectId(), poolId);

        try {
            Response poolResponse = poolCall.execute();
            return processResponse(poolResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching pool for poolId: " + poolId, exp);
        }
    }

    @Override
    public List getPoolHistory(String poolId, int count, int page, OrderEnum order) throws APIException {

        validatePoolId(poolId);

        ValidationHelper.validateCount(count);

        Call> poolHistoryCall = poolsApi.poolsPoolIdHistoryGet(getProjectId(), poolId, count, page, order.name());

        try {
            Response> poolHistoryResponse = poolHistoryCall.execute();
            return processResponse(poolHistoryResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching history for poolId: " + poolId, exp);
        }
    }

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

    @Override
    public List getEntirePoolHistory(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 getPoolHistory(poolId, 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 history for pool id: " + poolId, e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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

    @Override
    public PoolMetadata getPoolMetadata(String poolId) throws APIException {

        validatePoolId(poolId);

        Call poolMetadataCall = poolsApi.poolsPoolIdMetadataGet(getProjectId(), poolId);

        try {
            Response poolMetadataResponse = poolMetadataCall.execute();
            return processResponse(poolMetadataResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching pool metadata for poolId: " + poolId, exp);
        }
    }

    @Override
    public List getPoolRelays(String poolId) throws APIException {

        validatePoolId(poolId);

        Call> poolRelayCall = poolsApi.poolsPoolIdRelaysGet(getProjectId(), poolId);

        try {
            Response> poolRelaysResponse = poolRelayCall.execute();
            return processResponse(poolRelaysResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching pool relays for poolId: " + poolId, exp);
        }
    }

    @Override
    public List getPoolDelegators(String poolId, int count, int page, OrderEnum order) throws APIException {

        validatePoolId(poolId);

        ValidationHelper.validateCount(count);

        Call> poolDelegatorCall = poolsApi.poolsPoolIdDelegatorsGet(getProjectId(), poolId, count, page, order.name());

        try {
            Response> poolDelegatorsResponse = poolDelegatorCall.execute();
            return processResponse(poolDelegatorsResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching pool delegators for poolId: " + poolId, exp);
        }
    }

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


    @Override
    public List getAllPoolDelegators(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 getPoolDelegators(poolId, 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 delegators for poolId: " + poolId, e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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

    @Override
    public List getPoolBlocks(String poolId, int count, int page, OrderEnum order) throws APIException {

        validatePoolId(poolId);

        ValidationHelper.validateCount(count);

        Call> poolBlockCall = poolsApi.poolsPoolIdBlocksGet(getProjectId(), poolId, count, page, order.name());

        try {
            Response> poolBlocksResponse = poolBlockCall.execute();
            return processResponse(poolBlocksResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching pool blocks for poolId: " + poolId, exp);
        }
    }

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

    @Override
    public List getAllPoolBlocks(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 getPoolBlocks(poolId, 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 poolId: " + poolId, e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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

    @Override
    public List getPoolUpdates(String poolId, int count, int page, OrderEnum order) throws APIException {
        validatePoolId(poolId);

        ValidationHelper.validateCount(count);

        Call> poolUpdateCall = poolsApi.poolsPoolIdUpdatesGet(getProjectId(), poolId, count, page, order.name());

        try {
            Response> poolUpdatesResponse = poolUpdateCall.execute();
            return processResponse(poolUpdatesResponse);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching pool blocks for poolId: " + poolId, exp);
        }
    }

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

    @Override
    public List getAllPoolUpdates(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 getPoolUpdates(poolId, 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 poolId: " + poolId, e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy