Skip to content

Commit 8c3d810

Browse files
committed
Add MempoolClientBuilder and socks feature
Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
1 parent 1211e57 commit 8c3d810

6 files changed

Lines changed: 102 additions & 8 deletions

File tree

.github/workflows/ci.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ jobs:
1919
check:
2020
name: Check crate
2121
runs-on: ubuntu-latest
22+
strategy:
23+
matrix:
24+
crate:
25+
- -p mempoolspace
26+
- -p mempoolspace --features socks
2227
steps:
2328
- name: Checkout
2429
uses: actions/checkout@v4
2530
- name: Check
26-
run: cargo check
31+
run: cargo check -p ${{ matrix.crate }}
2732
- name: Clippy
28-
run: cargo clippy -- -D warnings
33+
run: cargo clippy -p ${{ matrix.crate }} -- -D warnings
2934
- name: Test
30-
run: cargo test
35+
run: cargo test -p ${{ matrix.crate }}

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ name = "mempoolspace"
33
version = "0.1.0"
44
edition = "2024"
55

6-
# TODO: add socks feature
6+
[features]
7+
default = []
8+
# Enable socks proxy
9+
socks = ["reqwest/socks"]
710

811
[dependencies]
912
bitcoin = { version = "0.32", default-features = false, features = ["std", "serde"] }

src/builder.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//! Mempool client builder
2+
3+
#[cfg(feature = "socks")]
4+
use std::net::SocketAddr;
5+
use std::time::Duration;
6+
7+
#[cfg(feature = "socks")]
8+
use reqwest::Proxy;
9+
use reqwest::{Client, ClientBuilder};
10+
use url::Url;
11+
12+
use crate::client::MempoolClient;
13+
use crate::error::Error;
14+
15+
/// Mempool client builder
16+
#[derive(Debug, Clone)]
17+
pub struct MempoolClientBuilder {
18+
/// Endpoint URL
19+
pub url: Url,
20+
/// Timeout for requests
21+
pub timeout: Duration,
22+
/// Socks5 proxy
23+
#[cfg(feature = "socks")]
24+
pub proxy: Option<SocketAddr>,
25+
}
26+
27+
impl MempoolClientBuilder {
28+
/// Construct a new builder
29+
pub fn new(url: Url) -> Self {
30+
Self {
31+
url,
32+
timeout: Duration::from_secs(60),
33+
#[cfg(feature = "socks")]
34+
proxy: None,
35+
}
36+
}
37+
38+
/// Set a custom timeout
39+
#[inline]
40+
pub fn timeout(mut self, timeout: Duration) -> Self {
41+
self.timeout = timeout;
42+
self
43+
}
44+
45+
/// Set proxy
46+
#[inline]
47+
#[cfg(feature = "socks")]
48+
pub fn proxy(mut self, proxy: SocketAddr) -> Self {
49+
self.proxy = Some(proxy);
50+
self
51+
}
52+
53+
/// Build mempool client
54+
pub fn build(self) -> Result<MempoolClient, Error> {
55+
// Construct builder
56+
let mut builder: ClientBuilder = Client::builder();
57+
58+
// Set proxy
59+
#[cfg(all(feature = "socks", not(target_arch = "wasm32")))]
60+
if let Some(proxy) = self.proxy {
61+
let proxy: String = format!("socks5h://{proxy}");
62+
builder = builder.proxy(Proxy::all(proxy)?);
63+
}
64+
65+
// Set timeout
66+
builder = builder.timeout(self.timeout);
67+
68+
// Build client
69+
let client: Client = builder.build()?;
70+
71+
// Construct client
72+
Ok(MempoolClient::from_client(self.url, client))
73+
}
74+
}

src/client.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use bitcoin::Address;
44
use reqwest::{Client, Response};
55
use url::Url;
66

7+
use crate::builder::MempoolClientBuilder;
78
use crate::error::Error;
89
use crate::response::{
910
AddressStats, BlockInfo, DifficultyAdjustment, FeeRecommendations, HashrateStats,
@@ -67,10 +68,19 @@ impl MempoolClient {
6768
/// # let _client = client;
6869
/// ```
6970
pub fn new(url: Url) -> Self {
70-
Self {
71-
url,
72-
client: Client::new(),
73-
}
71+
Self::from_client(url, Client::new())
72+
}
73+
74+
/// Construct a client builder
75+
#[inline]
76+
pub fn builder(url: Url) -> MempoolClientBuilder {
77+
MempoolClientBuilder::new(url)
78+
}
79+
80+
/// Construct new with a custom reqwest [`Client`].
81+
#[inline]
82+
pub fn from_client(url: Url, client: Client) -> Self {
83+
Self { client, url }
7484
}
7585

7686
/// Get details about difficulty adjustment.

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![warn(clippy::large_futures)]
66
#![warn(rustdoc::bare_urls)]
77

8+
pub mod builder;
89
pub mod client;
910
mod deser;
1011
pub mod error;

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pub use bitcoin::*;
88
pub use url::*;
99

10+
pub use crate::builder::*;
1011
pub use crate::client::*;
1112
pub use crate::error::*;
1213
pub use crate::response::*;

0 commit comments

Comments
 (0)