stellar_sdk/endpoints/
operation_call_builder.rs

1use std::collections::HashMap;
2
3use crate::api_call::api_call;
4use crate::endpoints::{horizon::Record, CallBuilder, Server};
5use crate::types::Operation;
6use crate::utils::{Direction, Endpoint};
7
8#[derive(Debug)]
9pub struct OperationCallBuilder<'a> {
10    server_url: &'a str,
11    endpoint: Endpoint,
12    query_params: HashMap<String, String>,
13    token: &'a Option<String>,
14}
15
16impl<'a> OperationCallBuilder<'a> {
17    pub fn new(s: &'a Server) -> Self {
18        Self {
19            server_url: &s.server_url,
20            endpoint: Endpoint::None,
21            query_params: HashMap::new(),
22            token: &s.options.auth_token,
23        }
24    }
25
26    pub fn include_failed(&mut self, i: bool) -> &mut Self {
27        self.query_params
28            .insert(String::from("include_failed"), i.to_string());
29
30        self
31    }
32}
33
34impl<'a> CallBuilder<Operation> for OperationCallBuilder<'a> {
35    fn cursor(&mut self, cursor: &str) -> &mut Self {
36        self.query_params
37            .insert(String::from("cursor"), String::from(cursor));
38
39        self
40    }
41
42    fn order(&mut self, dir: Direction) -> &mut Self {
43        self.query_params
44            .insert(String::from("order"), dir.to_string());
45
46        self
47    }
48
49    fn limit(&mut self, limit: u8) -> &mut Self {
50        self.query_params
51            .insert(String::from("limit"), limit.to_string());
52
53        self
54    }
55
56    fn for_endpoint(&mut self, endpoint: Endpoint) -> &mut Self {
57        self.endpoint = endpoint;
58
59        self
60    }
61
62    fn call(&self) -> Result<Record<Operation>, anyhow::Error> {
63        let url = format!(
64            "{}{}{}",
65            &self.server_url,
66            self.endpoint.as_str(),
67            "/operations",
68        );
69
70        api_call::<Record<Operation>>(
71            url,
72            crate::types::HttpMethod::GET,
73            &self.query_params,
74            self.token,
75        )
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn limit_operation_call_builder() {
85        let s = Server::new(String::from("https://horizon.stellar.org"), None)
86            .expect("Cannot connect to insecure horizon server");
87
88        let mut ocb = OperationCallBuilder::new(&s);
89
90        let op_records = ocb
91            .for_endpoint(Endpoint::Accounts(String::from(
92                "GAUZUPTHOMSZEV65VNSRMUDAAE4VBMSRYYAX3UOWYU3BQUZ6OK65NOWM",
93            )))
94            .limit(200)
95            .call()
96            .unwrap();
97
98        assert_eq!(op_records._embedded.records.len(), 200);
99    }
100}