Skip to content

Commit 964d07c

Browse files
committed
src:api:blocking:wait:intermediate migration
1 parent f8782d6 commit 964d07c

File tree

6 files changed

+149
-118
lines changed

6 files changed

+149
-118
lines changed

src/api.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const API_VERSION: &str = "v1";
2+
/// `pub fn api(option: &str, sub_string: &str) -> String`
3+
pub fn api(option: &str, sub_string: &str) -> String {
4+
use std::process::Command;
5+
6+
//if sub_string == "v1" {
7+
// print!("TODO: support --version v1 api versioning.");
8+
//} else if sub_string == "v2" {
9+
// print!("TODO: support --version v2 api versioning.");
10+
//} else {
11+
let output = if cfg!(target_os = "windows") {
12+
Command::new(format!("mempool-space_{}", option))
13+
.args(["/C", sub_string])
14+
.output()
15+
.expect("failed to execute process")
16+
} else {
17+
Command::new(format!("mempool-space_{}", option))
18+
.arg(sub_string)
19+
//.arg("echo hello")
20+
.output()
21+
.expect("failed to execute process")
22+
};
23+
24+
let result = String::from_utf8(output.stdout)
25+
.map_err(|non_utf8| String::from_utf8_lossy(non_utf8.as_bytes()).into_owned())
26+
.unwrap();
27+
print!("{}", result);
28+
result
29+
//}
30+
}

src/args.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std::path::PathBuf;
55
use std::process;
66

77
use crate::api;
8-
use crate::blocking;
8+
use crate::api::api;
9+
use crate::blocking::blocking;
910

