RestClient

Struct RestClient 

Source
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: u64

Request timeout in seconds

§poll_attemps: u64

Number of attempts to poll for transaction status

§poll_attemp_interval_time: u64

Interval between poll attempts in seconds

Implementations§

Source§

impl<'a> RestClient<'a>

Source

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?;
Source

pub async fn get_blockchain_rid( &self, blockchain_iid: u8, ) -> Result<String, RestError>

Retrieves the blockchain RID for a given blockchain IID.

§Arguments
  • blockchain_iid - Blockchain Instance Identifier
§Returns
  • Result<String, RestError> - Blockchain RID on success, or error on failure
Source

pub fn print_error(&self, error: &RestError, ignore_all_errors: bool) -> bool

Prints error information and determines if the error should be ignored.

§Arguments
  • error - The REST error to print
  • ignore_all_errors - Whether to ignore all errors
§Returns
  • bool - Whether the error should stop execution
Source

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);
Source

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
Source

pub async fn get_transaction_status( &self, blockchain_rid: &str, tx_rid: &str, ) -> Result<TransactionStatus, RestError>

Gets the status of a transaction without polling.

§Arguments
  • blockchain_rid - Blockchain RID
  • tx_rid - Transaction RID
§Returns
  • Result<TransactionStatus, RestError> - Transaction status or error
Source

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> - Returns Ok(TransactionConfirmationProofData) on successful retrieval and parsing of the proof, or Err(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_api call 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 TransactionConfirmationProofData struct.
§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);
    }
}
Source

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> - Returns Ok(Transaction) on successful retrieval and parsing of the raw transaction data, or Err(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_api call 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 Transaction struct by Transaction::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);
    }
}
Source

pub async fn get_transaction_status_with_poll( &self, blockchain_rid: &str, tx_rid: &str, attempts: u64, ) -> Result<TransactionStatus, RestError>

Gets the status of a transaction with polling for confirmation.

§Arguments
  • blockchain_rid - Blockchain RID
  • tx_rid - Transaction RID
  • attempts - Number of polling attempts made so far
§Returns
  • Result<TransactionStatus, RestError> - Transaction status or error
Source

pub async fn send_transaction( &self, tx: &Transaction, ) -> Result<RestResponse, RestError>

Sends a transaction to the blockchain.

§Arguments
  • tx - Transaction to send
§Returns
  • Result<RestResponse, RestError> - Response from the blockchain or error
Source

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 RID
  • query_prefix - Optional prefix for the query endpoint
  • query_type - Type of query to execute
  • query_params - Optional query parameters
  • query_args - Optional query arguments
§Returns
  • Result<RestResponse, RestError> - Query response or error

Trait Implementations§

Source§

impl<'a> Debug for RestClient<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for RestClient<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for RestClient<'a>

§

impl<'a> RefUnwindSafe for RestClient<'a>

§

impl<'a> Send for RestClient<'a>

§

impl<'a> Sync for RestClient<'a>

§

impl<'a> Unpin for RestClient<'a>

§

impl<'a> UnwindSafe for RestClient<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,