stellar_rs/transactions/
single_transaction_request.rs

1use crate::models::*;
2
3/// Represents the transaction hash.
4#[derive(Default, Clone)]
5pub struct TransactionHash(String);
6
7/// Represents the absence of a transaction hash.
8#[derive(Default, Clone)]
9pub struct NoTransactionHash;
10
11#[derive(Default)]
12pub struct SingleTransactionRequest<T> {
13    /// Transaction hash must be a hex-encoded, lowercase SHA-256, 64 char string.
14    transaction_hash: T,
15}
16
17impl SingleTransactionRequest<NoTransactionHash> {
18    /// Creates a new `SingleTransactionRequest` with default parameters.
19    pub fn new() -> Self {
20        SingleTransactionRequest::default()
21    }
22
23    /// Sets the transaction hash for the request.
24    ///
25    /// # Arguments
26    /// * `transaction_hash` - A `String` specifying the operation hash.
27    ///
28    pub fn set_transaction_hash(
29        self,
30        transaction_hash: impl Into<String>,
31    ) -> Result<SingleTransactionRequest<TransactionHash>, String> {
32        let transaction_hash = transaction_hash.into();
33        match transaction_hash.len() {
34            64 => Ok(SingleTransactionRequest {
35                transaction_hash: TransactionHash(transaction_hash),
36            }),
37            _ => Err("Transaction hash must be 64 characters long".to_string()),
38        }
39    }
40}
41
42impl Request for SingleTransactionRequest<TransactionHash> {
43    fn get_query_parameters(&self) -> String {
44        let mut query = String::new();
45        query.push_str(&format!("{}", self.transaction_hash.0));
46
47        query.trim_end_matches('&').to_string()
48    }
49
50    fn build_url(&self, base_url: &str) -> String {
51        // This URL is not built with query parameters, but with the transaction hash as addition to the path.
52        // Therefore there is no `?` but a `/` in the formatted string.
53        format!(
54            "{}/{}/{}",
55            base_url,
56            super::TRANSACTIONS_PATH,
57            self.get_query_parameters()
58        )
59    }
60}