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

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

package io.blockfrost.sdk.impl;

import io.blockfrost.sdk.api.AccountService;
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.retrofit.AccountsApi;
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 AccountServiceImpl extends BaseService implements AccountService {

    private AccountsApi accountsApi;

    public AccountServiceImpl(String baseUrl, String projectId) {
        super(baseUrl, projectId);
        this.accountsApi = getApiClient(AccountsApi.class);
    }

    private void validateStakeAddress(String address) throws APIException {
        if (address == null || address.equals("")) {
            throw new APIException("Stake address cannot be null or empty");
        }
    }

    @Override
    public Account getAccountByStakeAddress(String stakeAddress) throws APIException {

        validateStakeAddress(stakeAddress);

        Call call = accountsApi.accountsStakeAddressGet(getProjectId(), stakeAddress);

        try {
            Response response = call.execute();
            return processResponse(response);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching account info by stakeAddress", exp);
        }
    }

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

        validateStakeAddress(stakeAddress);

        Call> call = accountsApi.accountsStakeAddressHistoryGet(getProjectId(), stakeAddress, count, page, order.name());

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

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

    @Override
    public List getEntireAccountHistory(String stakeAddress, 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 getAccountHistory(stakeAddress, getDefaultFetchSize(), finalCurrentPageCount, order);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching account history", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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

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

        validateStakeAddress(stakeAddress);

        Call> call = accountsApi.accountsStakeAddressRewardsGet(getProjectId(), stakeAddress, count, page, order.name());

        try {
            Response> response = call.execute();
            return processResponse(response);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching account reward history for stakeAddress: " + stakeAddress, exp);
        }
    }

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

    @Override
    public List getEntireAccountRewardHistory(String stakeAddress, 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 getAccountRewardHistory(stakeAddress, getDefaultFetchSize(), finalCurrentPageCount, order);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching account history", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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

    @Override
    public List getAccountDelegationHistory(String stakeAddress, int count, int page, OrderEnum order) throws APIException {
        validateStakeAddress(stakeAddress);

        Call> call = accountsApi.accountsStakeAddressDelegationsGet(getProjectId(), stakeAddress, count, page, order.name());

        try {
            Response> response = call.execute();
            return processResponse(response);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching account delegation history for stakeAddress: " + stakeAddress, exp);
        }
    }

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

    @Override
    public List getEntireAccountDelegationHistory(String stakeAddress, 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 getAccountDelegationHistory(stakeAddress, getDefaultFetchSize(), finalCurrentPageCount, order);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching account history", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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

    @Override
    public List getAccountRegistrationHistory(String stakeAddress, int count, int page, OrderEnum order) throws APIException {
        validateStakeAddress(stakeAddress);

        Call> call = accountsApi.accountsStakeAddressRegistrationsGet(getProjectId(), stakeAddress, count, page, order.name());

        try {
            Response> response = call.execute();
            return processResponse(response);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching account registration history for stakeAddress: " + stakeAddress, exp);
        }
    }

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

    @Override
    public List getEntireAccountRegistrationHistory(String stakeAddress, 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 getAccountRegistrationHistory(stakeAddress, getDefaultFetchSize(), finalCurrentPageCount, order);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching account history", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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

    @Override
    public List getAccountWithdrawalHistory(String stakeAddress, int count, int page, OrderEnum order) throws APIException {
        validateStakeAddress(stakeAddress);

        Call> call = accountsApi.accountsStakeAddressWithdrawalsGet(getProjectId(), stakeAddress, count, page, order.name());

        try {
            Response> response = call.execute();
            return processResponse(response);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching account withdrawal history for stakeAddress: " + stakeAddress, exp);
        }
    }

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

    @Override
    public List getEntireAccountWithdrawalHistory(String stakeAddress, 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 getAccountWithdrawalHistory(stakeAddress, getDefaultFetchSize(), finalCurrentPageCount, order);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching account history", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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

    @Override
    public List getAccountMirHistory(String stakeAddress, int count, int page, OrderEnum order) throws APIException {
        validateStakeAddress(stakeAddress);

        Call> call = accountsApi.accountsStakeAddressMirsGet(getProjectId(), stakeAddress, count, page, order.name());

        try {
            Response> response = call.execute();
            return processResponse(response);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching account MIR history for stakeAddress: " + stakeAddress, exp);
        }
    }

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


    @Override
    public List getEntireAccountMirHistory(String stakeAddress, 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 getAccountMirHistory(stakeAddress, getDefaultFetchSize(), finalCurrentPageCount, order);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching account history", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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

    @Override
    public List getAccountAddresses(String stakeAddress, int count, int page, OrderEnum order) throws APIException {
        validateStakeAddress(stakeAddress);

        Call> call = accountsApi.accountsStakeAddressAddressesGet(getProjectId(), stakeAddress, count, page, order.name());

        try {
            Response> response = call.execute();
            return processResponse(response);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching account addresses for stakeAddress: " + stakeAddress, exp);
        }
    }

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

    @Override
    public List getAllAccountAddresses(String stakeAddress, 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 getAccountAddresses(stakeAddress, getDefaultFetchSize(), finalCurrentPageCount, order);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching account history", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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

    @Override
    public List getAccountAssets(String stakeAddress, int count, int page, OrderEnum order) throws APIException {
        validateStakeAddress(stakeAddress);

        Call> call = accountsApi.accountsStakeAddressAddressesAssetsGet(getProjectId(), stakeAddress, count, page, order.name());

        try {
            Response> response = call.execute();
            return processResponse(response);
        } catch (IOException exp) {
            throw new APIException("Exception while fetching account assets for stakeAddress: " + stakeAddress, exp);
        }
    }

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

    @Override
    public List getAllAccountAssets(String stakeAddress, 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 getAccountAssets(stakeAddress, getDefaultFetchSize(), finalCurrentPageCount, order);
                    } catch (APIException e) {
                        throw new RuntimeAPIException(e);
                    }
                }));
            }

            try {
                stopExecution = fetchData(completableFutures, responseList);
            } catch (Exception e) {
                throw new APIException("Exception while fetching account history", e);
            }

            currentPageCount += numThreads;
        }

        return responseList;

    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy