Skip to content

Commit 5b989db

Browse files
committed
wip(feat(bitreq)): add bitreq HTTP client blocking support
1 parent 8f3a67e commit 5b989db

File tree

4 files changed

+42
-29
lines changed

4 files changed

+42
-29
lines changed

.github/workflows/cont_integration.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ jobs:
2323
- blocking
2424
- blocking-https
2525
- blocking-https-rustls
26-
- blocking-https-native
27-
- blocking-https-bundled
26+
# - blocking-https-native
27+
# - blocking-https-bundled
2828
- async
2929
- async-https
3030
- async-https-native
@@ -55,7 +55,7 @@ jobs:
5555
if: matrix.rust.version == '1.63.0'
5656
run: |
5757
cargo update -p reqwest --precise "0.12.4"
58-
cargo update -p minreq --precise "2.13.2"
58+
# cargo update -p minreq --precise "2.13.2"
5959
cargo update -p home --precise "0.5.5"
6060
cargo update -p url --precise "2.5.0"
6161
cargo update -p tokio --precise "1.38.1"

Cargo.toml

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,38 @@ path = "src/lib.rs"
1818

1919
[dependencies]
2020
serde = { version = "1.0", features = ["derive"] }
21-
bitcoin = { version = "0.32", features = ["serde", "std"], default-features = false }
21+
bitcoin = { version = "0.32", features = [
22+
"serde",
23+
"std",
24+
], default-features = false }
2225
hex = { version = "0.2", package = "hex-conservative" }
2326
log = "^0.4"
24-
minreq = { version = "2.11.0", features = ["json-using-serde"], optional = true }
25-
reqwest = { version = "0.12", features = ["json"], default-features = false, optional = true }
27+
reqwest = { version = "0.12", features = [
28+
"json",
29+
], default-features = false, optional = true }
30+
bitreq = { git = "https://github.com/tcharding/corepc.git", package = "bitreq", branch = "push-rvqmqwwmqtvm", optional = true }
2631

2732
# default async runtime
2833
tokio = { version = "1", features = ["time"], optional = true }
2934

3035
[dev-dependencies]
3136
serde_json = "1.0"
3237
tokio = { version = "1.20.1", features = ["full"] }
33-
electrsd = { version = "0.33.0", features = ["legacy", "esplora_a33e97e1", "corepc-node_28_0"] }
38+
electrsd = { version = "0.33.0", features = [
39+
"legacy",
40+
"esplora_a33e97e1",
41+
"corepc-node_28_0",
42+
] }
3443
lazy_static = "1.4.0"
3544

3645
[features]
3746
default = ["blocking", "async", "async-https", "tokio"]
38-
blocking = ["minreq", "minreq/proxy"]
39-
blocking-https = ["blocking", "minreq/https"]
40-
blocking-https-rustls = ["blocking", "minreq/https-rustls"]
41-
blocking-https-native = ["blocking", "minreq/https-native"]
42-
blocking-https-bundled = ["blocking", "minreq/https-bundled"]
47+
blocking = ["bitreq", "bitreq/proxy", "bitreq/json-using-serde"]
48+
blocking-https = ["blocking", "bitreq/https"]
49+
blocking-https-rustls = ["blocking", "bitreq/https-rustls"]
50+
# TODO: (@oleonardolima) do we still need these features/support ? are they being used for wasm ?
51+
# blocking-https-native = ["blocking", "minreq/https-native"]
52+
# blocking-https-bundled = ["blocking", "minreq/https-bundled"]
4353

4454
tokio = ["dep:tokio"]
4555
async = ["reqwest", "reqwest/socks", "tokio?/time"]

src/blocking.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// You may not use this file except in accordance with one or both of these
1010
// licenses.
1111

12-
//! Esplora by way of `minreq` HTTP client.
12+
//! Esplora by way of `bitreq` HTTP client.
1313
1414
use std::collections::HashMap;
1515
use std::convert::TryFrom;
@@ -19,7 +19,7 @@ use std::thread;
1919
#[allow(unused_imports)]
2020
use log::{debug, error, info, trace};
2121

22-
use minreq::{Proxy, Request, Response};
22+
use bitreq::{Proxy, Request, Response};
2323

