satstream_rust_sdk/apis/
blocks_api.rs

1/* 
2 * Satstream API
3 *
4 * Satstream API
5 *
6 * OpenAPI spec version: 1.0
7 * Contact: team@satstream.io
8 * Generated by: https://github.com/swagger-api/swagger-codegen.git
9 */
10
11use std::rc::Rc;
12use std::borrow::Borrow;
13use std::borrow::Cow;
14use std::collections::HashMap;
15
16use hyper;
17use serde_json;
18use futures;
19use futures::{Future, Stream};
20
21use hyper::header::UserAgent;
22
23use super::{Error, configuration};
24
25pub struct BlocksApiClient<C: hyper::client::Connect> {
26    configuration: Rc<configuration::Configuration<C>>,
27}
28
29impl<C: hyper::client::Connect> BlocksApiClient<C> {
30    pub fn new(configuration: Rc<configuration::Configuration<C>>) -> BlocksApiClient<C> {
31        BlocksApiClient {
32            configuration: configuration,
33        }
34    }
35}
36
37pub trait BlocksApi {
38    fn get_block_by_hash(&self, hash: &str) -> Box<Future<Item = ::models::ResponsesGetBlockByHash, Error = Error<serde_json::Value>>>;
39    fn get_block_info(&self, height: i32) -> Box<Future<Item = ::models::ResponsesGetBlockInfo, Error = Error<serde_json::Value>>>;
40    fn get_block_transactions(&self, height: i32) -> Box<Future<Item = ::models::ResponsesGetBlockTransactions, Error = Error<serde_json::Value>>>;
41    fn get_current_block_height(&self, ) -> Box<Future<Item = ::models::ResponsesGetBlockHeight, Error = Error<serde_json::Value>>>;
42}
43
44
45impl<C: hyper::client::Connect>BlocksApi for BlocksApiClient<C> {
46    fn get_block_by_hash(&self, hash: &str) -> Box<Future<Item = ::models::ResponsesGetBlockByHash, Error = Error<serde_json::Value>>> {
47        let configuration: &configuration::Configuration<C> = self.configuration.borrow();
48
49        let method = hyper::Method::Get;
50
51        let query_string = {
52            let mut query = ::url::form_urlencoded::Serializer::new(String::new());
53            query.finish()
54        };
55        let uri_str = format!("{}/blocks/hash/{hash}?{}", configuration.base_path, query_string, hash=hash);
56
57        // TODO(farcaller): handle error
58        // if let Err(e) = uri {
59        //     return Box::new(futures::future::err(e));
60        // }
61        let mut uri: hyper::Uri = uri_str.parse().unwrap();
62
63        let mut req = hyper::Request::new(method, uri);
64
65        if let Some(ref user_agent) = configuration.user_agent {
66            req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone())));
67        }
68
69
70
71
72        // send request
73        Box::new(
74        configuration.client.request(req)
75            .map_err(|e| Error::from(e))
76            .and_then(|resp| {
77                let status = resp.status();
78                resp.body().concat2()
79                    .and_then(move |body| Ok((status, body)))
80                    .map_err(|e| Error::from(e))
81            })
82            .and_then(|(status, body)| {
83                if status.is_success() {
84                    Ok(body)
85                } else {
86                    Err(Error::from((status, &*body)))
87                }
88            })
89            .and_then(|body| {
90                let parsed: Result<::models::ResponsesGetBlockByHash, _> = serde_json::from_slice(&body);
91                parsed.map_err(|e| Error::from(e))
92            })
93        )
94    }
95
96    fn get_block_info(&self, height: i32) -> Box<Future<Item = ::models::ResponsesGetBlockInfo, Error = Error<serde_json::Value>>> {
97        let configuration: &configuration::Configuration<C> = self.configuration.borrow();
98
99        let method = hyper::Method::Get;
100
101        let query_string = {
102            let mut query = ::url::form_urlencoded::Serializer::new(String::new());
103            query.finish()
104        };
105        let uri_str = format!("{}/blocks/{height}?{}", configuration.base_path, query_string, height=height);
106
107        // TODO(farcaller): handle error
108        // if let Err(e) = uri {
109        //     return Box::new(futures::future::err(e));
110        // }
111        let mut uri: hyper::Uri = uri_str.parse().unwrap();
112
113        let mut req = hyper::Request::new(method, uri);
114
115        if let Some(ref user_agent) = configuration.user_agent {
116            req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone())));
117        }
118
119
120
121
122        // send request
123        Box::new(
124        configuration.client.request(req)
125            .map_err(|e| Error::from(e))
126            .and_then(|resp| {
127                let status = resp.status();
128                resp.body().concat2()
129                    .and_then(move |body| Ok((status, body)))
130                    .map_err(|e| Error::from(e))
131            })
132            .and_then(|(status, body)| {
133                if status.is_success() {
134                    Ok(body)
135                } else {
136                    Err(Error::from((status, &*body)))
137                }
138            })
139            .and_then(|body| {
140                let parsed: Result<::models::ResponsesGetBlockInfo, _> = serde_json::from_slice(&body);
141                parsed.map_err(|e| Error::from(e))
142            })
143        )
144    }
145
146    fn get_block_transactions(&self, height: i32) -> Box<Future<Item = ::models::ResponsesGetBlockTransactions, Error = Error<serde_json::Value>>> {
147        let configuration: &configuration::Configuration<C> = self.configuration.borrow();
148
149        let method = hyper::Method::Get;
150
151        let query_string = {
152            let mut query = ::url::form_urlencoded::Serializer::new(String::new());
153            query.finish()
154        };
155        let uri_str = format!("{}/blocks/{height}/transactions?{}", configuration.base_path, query_string, height=height);
156
157        // TODO(farcaller): handle error
158        // if let Err(e) = uri {
159        //     return Box::new(futures::future::err(e));
160        // }
161        let mut uri: hyper::Uri = uri_str.parse().unwrap();
162
163        let mut req = hyper::Request::new(method, uri);
164
165        if let Some(ref user_agent) = configuration.user_agent {
166            req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone())));
167        }
168
169
170
171
172        // send request
173        Box::new(
174        configuration.client.request(req)
175            .map_err(|e| Error::from(e))
176            .and_then(|resp| {
177                let status = resp.status();
178                resp.body().concat2()
179                    .and_then(move |body| Ok((status, body)))
180                    .map_err(|e| Error::from(e))
181            })
182            .and_then(|(status, body)| {
183                if status.is_success() {
184                    Ok(body)
185                } else {
186                    Err(Error::from((status, &*body)))
187                }
188            })
189            .and_then(|body| {
190                let parsed: Result<::models::ResponsesGetBlockTransactions, _> = serde_json::from_slice(&body);
191                parsed.map_err(|e| Error::from(e))
192            })
193        )
194    }
195
196    fn get_current_block_height(&self, ) -> Box<Future<Item = ::models::ResponsesGetBlockHeight, Error = Error<serde_json::Value>>> {
197        let configuration: &configuration::Configuration<C> = self.configuration.borrow();
198
199        let method = hyper::Method::Get;
200
201        let query_string = {
202            let mut query = ::url::form_urlencoded::Serializer::new(String::new());
203            query.finish()
204        };
205        let uri_str = format!("{}/blocks/current-height?{}", configuration.base_path, query_string);
206
207        // TODO(farcaller): handle error
208        // if let Err(e) = uri {
209        //     return Box::new(futures::future::err(e));
210        // }
211        let mut uri: hyper::Uri = uri_str.parse().unwrap();
212
213        let mut req = hyper::Request::new(method, uri);
214
215        if let Some(ref user_agent) = configuration.user_agent {
216            req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone())));
217        }
218
219
220
221
222        // send request
223        Box::new(
224        configuration.client.request(req)
225            .map_err(|e| Error::from(e))
226            .and_then(|resp| {
227                let status = resp.status();
228                resp.body().concat2()
229                    .and_then(move |body| Ok((status, body)))
230                    .map_err(|e| Error::from(e))
231            })
232            .and_then(|(status, body)| {
233                if status.is_success() {
234                    Ok(body)
235                } else {
236                    Err(Error::from((status, &*body)))
237                }
238            })
239            .and_then(|body| {
240                let parsed: Result<::models::ResponsesGetBlockHeight, _> = serde_json::from_slice(&body);
241                parsed.map_err(|e| Error::from(e))
242            })
243        )
244    }
245
246}