esplora_btc_api/apis/
block_api.rs

1/*
2 * Blockstream Esplora HTTP API
3 *
4 * JSON over RESTful HTTP. Amounts are always represented in satoshis.
5 *
6 * The version of the OpenAPI document: 1.0.1
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13
14use crate::apis::ResponseContent;
15use super::{Error, configuration};
16
17
18/// struct for typed errors of method `get_block`
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetBlockError {
22    DefaultResponse(crate::models::InlineResponseDefault2),
23    UnknownValue(serde_json::Value),
24}
25
26/// struct for typed errors of method `get_block_at_height`
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum GetBlockAtHeightError {
30    DefaultResponse(String),
31    UnknownValue(serde_json::Value),
32}
33
34/// struct for typed errors of method `get_block_header`
35#[derive(Debug, Clone, Serialize, Deserialize)]
36#[serde(untagged)]
37pub enum GetBlockHeaderError {
38    DefaultResponse(String),
39    UnknownValue(serde_json::Value),
40}
41
42/// struct for typed errors of method `get_block_raw`
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum GetBlockRawError {
46    DefaultResponse(std::path::PathBuf),
47    UnknownValue(serde_json::Value),
48}
49
50/// struct for typed errors of method `get_block_status`
51#[derive(Debug, Clone, Serialize, Deserialize)]
52#[serde(untagged)]
53pub enum GetBlockStatusError {
54    DefaultResponse(crate::models::InlineResponseDefault3),
55    UnknownValue(serde_json::Value),
56}
57
58/// struct for typed errors of method `get_block_tx_by_index`
59#[derive(Debug, Clone, Serialize, Deserialize)]
60#[serde(untagged)]
61pub enum GetBlockTxByIndexError {
62    DefaultResponse(String),
63    UnknownValue(serde_json::Value),
64}
65
66/// struct for typed errors of method `get_block_txids`
67#[derive(Debug, Clone, Serialize, Deserialize)]
68#[serde(untagged)]
69pub enum GetBlockTxidsError {
70    DefaultResponse(Vec<String>),
71    UnknownValue(serde_json::Value),
72}
73
74/// struct for typed errors of method `get_last_block_hash`
75#[derive(Debug, Clone, Serialize, Deserialize)]
76#[serde(untagged)]
77pub enum GetLastBlockHashError {
78    DefaultResponse(String),
79    UnknownValue(serde_json::Value),
80}
81
82/// struct for typed errors of method `get_last_block_height`
83#[derive(Debug, Clone, Serialize, Deserialize)]
84#[serde(untagged)]
85pub enum GetLastBlockHeightError {
86    DefaultResponse(f32),
87    UnknownValue(serde_json::Value),
88}
89
90
91pub async fn get_block(configuration: &configuration::Configuration, hash: &str) -> Result<crate::models::InlineResponseDefault2, Error<GetBlockError>> {
92
93    let local_var_client = &configuration.client;
94
95    let local_var_uri_str = format!("{}/block/{hash}", configuration.base_path, hash=crate::apis::urlencode(hash));
96    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
97
98    if let Some(ref local_var_user_agent) = configuration.user_agent {
99        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
100    }
101
102    let local_var_req = local_var_req_builder.build()?;
103    let local_var_resp = local_var_client.execute(local_var_req).await?;
104
105    let local_var_status = local_var_resp.status();
106    let local_var_content = local_var_resp.text().await?;
107
108    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
109        serde_json::from_str(&local_var_content).map_err(Error::from)
110    } else {
111        let local_var_entity: Option<GetBlockError> = serde_json::from_str(&local_var_content).ok();
112        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
113        Err(Error::ResponseError(local_var_error))
114    }
115}
116
117pub async fn get_block_at_height(configuration: &configuration::Configuration, height: f32) -> Result<String, Error<GetBlockAtHeightError>> {
118
119    let local_var_client = &configuration.client;
120
121    let local_var_uri_str = format!("{}/block-height/{height}", configuration.base_path, height=height);
122    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
123
124    if let Some(ref local_var_user_agent) = configuration.user_agent {
125        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
126    }
127
128    let local_var_req = local_var_req_builder.build()?;
129    let local_var_resp = local_var_client.execute(local_var_req).await?;
130
131    let local_var_status = local_var_resp.status();
132    let local_var_content = local_var_resp.text().await?;
133
134    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
135        serde_json::from_str(&local_var_content).map_err(Error::from)
136    } else {
137        let local_var_entity: Option<GetBlockAtHeightError> = serde_json::from_str(&local_var_content).ok();
138        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
139        Err(Error::ResponseError(local_var_error))
140    }
141}
142
143pub async fn get_block_header(configuration: &configuration::Configuration, hash: &str) -> Result<String, Error<GetBlockHeaderError>> {
144
145    let local_var_client = &configuration.client;
146
147    let local_var_uri_str = format!("{}/block/{hash}/header", configuration.base_path, hash=crate::apis::urlencode(hash));
148    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
149
150    if let Some(ref local_var_user_agent) = configuration.user_agent {
151        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
152    }
153
154    let local_var_req = local_var_req_builder.build()?;
155    let local_var_resp = local_var_client.execute(local_var_req).await?;
156
157    let local_var_status = local_var_resp.status();
158    let local_var_content = local_var_resp.text().await?;
159
160    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
161        serde_json::from_str(&local_var_content).map_err(Error::from)
162    } else {
163        let local_var_entity: Option<GetBlockHeaderError> = serde_json::from_str(&local_var_content).ok();
164        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
165        Err(Error::ResponseError(local_var_error))
166    }
167}
168
169pub async fn get_block_raw(configuration: &configuration::Configuration, hash: &str) -> Result<std::path::PathBuf, Error<GetBlockRawError>> {
170
171    let local_var_client = &configuration.client;
172
173    let local_var_uri_str = format!("{}/block/{hash}/raw", configuration.base_path, hash=crate::apis::urlencode(hash));
174    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
175
176    if let Some(ref local_var_user_agent) = configuration.user_agent {
177        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
178    }
179
180    let local_var_req = local_var_req_builder.build()?;
181    let local_var_resp = local_var_client.execute(local_var_req).await?;
182
183    let local_var_status = local_var_resp.status();
184    let local_var_content = local_var_resp.text().await?;
185
186    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
187        serde_json::from_str(&local_var_content).map_err(Error::from)
188    } else {
189        let local_var_entity: Option<GetBlockRawError> = serde_json::from_str(&local_var_content).ok();
190        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
191        Err(Error::ResponseError(local_var_error))
192    }
193}
194
195pub async fn get_block_status(configuration: &configuration::Configuration, hash: &str) -> Result<crate::models::InlineResponseDefault3, Error<GetBlockStatusError>> {
196
197    let local_var_client = &configuration.client;
198
199    let local_var_uri_str = format!("{}/block/{hash}/status", configuration.base_path, hash=crate::apis::urlencode(hash));
200    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
201
202    if let Some(ref local_var_user_agent) = configuration.user_agent {
203        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
204    }
205
206    let local_var_req = local_var_req_builder.build()?;
207    let local_var_resp = local_var_client.execute(local_var_req).await?;
208
209    let local_var_status = local_var_resp.status();
210    let local_var_content = local_var_resp.text().await?;
211
212    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
213        serde_json::from_str(&local_var_content).map_err(Error::from)
214    } else {
215        let local_var_entity: Option<GetBlockStatusError> = serde_json::from_str(&local_var_content).ok();
216        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
217        Err(Error::ResponseError(local_var_error))
218    }
219}
220
221pub async fn get_block_tx_by_index(configuration: &configuration::Configuration, hash: &str, index: f32) -> Result<String, Error<GetBlockTxByIndexError>> {
222
223    let local_var_client = &configuration.client;
224
225    let local_var_uri_str = format!("{}/block/{hash}/txid/{index}", configuration.base_path, hash=crate::apis::urlencode(hash), index=index);
226    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
227
228    if let Some(ref local_var_user_agent) = configuration.user_agent {
229        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
230    }
231
232    let local_var_req = local_var_req_builder.build()?;
233    let local_var_resp = local_var_client.execute(local_var_req).await?;
234
235    let local_var_status = local_var_resp.status();
236    let local_var_content = local_var_resp.text().await?;
237
238    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
239        serde_json::from_str(&local_var_content).map_err(Error::from)
240    } else {
241        let local_var_entity: Option<GetBlockTxByIndexError> = serde_json::from_str(&local_var_content).ok();
242        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
243        Err(Error::ResponseError(local_var_error))
244    }
245}
246
247pub async fn get_block_txids(configuration: &configuration::Configuration, hash: &str) -> Result<Vec<String>, Error<GetBlockTxidsError>> {
248
249    let local_var_client = &configuration.client;
250
251    let local_var_uri_str = format!("{}/block/{hash}/txids", configuration.base_path, hash=crate::apis::urlencode(hash));
252    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
253
254    if let Some(ref local_var_user_agent) = configuration.user_agent {
255        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
256    }
257
258    let local_var_req = local_var_req_builder.build()?;
259    let local_var_resp = local_var_client.execute(local_var_req).await?;
260
261    let local_var_status = local_var_resp.status();
262    let local_var_content = local_var_resp.text().await?;
263
264    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
265        serde_json::from_str(&local_var_content).map_err(Error::from)
266    } else {
267        let local_var_entity: Option<GetBlockTxidsError> = serde_json::from_str(&local_var_content).ok();
268        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
269        Err(Error::ResponseError(local_var_error))
270    }
271}
272
273pub async fn get_last_block_hash(configuration: &configuration::Configuration, ) -> Result<String, Error<GetLastBlockHashError>> {
274
275    let local_var_client = &configuration.client;
276
277    let local_var_uri_str = format!("{}/blocks/tip/hash", configuration.base_path);
278    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
279
280    if let Some(ref local_var_user_agent) = configuration.user_agent {
281        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
282    }
283
284    let local_var_req = local_var_req_builder.build()?;
285    let local_var_resp = local_var_client.execute(local_var_req).await?;
286
287    let local_var_status = local_var_resp.status();
288    let local_var_content = local_var_resp.text().await?;
289
290    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
291        serde_json::from_str(&local_var_content).map_err(Error::from)
292    } else {
293        let local_var_entity: Option<GetLastBlockHashError> = serde_json::from_str(&local_var_content).ok();
294        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
295        Err(Error::ResponseError(local_var_error))
296    }
297}
298
299pub async fn get_last_block_height(configuration: &configuration::Configuration, ) -> Result<f32, Error<GetLastBlockHeightError>> {
300
301    let local_var_client = &configuration.client;
302
303    let local_var_uri_str = format!("{}/blocks/tip/height", configuration.base_path);
304    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
305
306    if let Some(ref local_var_user_agent) = configuration.user_agent {
307        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
308    }
309
310    let local_var_req = local_var_req_builder.build()?;
311    let local_var_resp = local_var_client.execute(local_var_req).await?;
312
313    let local_var_status = local_var_resp.status();
314    let local_var_content = local_var_resp.text().await?;
315
316    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
317        serde_json::from_str(&local_var_content).map_err(Error::from)
318    } else {
319        let local_var_entity: Option<GetLastBlockHeightError> = serde_json::from_str(&local_var_content).ok();
320        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
321        Err(Error::ResponseError(local_var_error))
322    }
323}
324