satstream_rust_sdk/apis/
transactions_api.rs1use 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 TransactionsApiClient<C: hyper::client::Connect> {
26 configuration: Rc<configuration::Configuration<C>>,
27}
28
29impl<C: hyper::client::Connect> TransactionsApiClient<C> {
30 pub fn new(configuration: Rc<configuration::Configuration<C>>) -> TransactionsApiClient<C> {
31 TransactionsApiClient {
32 configuration: configuration,
33 }
34 }
35}
36
37pub trait TransactionsApi {
38 fn broadcast_transaction(&self, transaction: &str) -> Box<Future<Item = ::models::ResponsesSendRawTransaction, Error = Error<serde_json::Value>>>;
39 fn get_transaction(&self, hash: &str) -> Box<Future<Item = ::models::ResponsesGetTransaction, Error = Error<serde_json::Value>>>;
40 fn get_transaction_info(&self, txid: &str) -> Box<Future<Item = ::models::ResponsesGetTxInfo, Error = Error<serde_json::Value>>>;
41}
42
43
44impl<C: hyper::client::Connect>TransactionsApi for TransactionsApiClient<C> {
45 fn broadcast_transaction(&self, transaction: &str) -> Box<Future<Item = ::models::ResponsesSendRawTransaction, Error = Error<serde_json::Value>>> {
46 let configuration: &configuration::Configuration<C> = self.configuration.borrow();
47
48 let mut auth_headers = HashMap::<String, String>::new();
49 let mut auth_query = HashMap::<String, String>::new();
50 if let Some(ref apikey) = configuration.api_key {
51 let key = apikey.key.clone();
52 let val = match apikey.prefix {
53 Some(ref prefix) => format!("{} {}", prefix, key),
54 None => key,
55 };
56 auth_headers.insert("X-API-KEY".to_owned(), val);
57 };
58 let method = hyper::Method::Post;
59
60 let query_string = {
61 let mut query = ::url::form_urlencoded::Serializer::new(String::new());
62 for (key, val) in &auth_query {
63 query.append_pair(key, val);
64 }
65 query.finish()
66 };
67 let uri_str = format!("{}/transactions/broadcast?{}", configuration.base_path, query_string);
68
69 let mut uri: hyper::Uri = uri_str.parse().unwrap();
74
75 let mut req = hyper::Request::new(method, uri);
76
77 if let Some(ref user_agent) = configuration.user_agent {
78 req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone())));
79 }
80
81
82 for (key, val) in auth_headers {
83 req.headers_mut().set_raw(key, val);
84 }
85
86 let serialized = serde_json::to_string(&transaction).unwrap();
87 req.headers_mut().set(hyper::header::ContentType::json());
88 req.headers_mut().set(hyper::header::ContentLength(serialized.len() as u64));
89 req.set_body(serialized);
90
91 Box::new(
93 configuration.client.request(req)
94 .map_err(|e| Error::from(e))
95 .and_then(|resp| {
96 let status = resp.status();
97 resp.body().concat2()
98 .and_then(move |body| Ok((status, body)))
99 .map_err(|e| Error::from(e))
100 })
101 .and_then(|(status, body)| {
102 if status.is_success() {
103 Ok(body)
104 } else {
105 Err(Error::from((status, &*body)))
106 }
107 })
108 .and_then(|body| {
109 let parsed: Result<::models::ResponsesSendRawTransaction, _> = serde_json::from_slice(&body);
110 parsed.map_err(|e| Error::from(e))
111 })
112 )
113 }
114
115 fn get_transaction(&self, hash: &str) -> Box<Future<Item = ::models::ResponsesGetTransaction, Error = Error<serde_json::Value>>> {
116 let configuration: &configuration::Configuration<C> = self.configuration.borrow();
117
118 let method = hyper::Method::Get;
119
120 let query_string = {
121 let mut query = ::url::form_urlencoded::Serializer::new(String::new());
122 query.finish()
123 };
124 let uri_str = format!("{}/indexer/tx/{hash}?{}", configuration.base_path, query_string, hash=hash);
125
126 let mut uri: hyper::Uri = uri_str.parse().unwrap();
131
132 let mut req = hyper::Request::new(method, uri);
133
134 if let Some(ref user_agent) = configuration.user_agent {
135 req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone())));
136 }
137
138
139
140
141 Box::new(
143 configuration.client.request(req)
144 .map_err(|e| Error::from(e))
145 .and_then(|resp| {
146 let status = resp.status();
147 resp.body().concat2()
148 .and_then(move |body| Ok((status, body)))
149 .map_err(|e| Error::from(e))
150 })
151 .and_then(|(status, body)| {
152 if status.is_success() {
153 Ok(body)
154 } else {
155 Err(Error::from((status, &*body)))
156 }
157 })
158 .and_then(|body| {
159 let parsed: Result<::models::ResponsesGetTransaction, _> = serde_json::from_slice(&body);
160 parsed.map_err(|e| Error::from(e))
161 })
162 )
163 }
164
165 fn get_transaction_info(&self, txid: &str) -> Box<Future<Item = ::models::ResponsesGetTxInfo, Error = Error<serde_json::Value>>> {
166 let configuration: &configuration::Configuration<C> = self.configuration.borrow();
167
168 let mut auth_headers = HashMap::<String, String>::new();
169 let mut auth_query = HashMap::<String, String>::new();
170 if let Some(ref apikey) = configuration.api_key {
171 let key = apikey.key.clone();
172 let val = match apikey.prefix {
173 Some(ref prefix) => format!("{} {}", prefix, key),
174 None => key,
175 };
176 auth_headers.insert("X-API-KEY".to_owned(), val);
177 };
178 let method = hyper::Method::Get;
179
180 let query_string = {
181 let mut query = ::url::form_urlencoded::Serializer::new(String::new());
182 for (key, val) in &auth_query {
183 query.append_pair(key, val);
184 }
185 query.finish()
186 };
187 let uri_str = format!("{}/transactions/{txid}?{}", configuration.base_path, query_string, txid=txid);
188
189 let mut uri: hyper::Uri = uri_str.parse().unwrap();
194
195 let mut req = hyper::Request::new(method, uri);
196
197 if let Some(ref user_agent) = configuration.user_agent {
198 req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone())));
199 }
200
201
202 for (key, val) in auth_headers {
203 req.headers_mut().set_raw(key, val);
204 }
205
206
207 Box::new(
209 configuration.client.request(req)
210 .map_err(|e| Error::from(e))
211 .and_then(|resp| {
212 let status = resp.status();
213 resp.body().concat2()
214 .and_then(move |body| Ok((status, body)))
215 .map_err(|e| Error::from(e))
216 })
217 .and_then(|(status, body)| {
218 if status.is_success() {
219 Ok(body)
220 } else {
221 Err(Error::from((status, &*body)))
222 }
223 })
224 .and_then(|body| {
225 let parsed: Result<::models::ResponsesGetTxInfo, _> = serde_json::from_slice(&body);
226 parsed.map_err(|e| Error::from(e))
227 })
228 )
229 }
230
231}