Skip to main content

aptos_client_icp/apis/
blocks_api.rs

1/*
2 * Aptos Node API
3 *
4 * The Aptos Node API is a RESTful API for client applications to interact with the Aptos blockchain.
5 *
6 * The version of the OpenAPI document: 1.2.0
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use super::{configuration, Error};
12use crate::{apis::ResponseContent, models};
13use serde::{Deserialize, Serialize};
14
15/// struct for typed errors of method [`get_block_by_height`]
16#[derive(Debug, Clone, Serialize, Deserialize)]
17#[serde(untagged)]
18pub enum GetBlockByHeightError {
19    Status400(models::AptosError),
20    Status403(models::AptosError),
21    Status404(models::AptosError),
22    Status410(models::AptosError),
23    Status500(models::AptosError),
24    Status503(models::AptosError),
25    UnknownValue(serde_json::Value),
26}
27
28/// struct for typed errors of method [`get_block_by_version`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum GetBlockByVersionError {
32    Status400(models::AptosError),
33    Status403(models::AptosError),
34    Status404(models::AptosError),
35    Status410(models::AptosError),
36    Status500(models::AptosError),
37    Status503(models::AptosError),
38    UnknownValue(serde_json::Value),
39}
40
41/// This endpoint allows you to get the transactions in a block and the corresponding block information.  Transactions are limited by max default transactions size.  If not all transactions are present, the user will need to query for the rest of the transactions via the get transactions API.  If the block is pruned, it will return a 410
42pub async fn get_block_by_height(
43    configuration: &configuration::Configuration,
44    block_height: i32,
45    with_transactions: Option<bool>,
46) -> Result<models::Block, Error<GetBlockByHeightError>> {
47    let local_var_configuration = configuration;
48
49    let local_var_client = &local_var_configuration.client;
50
51    let local_var_uri_str = format!(
52        "{}/blocks/by_height/{block_height}",
53        local_var_configuration.base_path,
54        block_height = block_height
55    );
56    let mut local_var_req_builder =
57        local_var_client.request(crate::http::HttpMethod::GET, local_var_uri_str.as_str());
58
59    if let Some(ref local_var_str) = with_transactions {
60        local_var_req_builder =
61            local_var_req_builder.query(&[("with_transactions", &local_var_str.to_string())]);
62    }
63    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
64        local_var_req_builder =
65            local_var_req_builder.header("User-Agent".to_string(), local_var_user_agent.clone());
66    }
67
68    let local_var_req = local_var_req_builder.build()?;
69    let local_var_resp = local_var_client.execute(local_var_req).await?;
70
71    let local_var_status = local_var_resp.status();
72    let local_var_content = local_var_resp.text().await?;
73
74    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
75        serde_json::from_str(&local_var_content).map_err(Error::from)
76    } else {
77        let local_var_entity: Option<GetBlockByHeightError> =
78            serde_json::from_str(&local_var_content).ok();
79        let local_var_error = ResponseContent {
80            status: local_var_status,
81            content: local_var_content,
82            entity: local_var_entity,
83        };
84        Err(Error::ResponseError(local_var_error))
85    }
86}
87
88/// This endpoint allows you to get the transactions in a block and the corresponding block information given a version in the block.  Transactions are limited by max default transactions size.  If not all transactions are present, the user will need to query for the rest of the transactions via the get transactions API.  If the block has been pruned, it will return a 410
89pub async fn get_block_by_version(
90    configuration: &configuration::Configuration,
91    version: i32,
92    with_transactions: Option<bool>,
93) -> Result<models::Block, Error<GetBlockByVersionError>> {
94    let local_var_configuration = configuration;
95
96    let local_var_client = &local_var_configuration.client;
97
98    let local_var_uri_str = format!(
99        "{}/blocks/by_version/{version}",
100        local_var_configuration.base_path,
101        version = version
102    );
103    let mut local_var_req_builder =
104        local_var_client.request(crate::http::HttpMethod::GET, local_var_uri_str.as_str());
105
106    if let Some(ref local_var_str) = with_transactions {
107        local_var_req_builder =
108            local_var_req_builder.query(&[("with_transactions", &local_var_str.to_string())]);
109    }
110    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
111        local_var_req_builder =
112            local_var_req_builder.header("User-Agent".to_string(), local_var_user_agent.clone());
113    }
114
115    let local_var_req = local_var_req_builder.build()?;
116    let local_var_resp = local_var_client.execute(local_var_req).await?;
117
118    let local_var_status = local_var_resp.status();
119    let local_var_content = local_var_resp.text().await?;
120
121    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
122        serde_json::from_str(&local_var_content).map_err(Error::from)
123    } else {
124        let local_var_entity: Option<GetBlockByVersionError> =
125            serde_json::from_str(&local_var_content).ok();
126        let local_var_error = ResponseContent {
127            status: local_var_status,
128            content: local_var_content,
129            entity: local_var_entity,
130        };
131        Err(Error::ResponseError(local_var_error))
132    }
133}