pub struct RestClient<'a> {
pub node_url: Vec<&'a str>,
pub request_time_out: u64,
pub poll_attemps: u64,
pub poll_attemp_interval_time: u64,
}Expand description
A REST client for interacting with Postchain blockchain nodes.
This client handles communication with blockchain nodes, including:
- Transaction submission and status checking
- Node discovery and management
- Query execution
- Error handling
Fields§
§node_url: Vec<&'a str>List of node URLs to connect to
request_time_out: u64Request timeout in seconds
poll_attemps: u64Number of attempts to poll for transaction status
poll_attemp_interval_time: u64Interval between poll attempts in seconds
Implementations§
Source§impl<'a> RestClient<'a>
impl<'a> RestClient<'a>
Sourcepub async fn get_nodes_from_directory(
&self,
brid: &str,
) -> Result<Vec<String>, RestError>
pub async fn get_nodes_from_directory( &self, brid: &str, ) -> Result<Vec<String>, RestError>
Retrieves a list of node URLs from the blockchain directory.
§Arguments
brid- Blockchain RID (Resource Identifier)
§Returns
Result<Vec<String>, RestError>- List of node URLs on success, or error on failure
§Example
let client = RestClient::default();
let nodes = client.get_nodes_from_directory("blockchain_rid").await?;Sourcepub fn print_error(&self, error: &RestError, ignore_all_errors: bool) -> bool
pub fn print_error(&self, error: &RestError, ignore_all_errors: bool) -> bool
Sourcepub async fn detect_merkle_hash_version(&self, brid: &str) -> u8
pub async fn detect_merkle_hash_version(&self, brid: &str) -> u8
Detects the Merkle hash version used by a blockchain.
This function queries the blockchain’s configuration to determine which version of the Merkle hash algorithm is being used. If the query fails or the version information is not available, it defaults to version 1.
§Arguments
brid- The blockchain RID (Resource Identifier) as a hex-encoded string
§Returns
u8- The Merkle hash version number (defaults to 1 if not specified)
§Example
let client = RestClient::default();
let brid = "DCE5D72ED7E1675291AFE7F9D649D898C8D3E7411E52882D03D1B3D240BDD91B";
let hash_version = client.detect_merkle_hash_version(brid).await;
println!("Blockchain uses Merkle hash version {}", hash_version);Sourcepub fn update_node_urls(&mut self, node_urls: &'a [String])
pub fn update_node_urls(&mut self, node_urls: &'a [String])
Updates the list of node URLs used by the client.
§Arguments
node_urls- New list of node URLs to use
Sourcepub async fn get_transaction_status(
&self,
blockchain_rid: &str,
tx_rid: &str,
) -> Result<TransactionStatus, RestError>
pub async fn get_transaction_status( &self, blockchain_rid: &str, tx_rid: &str, ) -> Result<TransactionStatus, RestError>
Sourcepub async fn get_confirmation_proof(
&self,
blockchain_rid: &str,
tx_rid: &str,
) -> Result<TransactionConfirmationProofData, RestError>
pub async fn get_confirmation_proof( &self, blockchain_rid: &str, tx_rid: &str, ) -> Result<TransactionConfirmationProofData, RestError>
Retrieves the confirmation proof for a given transaction.
This function makes a GET request to the /tx/{blockchain_rid}/{tx_rid}/confirmationProof
endpoint of the Postchain node to fetch the cryptographic proof that a transaction
has been confirmed on the blockchain.
§Arguments
blockchain_rid- A string slice representing the Blockchain RID (Resource Identifier)tx_rid- A string slice representing the Transaction RID (Resource Identifier)
§Returns
Result<TransactionConfirmationProofData, RestError>- ReturnsOk(TransactionConfirmationProofData)on successful retrieval and parsing of the proof, orErr(RestError)if the request fails, the response is not JSON, or the ‘proof’ field is missing/invalid.
§Errors
This function can return a RestError in the following cases:
- If the underlying
postchain_rest_apicall fails (e.g., network issues, node unreachable). - If the response from the node is not a JSON object.
- If the JSON response does not contain a “proof” field, or if the “proof” field is not a string.
- If the string value of the “proof” field cannot be successfully parsed into a
TransactionConfirmationProofDatastruct.
§Example
let client = RestClient::default();
let blockchain_rid = "your_blockchain_rid_hex_string"; // Replace with actual blockchain RID
let tx_rid = "your_transaction_rid_hex_string"; // Replace with actual transaction RID
match client.get_confirmation_proof(blockchain_rid, tx_rid).await {
Ok(proof_data) => {
println!("Successfully retrieved confirmation proof:");
println!("Block height: {}", proof_data.block_height);
// Further processing of proof_data...
},
Err(e) => {
eprintln!("Failed to get confirmation proof: {}", e);
}
}Sourcepub async fn get_raw_transaction_data(
&self,
blockchain_rid: &str,
tx_rid: &str,
) -> Result<Transaction, RestError>
pub async fn get_raw_transaction_data( &self, blockchain_rid: &str, tx_rid: &str, ) -> Result<Transaction, RestError>
Retrieves the raw transaction data for a given transaction.
This function makes a GET request to the /tx/{blockchain_rid}/{tx_rid} endpoint
of the Postchain node to fetch the raw hexadecimal representation of a transaction.
§Arguments
blockchain_rid- A string slice representing the Blockchain RID.tx_rid- A string slice representing the Transaction RID.
§Returns
Result<Transaction, RestError>- ReturnsOk(Transaction)on successful retrieval and parsing of the raw transaction data, orErr(RestError)if the request fails, the response is not JSON, or the ‘tx’ field is missing/invalid.
§Errors
This function can return a RestError in the following cases:
- If the underlying
postchain_rest_apicall fails (e.g., network issues, node unreachable). - If the response from the node is not a JSON object.
- If the JSON response does not contain a “tx” field, or if the “tx” field is not a string.
- If the string value of the “tx” field cannot be successfully parsed into a
Transactionstruct byTransaction::from_raw_data.
§Example
let client = RestClient::default();
let blockchain_rid = "your_blockchain_rid_hex_string"; // Replace with actual blockchain RID
let tx_rid = "your_transaction_rid_hex_string"; // Replace with actual transaction RID
match client.get_raw_transaction_data(blockchain_rid, tx_rid).await {
Ok(transaction) => {
println!("Successfully retrieved raw transaction data: {:?}", transaction);
// Further processing of transaction object...
},
Err(e) => {
eprintln!("Failed to get raw transaction data: {}", e);
}
}Sourcepub async fn get_transaction_status_with_poll(
&self,
blockchain_rid: &str,
tx_rid: &str,
attempts: u64,
) -> Result<TransactionStatus, RestError>
pub async fn get_transaction_status_with_poll( &self, blockchain_rid: &str, tx_rid: &str, attempts: u64, ) -> Result<TransactionStatus, RestError>
Sourcepub async fn send_transaction(
&self,
tx: &Transaction,
) -> Result<RestResponse, RestError>
pub async fn send_transaction( &self, tx: &Transaction, ) -> Result<RestResponse, RestError>
Sourcepub async fn query<T: AsRef<str>>(
&self,
brid: &str,
query_prefix: Option<&str>,
query_type: &'a str,
query_params: Option<&'a mut Vec<(&'a str, &'a str)>>,
query_args: Option<&'a mut Vec<(T, Params)>>,
) -> Result<RestResponse, RestError>
pub async fn query<T: AsRef<str>>( &self, brid: &str, query_prefix: Option<&str>, query_type: &'a str, query_params: Option<&'a mut Vec<(&'a str, &'a str)>>, query_args: Option<&'a mut Vec<(T, Params)>>, ) -> Result<RestResponse, RestError>
Executes a query on the blockchain.
§Arguments
brid- Blockchain RIDquery_prefix- Optional prefix for the query endpointquery_type- Type of query to executequery_params- Optional query parametersquery_args- Optional query arguments
§Returns
Result<RestResponse, RestError>- Query response or error