2424
use bitcoin::consensus::{deserialize, serialize, Decodable};
2525
use bitcoin::hashes::{sha256, Hash};
@@ -68,7 +68,7 @@ impl BlockingClient {
6868

6969
/// Perform a raw HTTP GET request with the given URI `path`.
7070
pub fn get_request(&self, path: &str) -> Result<Request, Error> {
71-
let mut request = minreq::get(format!("{}{}", self.url, path));
71+
let mut request = bitreq::get(format!("{}{}", self.url, path));
7272

7373
if let Some(proxy) = &self.proxy {
7474
let proxy = Proxy::new(proxy.as_str())?;
@@ -110,7 +110,7 @@ impl BlockingClient {
110110
Err(Error::HttpResponse { status, message })
111111
}
112112
Ok(resp) => Ok(Some(
113-
Txid::from_str(resp.as_str().map_err(Error::Minreq)?).map_err(Error::HexToArray)?,
113+
Txid::from_str(resp.as_str().map_err(Error::BitReq)?).map_err(Error::HexToArray)?,
114114
)),
115115
Err(e) => Err(e),
116116
}
@@ -125,7 +125,7 @@ impl BlockingClient {
125125
Err(Error::HttpResponse { status, message })
126126
}
127127
Ok(resp) => {
128-
let hex_str = resp.as_str().map_err(Error::Minreq)?;
128+
let hex_str = resp.as_str().map_err(Error::BitReq)?;
129129
let hex_vec = Vec::from_hex(hex_str).unwrap();
130130
deserialize::<T>(&hex_vec)
131131
.map_err(Error::BitcoinEncoding)
@@ -143,7 +143,7 @@ impl BlockingClient {
143143
Err(Error::HttpResponse { status, message })
144144
}
145145
Ok(resp) => {
146-
let hex_str = resp.as_str().map_err(Error::Minreq)?;
146+
let hex_str = resp.as_str().map_err(Error::BitReq)?;
147147
let hex_vec = Vec::from_hex(hex_str).unwrap();
148148
deserialize::<T>(&hex_vec).map_err(Error::BitcoinEncoding)
149149
}
@@ -162,7 +162,7 @@ impl BlockingClient {
162162
let message = resp.as_str().unwrap_or_default().to_string();
163163
Err(Error::HttpResponse { status, message })
164164
}
165-
Ok(resp) => Ok(resp.json::<T>().map_err(Error::Minreq)?),
165+
Ok(resp) => Ok(resp.json()?),
166166
Err(e) => Err(e),
167167
}
168168
}
@@ -178,7 +178,7 @@ impl BlockingClient {
178178
let message = resp.as_str().unwrap_or_default().to_string();
179179
Err(Error::HttpResponse { status, message })
180180
}
181-
Ok(resp) => Ok(Some(resp.json::<T>()?)),
181+
Ok(resp) => Ok(Some(resp.json()?)),
182182
Err(e) => Err(e),
183183
}
184184
}
@@ -268,7 +268,7 @@ impl BlockingClient {
268268

269269
/// Broadcast a [`Transaction`] to Esplora
270270
pub fn broadcast(&self, transaction: &Transaction) -> Result<(), Error> {
271-
let mut request = minreq::post(format!("{}/tx", self.url)).with_body(
271+
let mut request = bitreq::post(format!("{}/tx", self.url)).with_body(
272272
serialize(transaction)
273273
.to_lower_hex_string()
274274
.as_bytes()
@@ -291,7 +291,7 @@ impl BlockingClient {
291291
Err(Error::HttpResponse { status, message })
292292
}
293293
Ok(_resp) => Ok(()),
294-
Err(e) => Err(Error::Minreq(e)),
294+
Err(e) => Err(Error::BitReq(e)),
295295
}
296296
}
297297

src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@
6565
//! certificates.
6666
//!
6767
//! [`dont remove this line or cargo doc will break`]: https://example.com
68-
#![cfg_attr(not(feature = "minreq"), doc = "[`minreq`]: https://docs.rs/minreq")]
68+
#![cfg_attr(not(feature = "bitreq"), doc = "[`bitreq`]: https://docs.rs/bitreq")]
6969
#![cfg_attr(not(feature = "reqwest"), doc = "[`reqwest`]: https://docs.rs/reqwest")]
7070
#![allow(clippy::result_large_err)]
7171

7272
use std::collections::HashMap;
73-
use std::fmt;
7473
use std::num::TryFromIntError;
7574
use std::time::Duration;
75+
use std::{fmt, str};
7676

7777
#[cfg(feature = "async")]
7878
pub use r#async::Sleeper;
@@ -89,6 +89,9 @@ pub use blocking::BlockingClient;
8989
#[cfg(feature = "async")]
9090
pub use r#async::AsyncClient;
9191

92+
// #[cfg(feature = "bitreq")]
93+
// use crate::bitreq::;
94+
9295
/// Response status codes for which the request may be retried.
9396
pub const RETRYABLE_ERROR_CODES: [u16; 3] = [
9497
429, // TOO_MANY_REQUESTS
@@ -200,9 +203,9 @@ impl Builder {
200203
/// Errors that can happen during a request to `Esplora` servers.
201204
#[derive(Debug)]
202205
pub enum Error {
203-
/// Error during `minreq` HTTP request
206+
/// Error during `bitreq` HTTP request
204207
#[cfg(feature = "blocking")]
205-
Minreq(::minreq::Error),
208+
BitReq(::bitreq::Error),
206209
/// Error during reqwest HTTP request
207210
#[cfg(feature = "async")]
208211
Reqwest(::reqwest::Error),
@@ -253,7 +256,7 @@ macro_rules! impl_error {
253256

254257
impl std::error::Error for Error {}
255258
#[cfg(feature = "blocking")]
256-
impl_error!(::minreq::Error, Minreq, Error);
259+
impl_error!(::bitreq::Error, BitReq, Error);
257260
#[cfg(feature = "async")]
258261
impl_error!(::reqwest::Error, Reqwest, Error);
259262
impl_error!(std::num::ParseIntError, Parsing, Error);
@@ -325,7 +328,7 @@ mod test {
325328

326329
let mut builder = Builder::new(&format!("http://{esplora_url}"));
327330
if !headers.is_empty() {
328-
builder.headers = headers;
331+
builder.headers = headers.clone();
329332
}
330333

331334
let blocking_client = builder.build_blocking();
@@ -951,7 +954,7 @@ mod test {
951954
assert_eq!(blocks_genesis, blocks_genesis_async);
952955
}
953956

954-
#[cfg(all(feature = "blocking", feature = "async"))]
957+
#[cfg(all(feature = "blocking", feature = "async", feature = "bitreq"))]
955958
#[tokio::test]
956959
async fn test_get_tx_with_http_header() {
957960
let headers = [(

0 commit comments

Comments
 (0)