1011
/// GET /api/v1/historical-price?currency=CURRENCY&timestamp=TIMESTAMP
1112
///
@@ -47,11 +48,11 @@ pub fn block_txs(block_hash: &str, start_index: &str) {
4748
let _res = blocking(&format!("block/{}/txs/{}", block_hash, &"0"));
4849
}
4950
}
51+
5052
/// GET /api/v1/blocks[/:startHeight]
5153
/// <https://mempool.space/docs/api/rest#get-blocks>
5254
pub fn blocks(start_height: &str) {
53-
//TODO blocks_tip_height
54-
let blocks_tip_height = api("blocks_tip_height", &"extraneous_arg");
55+
let blocks_tip_height = api::api("blocks_tip_height", &"extraneous_arg");
5556
let blocks_tip_height_int = blocks_tip_height.parse::<i32>().unwrap_or(0);
5657
let start_height_int = start_height.parse::<i32>().unwrap_or(0);
5758
if start_height_int >= 0 && start_height_int <= blocks_tip_height_int {

src/blockheight.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1+
use reqwest::Url;
12
use std::io::Read;
23
use std::time::SystemTime;
34

4-
use reqwest::Url;
5-
6-
/// function example
5+
/// pub fn blockheight() -> Result<f64, ascii::AsciiChar>
6+
///
77
pub fn blockheight() -> Result<f64, ascii::AsciiChar> {
8+
//! inside 1
9+
//!
10+
//! inside 2
11+
//!
12+
//! inside 3
13+
//!
14+
//! [google.com](www.google.com)
15+
//!
16+
//! <https://google.com>
17+
//!
18+
//! - comment
19+
//!
20+
//! - comment
21+
//!
22+
//! since_the_epoch
823
let since_the_epoch = SystemTime::now()
924
.duration_since(SystemTime::UNIX_EPOCH)
1025
.expect("get millis error");
@@ -13,11 +28,16 @@ pub fn blockheight() -> Result<f64, ascii::AsciiChar> {
1328
let _now_millis = seconds * 1000 + subsec_millis;
1429
let url = Url::parse("https://mempool.space/api/blocks/tip/height").unwrap();
1530
let mut res = reqwest::blocking::get(url).unwrap();
16-
1731
let mut tmp_string = String::new();
1832
res.read_to_string(&mut tmp_string).unwrap();
1933
let tmp_u64 = tmp_string.parse::<u64>().unwrap_or(0);
20-
2134
let blockheight = tmp_u64 as f64;
2235
return Ok(blockheight);
2336
}
37+
38+
/// pub fn function_example(){}
39+
///
40+
pub fn function_example() {
41+
42+
//! inside comment
43+
}

src/blocking.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::URL;
2+
/// `pub fn blocking(api: &String) -> Result<&str>`
3+
///
4+
pub fn blocking(api: &String) -> Result<&str, ascii::AsciiChar> {
5+
//print!("api={}", api);
6+
let call = format!("{}/{}", URL, api);
7+
let mut body = ureq::get(&call)
8+
.call()
9+
.expect("calls to blocking(api: &String) needs to include /v1/<api_endpoint> in some cases.")
10+
.into_reader();
11+
let mut buf = Vec::new();
12+
body.read_to_end(&mut buf).unwrap();
13+
if !api.ends_with("raw") {
14+
//print!("!api.ends_with raw");
15+
let text = match std::str::from_utf8(&buf) {
16+
Ok(s) => s,
17+
Err(_) => panic!("Invalid ASCII data"),
18+
};
19+
print!("{}", text);
20+
} else {
21+
if api.ends_with("raw") {
22+
//print!("api.ends_with raw");
23+
print!("{:?}", &buf);
24+
}
25+
if api.ends_with("something_else") {
26+
//print!("api.ends_with something_else");
27+
print!("{:?}", &buf);
28+
}
29+
}
30+
Ok(api)
31+
}

src/lib.rs

Lines changed: 59 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,66 @@
77
// Author: Simon Brummer ([email protected])
88

99
#![warn(missing_docs, clippy::unwrap_used)]
10+
11+
///
12+
/// A CLI tool for [`mempool_space`].
13+
///
14+
/// [`mempool_space`]: https://github.com/randymcmillan/mempool_space
15+
16+
/// TESTING
17+
///
18+
/// TESTING 2
19+
///
20+
/// TESTING 2
21+
///
22+
/// TESTING 2
23+
///
24+
/// TESTING 2
25+
26+
// #[warn(missing_docs, clippy::unwrap_used)]
27+
pub mod api;
28+
pub mod blockheight;
29+
pub mod blocking;
30+
pub mod error;
31+
pub mod resolve_policy;
32+
pub mod target;
33+
34+
pub use error::{CheckTargetError, ParseTargetError, ResolveTargetError};
35+
pub use resolve_policy::ResolvePolicy;
36+
pub use target::{Fqhn, IcmpTarget, Port, Status, Target, TcpTarget};
37+
38+
#[cfg(feature = "async")]
39+
pub use async_target::{AsyncTarget, AsyncTargetExecutor, BoxedHandler, BoxedTarget, OldStatus};
40+
41+
/// Command-line argument parser.
42+
pub mod args;
43+
#[cfg(feature = "async")]
44+
pub mod async_target;
45+
46+
/// Configuration file parser.
47+
pub mod config;
48+
/// Custom error implementation.
49+
pub mod this_error;
50+
/// Upload handler.
51+
pub mod upload;
52+
53+
use crate::args::Args;
54+
use crate::config::Config;
55+
use crate::this_error::{Error, Result};
56+
use crate::upload::Uploader;
57+
// use colored::Colorize;
58+
use crossterm::style::Stylize;
59+
use std::fs;
60+
use std::io::IsTerminal;
1061
use std::io::Read;
11-
const API_VERSION: &str = "v1";
62+
use std::io::{self}; //, Read};
63+
64+
use crate::blocking::blocking;
65+
1266
const URL: &str = "https://mempool.space/api";
1367

68+
// BOOM
69+
//
1470
/// mempool_space - a mempool.space API lib
1571
///
1672
/// Author: @RandyMcMillan ([email protected])
@@ -24,38 +80,8 @@ const URL: &str = "https://mempool.space/api";
2480
/// 1. Flags follow the mempool.space api/rest (replace dashes with underscores)
2581
/// 2. Flags invoke the executable
2682
27-
///
28-
29-
/// `pub fn api(option: &str, sub_string: &str) -> String`
3083
///
31-
pub fn api(option: &str, sub_string: &str) -> String {
32-
use std::process::Command;
3384
34-
//if sub_string == "v1" {
35-
// print!("TODO: support --version v1 api versioning.");
36-
//} else if sub_string == "v2" {
37-
// print!("TODO: support --version v2 api versioning.");
38-
//} else {
39-
let output = if cfg!(target_os = "windows") {
40-
Command::new(format!("mempool-space_{}", option))
41-
.args(["/C", sub_string])
42-
.output()
43-
.expect("failed to execute process")
44-
} else {
45-
Command::new(format!("mempool-space_{}", option))
46-
.arg(sub_string)
47-
//.arg("echo hello")
48-
.output()
49-
.expect("failed to execute process")
50-
};
51-
52-
let result = String::from_utf8(output.stdout)
53-
.map_err(|non_utf8| String::from_utf8_lossy(non_utf8.as_bytes()).into_owned())
54-
.unwrap();
55-
print!("{}", result);
56-
result
57-
//}
58-
}
5985
/// mempool_space - a mempool.space API lib
6086
///
6187
/// Author: @RandyMcMillan ([email protected])
@@ -84,84 +110,6 @@ pub fn api(option: &str, sub_string: &str) -> String {
84110
/// mempool-space_block $(mempool-space_blocks_tip_hash)
85111
///
86112
87-
/// `pub fn blocking(api: &String) -> Result<&str>`
88-
pub fn blocking(api: &String) -> Result<&str> {
89-
//print!("api={}", api);
90-
let call = format!("{}/{}", URL, api);
91-
let mut body = ureq::get(&call)
92-
.call()
93-
.expect("calls to blocking(api: &String) needs to include /v1/<api_endpoint> in some cases.")
94-
.into_reader();
95-
let mut buf = Vec::new();
96-
body.read_to_end(&mut buf).unwrap();
97-
if !api.ends_with("raw") {
98-
//print!("!api.ends_with raw");
99-
let text = match std::str::from_utf8(&buf) {
100-
Ok(s) => s,
101-
Err(_) => panic!("Invalid ASCII data"),
102-
};
103-
print!("{}", text);
104-
} else {
105-
if api.ends_with("raw") {
106-
//print!("api.ends_with raw");
107-
print!("{:?}", &buf);
108-
}
109-
if api.ends_with("something_else") {
110-
//print!("api.ends_with something_else");
111-
print!("{:?}", &buf);
112-
}
113-
}
114-
Ok(api)
115-
}
116-
117-
/// `pub mod blockheight`
118-
pub mod blockheight;
119-
/// `pub mod error`
120-
pub mod error;
121-
/// `pub mod resolve_policy`
122-
pub mod resolve_policy;
123-
/// `pub mod target`
124-
pub mod target;
125-
126-
#[cfg(feature = "async")]
127-
/// `pub mod async_target`
128-
pub mod async_target;
129-
130-
// Re-exports
131-
/// `pub use error`
132-
pub use error::{CheckTargetError, ParseTargetError, ResolveTargetError};
133-
/// `pub use resolve_policy`
134-
pub use resolve_policy::ResolvePolicy;
135-
/// `pub use target`
136-
pub use target::{Fqhn, IcmpTarget, Port, Status, Target, TcpTarget};
137-
138-
#[cfg(feature = "async")]
139-
pub use async_target::{AsyncTarget, AsyncTargetExecutor, BoxedHandler, BoxedTarget, OldStatus};
140-
141-
/// A CLI tool for [`mempool_space`].
142-
///
143-
/// [`mempool_space`]: https://github.com/randymcmillan/mempool_space
144-
145-
/// Command-line argument parser.
146-
pub mod args;
147-
/// Configuration file parser.
148-
pub mod config;
149-
/// Custom error implementation.
150-
pub mod this_error;
151-
/// Upload handler.
152-
pub mod upload;
153-
154-
use crate::args::Args;
155-
use crate::config::Config;
156-
use crate::this_error::{Error, Result};
157-
use crate::upload::Uploader;
158-
// use colored::Colorize;
159-
use std::fs;
160-
use std::io::IsTerminal;
161-
use std::io::{self}; //, Read};
162-
163-
use crossterm::style::Stylize;
164-
165113
/// Default name of the configuration file.
166114
const CONFIG_FILE: &str = "config.toml";
167115

@@ -275,11 +223,12 @@ pub fn wait(sleep: &str) {
275223
// }
276224
}
277225

226+
/// TESTS
278227
#[cfg(test)]
279228
mod tests {
280229
// Note this useful idiom: importing names from outer (for mod tests) scope.
281230
use super::*;
282-
use crate::args::api;
231+
use crate::api::api;
283232

284233
/// General
285234
/// https://mempool.space/docs/api/rest

src/wait.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)