arkecosystem_client/api/
transactions.rs

1use api::models::{Transaction, TransactionFees, TransactionTypes};
2use api::Result;
3use http::client::Client;
4use std::borrow::Borrow;
5use std::collections::HashMap;
6
7pub struct Transactions {
8    client: Client,
9}
10
11impl Transactions {
12    pub fn new(client: Client) -> Transactions {
13        Transactions { client }
14    }
15
16    pub fn all(&self) -> Result<Vec<Transaction>> {
17        self.all_params(Vec::<(String, String)>::new())
18    }
19
20    pub fn all_params<I, K, V>(&self, parameters: I) -> Result<Vec<Transaction>>
21    where
22        I: IntoIterator,
23        I::Item: Borrow<(K, V)>,
24        K: AsRef<str>,
25        V: AsRef<str>,
26    {
27        self.client.get_with_params("transactions", parameters)
28    }
29
30    pub fn create(&self, transactions: Vec<&str>) -> Result<Transaction> {
31        let mut payload = HashMap::<&str, Vec<&str>>::new();
32        payload.insert("transactions", transactions);
33
34        self.client.post("transactions", Some(payload))
35    }
36
37    pub fn show(&self, id: &str) -> Result<Transaction> {
38        let endpoint = format!("transactions/{}", id);
39        self.client.get(&endpoint)
40    }
41
42    pub fn all_unconfirmed(&self) -> Result<Vec<Transaction>> {
43        self.all_unconfirmed_params(Vec::<(String, String)>::new())
44    }
45
46    pub fn all_unconfirmed_params<I, K, V>(&self, parameters: I) -> Result<Vec<Transaction>>
47    where
48        I: IntoIterator,
49        I::Item: Borrow<(K, V)>,
50        K: AsRef<str>,
51        V: AsRef<str>,
52    {
53        self.client
54            .get_with_params("transactions/unconfirmed", parameters)
55    }
56
57    pub fn show_unconfirmed(&self, id: &str) -> Result<Vec<Transaction>> {
58        let endpoint = format!("transactions/unconfirmed/{}", id);
59        self.client.get(&endpoint)
60    }
61
62    pub fn search<I, K, V>(
63        &self,
64        payload: Option<HashMap<&str, &str>>,
65        parameters: I,
66    ) -> Result<Vec<Transaction>>
67    where
68        I: IntoIterator,
69        I::Item: Borrow<(K, V)>,
70        K: AsRef<str>,
71        V: AsRef<str>,
72    {
73        self.client
74            .post_with_params("transactions/search", payload, parameters)
75    }
76
77    /// Returns the transaction types and their ID
78    ///
79    /// # Example
80    /// ```
81    /// # extern crate serde_json;
82    /// # extern crate arkecosystem_client;
83    ///
84    /// # use serde_json::to_string_pretty;
85    /// # use arkecosystem_client::connection::Connection;
86    ///
87    /// # fn main() {
88    ///   # let client = Connection::new("http://95.179.170.23:4003/api/");
89    ///   let types = client.transactions.types().unwrap();
90    ///   println!("{}", to_string_pretty(&types).unwrap());
91    /// # }
92    /// ```
93    pub fn types(&self) -> Result<TransactionTypes> {
94        self.client.get("transactions/types")
95    }
96
97    /// Returns the static fees of the last block processed by the node
98    ///
99    /// # Example
100    /// ```
101    /// # extern crate serde_json;
102    /// # extern crate arkecosystem_client;
103    ///
104    /// # use serde_json::to_string_pretty;
105    /// # use arkecosystem_client::connection::Connection;
106    ///
107    /// # fn main() {
108    ///   # let client = Connection::new("http://167.114.43.38:4003/api/");
109    ///   let fees = client.transactions.fees().unwrap();
110    ///   println!("{}", to_string_pretty(&fees).unwrap());
111    /// # }
112    /// ```
113    pub fn fees(&self) -> Result<TransactionFees> {
114        self.client.get("transactions/fees")
115    }
116}