aptos-client-icp 0.0.1

The Aptos Node API is a RESTful API for client applications to interact with the Aptos blockchain.
Documentation
/*
 * Aptos Node API
 *
 * The Aptos Node API is a RESTful API for client applications to interact with the Aptos blockchain.
 *
 * The version of the OpenAPI document: 1.2.0
 *
 * Generated by: https://openapi-generator.tech
 */

use super::{configuration, Error};
use crate::{apis::ResponseContent, models};
use serde::{Deserialize, Serialize};

/// struct for typed errors of method [`get_ledger_info`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetLedgerInfoError {
    Status400(models::AptosError),
    Status403(models::AptosError),
    Status500(models::AptosError),
    Status503(models::AptosError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`healthy`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum HealthyError {
    Status503(models::AptosError),
    Status500(models::AptosError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`info`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum InfoError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`spec`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SpecError {
    UnknownValue(serde_json::Value),
}

/// Get the latest ledger information, including data such as chain ID, role type, ledger versions, epoch, etc.
pub async fn get_ledger_info(
    configuration: &configuration::Configuration,
) -> Result<models::IndexResponse, Error<GetLedgerInfoError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/", local_var_configuration.base_path);
    let mut local_var_req_builder =
        local_var_client.request(crate::http::HttpMethod::GET, local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder =
            local_var_req_builder.header("User-Agent".to_string(), local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<GetLedgerInfoError> =
            serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent {
            status: local_var_status,
            content: local_var_content,
            entity: local_var_entity,
        };
        Err(Error::ResponseError(local_var_error))
    }
}

/// By default this endpoint just checks that it can get the latest ledger info and then returns 200.  If the duration_secs param is provided, this endpoint will return a 200 if the following condition is true:  `server_latest_ledger_info_timestamp >= server_current_time_timestamp - duration_secs`
pub async fn healthy(
    configuration: &configuration::Configuration,
    duration_secs: Option<i32>,
) -> Result<models::HealthCheckSuccess, Error<HealthyError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/-/healthy", local_var_configuration.base_path);
    let mut local_var_req_builder =
        local_var_client.request(crate::http::HttpMethod::GET, local_var_uri_str.as_str());

    if let Some(ref local_var_str) = duration_secs {
        local_var_req_builder =
            local_var_req_builder.query(&[("duration_secs", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder =
            local_var_req_builder.header("User-Agent".to_string(), local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<HealthyError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent {
            status: local_var_status,
            content: local_var_content,
            entity: local_var_entity,
        };
        Err(Error::ResponseError(local_var_error))
    }
}

pub async fn info(
    configuration: &configuration::Configuration,
) -> Result<std::collections::HashMap<String, serde_json::Value>, Error<InfoError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/info", local_var_configuration.base_path);
    let mut local_var_req_builder =
        local_var_client.request(crate::http::HttpMethod::GET, local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder =
            local_var_req_builder.header("User-Agent".to_string(), local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<InfoError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent {
            status: local_var_status,
            content: local_var_content,
            entity: local_var_entity,
        };
        Err(Error::ResponseError(local_var_error))
    }
}

/// Provides a UI that you can use to explore the API. You can also retrieve the API directly at `/spec.yaml` and `/spec.json`.
pub async fn spec(
    configuration: &configuration::Configuration,
) -> Result<String, Error<SpecError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/spec", local_var_configuration.base_path);
    let mut local_var_req_builder =
        local_var_client.request(crate::http::HttpMethod::GET, local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder =
            local_var_req_builder.header("User-Agent".to_string(), local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<SpecError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent {
            status: local_var_status,
            content: local_var_content,
            entity: local_var_entity,
        };
        Err(Error::ResponseError(local_var_error))
    }
}