arkecosystem_client/api/
delegates.rs

1use http::client::Client;
2use std::borrow::Borrow;
3use std::collections::HashMap;
4
5use api::models::{Balances, Block, Delegate, Wallet};
6use api::Result;
7
8pub struct Delegates {
9    client: Client,
10}
11
12impl Delegates {
13    pub fn new(client: Client) -> Delegates {
14        Delegates { client }
15    }
16
17    pub fn all(&self) -> Result<Vec<Delegate>> {
18        self.all_params(Vec::<(String, String)>::new())
19    }
20
21    pub fn all_params<I, K, V>(&self, parameters: I) -> Result<Vec<Delegate>>
22    where
23        I: IntoIterator,
24        I::Item: Borrow<(K, V)>,
25        K: AsRef<str>,
26        V: AsRef<str>,
27    {
28        self.client.get_with_params("delegates", parameters)
29    }
30
31    pub fn show(&self, id: &str) -> Result<Delegate> {
32        let endpoint = format!("delegates/{}", id);
33        self.client.get(&endpoint)
34    }
35
36    pub fn blocks(&self, id: &str) -> Result<Vec<Block>> {
37        self.blocks_params(id, Vec::<(String, String)>::new())
38    }
39
40    pub fn blocks_params<I, K, V>(&self, id: &str, parameters: I) -> Result<Vec<Block>>
41    where
42        I: IntoIterator,
43        I::Item: Borrow<(K, V)>,
44        K: AsRef<str>,
45        V: AsRef<str>,
46    {
47        let endpoint = format!("delegates/{}/blocks", id);
48        self.client.get_with_params(&endpoint, parameters)
49    }
50
51    pub fn voters(&self, id: &str) -> Result<Vec<Wallet>> {
52        self.voters_params(id, Vec::<(String, String)>::new())
53    }
54
55    pub fn voters_params<I, K, V>(&self, id: &str, parameters: I) -> Result<Vec<Wallet>>
56    where
57        I: IntoIterator,
58        I::Item: Borrow<(K, V)>,
59        K: AsRef<str>,
60        V: AsRef<str>,
61    {
62        let endpoint = format!("delegates/{}/voters", id);
63        self.client.get_with_params(&endpoint, parameters)
64    }
65
66    /// Returns the voters of a delegate and their balances
67    ///
68    /// # Example
69    /// ```
70    /// # extern crate serde_json;
71    /// # extern crate arkecosystem_client;
72    ///
73    /// # use serde_json::to_string_pretty;
74    /// # use arkecosystem_client::connection::Connection;
75    ///
76    /// # fn main() {
77    ///   # let client = Connection::new("http://167.114.43.38:4003/api/");
78    ///   let delegate_id = "yo";
79    ///   let voters_balances = client.delegates.voters_balances(&delegate_id).unwrap();
80    ///   println!("{}", to_string_pretty(&voters_balances).unwrap());
81    /// # }
82    /// ```
83    pub fn voters_balances(&self, id: &str) -> Result<Balances> {
84        let endpoint = format!("delegates/{}/voters/balances", id);
85        self.client.get(&endpoint)
86    }
87
88    /// Searches the delegates
89    ///
90    /// # Example
91    /// ```
92    /// # extern crate serde_json;
93    /// # extern crate arkecosystem_client;
94    ///
95    /// # use serde_json::to_string_pretty;
96    /// # use arkecosystem_client::connection::Connection;
97    ///
98    /// # fn main() {
99    ///   # let client = Connection::new("http://167.114.43.38:4003/api/");
100    ///   let payload = [("username", "p")].iter();
101    ///   let params = [("limit", "2")].iter();
102    ///   let search = client.delegates.search(Some(payload), params).unwrap();
103    ///   println!("{}", to_string_pretty(&search).unwrap());
104    /// # }
105    /// ```
106    pub fn search<I, K, V>(
107        &self,
108        payload: Option<HashMap<&str, &str>>,
109        parameters: I,
110    ) -> Result<Vec<Delegate>>
111    where
112        I: IntoIterator,
113        I::Item: Borrow<(K, V)>,
114        K: AsRef<str>,
115        V: AsRef<str>,
116    {
117        self.client
118            .post_with_params("delegates/search", payload, parameters)
119    }
120}