use algonaut_client::{indexer::v2::Client, Headers};
use algonaut_core::{Address, Round};
use algonaut_model::indexer::v2::{
AccountInfoResponse, AccountResponse, AccountTransactionResponse, ApplicationInfoResponse,
ApplicationResponse, AssetResponse, AssetTransactionResponse, AssetsInfoResponse,
BalancesResponse, Block, QueryAccount, QueryAccountInfo, QueryAccountTransaction,
QueryApplicationInfo, QueryApplications, QueryAssetTransaction, QueryAssets, QueryAssetsInfo,
QueryBalances, QueryTransaction, TransactionInfoResponse, TransactionResponse,
};
use crate::error::ServiceError;
#[derive(Debug)]
pub struct Indexer {
pub(super) client: Client,
}
impl Indexer {
pub fn new(url: &str) -> Result<Indexer, ServiceError> {
Self::with_headers(url, vec![])
}
pub fn with_headers(url: &str, headers: Headers) -> Result<Indexer, ServiceError> {
Ok(Indexer {
client: Client::new(url, headers)?,
})
}
pub async fn health(&self) -> Result<(), ServiceError> {
Ok(self.client.health().await?)
}
pub async fn accounts(&self, query: &QueryAccount) -> Result<AccountResponse, ServiceError> {
Ok(self.client.accounts(query).await?)
}
pub async fn account_info(
&self,
address: &Address,
query: &QueryAccountInfo,
) -> Result<AccountInfoResponse, ServiceError> {
Ok(self.client.account_info(address, query).await?)
}
pub async fn account_transactions(
&self,
address: &Address,
query: &QueryAccountTransaction,
) -> Result<AccountTransactionResponse, ServiceError> {
Ok(self.client.account_transactions(address, query).await?)
}
pub async fn applications(
&self,
query: &QueryApplications,
) -> Result<ApplicationResponse, ServiceError> {
Ok(self.client.applications(query).await?)
}
pub async fn application_info(
&self,
id: u64,
query: &QueryApplicationInfo,
) -> Result<ApplicationInfoResponse, ServiceError> {
Ok(self.client.application_info(id, query).await?)
}
pub async fn assets(&self, query: &QueryAssets) -> Result<AssetResponse, ServiceError> {
Ok(self.client.assets(query).await?)
}
pub async fn assets_info(
&self,
id: u64,
query: &QueryAssetsInfo,
) -> Result<AssetsInfoResponse, ServiceError> {
Ok(self.client.assets_info(id, query).await?)
}
pub async fn asset_balances(
&self,
id: u64,
query: &QueryBalances,
) -> Result<BalancesResponse, ServiceError> {
Ok(self.client.asset_balances(id, query).await?)
}
pub async fn asset_transactions(
&self,
id: u64,
query: &QueryAssetTransaction,
) -> Result<AssetTransactionResponse, ServiceError> {
Ok(self.client.asset_transactions(id, query).await?)
}
pub async fn block(&self, round: Round) -> Result<Block, ServiceError> {
Ok(self.client.block(round).await?)
}
pub async fn transactions(
&self,
query: &QueryTransaction,
) -> Result<TransactionResponse, ServiceError> {
Ok(self.client.transactions(query).await?)
}
pub async fn transaction_info(
&self,
id: &str,
) -> Result<TransactionInfoResponse, ServiceError> {
Ok(self.client.transaction_info(id).await?)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_with_valid_url() {
let indexer = Indexer::new("http://example.com");
assert!(indexer.ok().is_some());
}
#[test]
#[should_panic(expected = "")]
fn test_create_with_empty_url() {
Indexer::new("").unwrap();
}
}