Skip to content

Commit 002abbb

Browse files
committed
feat(pubky): add PubkyClientBuilder::testnet() and skip setting dht_request_timeout in CI
1 parent 16771d6 commit 002abbb

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

pubky/src/native.rs

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::time::Duration;
22

3-
use ::pkarr::{
4-
mainline::dht::{DhtSettings, Testnet},
5-
PkarrClient, PublicKey, SignedPacket,
6-
};
3+
use ::pkarr::{mainline::dht::Testnet, PkarrClient, PublicKey, SignedPacket};
74
use bytes::Bytes;
85
use pkarr::Keypair;
96
use pubky_common::session::Session;
@@ -21,21 +18,30 @@ use crate::{
2118

2219
static DEFAULT_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
2320

24-
impl Default for PubkyClient {
25-
fn default() -> Self {
26-
Self::new()
27-
}
28-
}
29-
3021
#[derive(Debug, Default)]
3122
pub struct PubkyClientBuilder {
32-
pkarr_settings: Option<pkarr::Settings>,
23+
pkarr_settings: pkarr::Settings,
3324
}
3425

3526
impl PubkyClientBuilder {
3627
/// Set Pkarr client [pkarr::Settings].
3728
pub fn pkarr_settings(mut self, settings: pkarr::Settings) -> Self {
38-
self.pkarr_settings = settings.into();
29+
self.pkarr_settings = settings;
30+
self
31+
}
32+
33+
/// Use the bootstrap nodes of a testnet, useful mostly in unit tests.
34+
pub fn testnet(self, testnet: &Testnet) -> Self {
35+
self.bootstrap(testnet.bootstrap.to_vec())
36+
}
37+
38+
pub fn dht_request_timeout(mut self, timeout: Duration) -> Self {
39+
self.pkarr_settings.dht.request_timeout = timeout.into();
40+
self
41+
}
42+
43+
pub fn bootstrap(mut self, bootstrap: Vec<String>) -> Self {
44+
self.pkarr_settings.dht.bootstrap = bootstrap.into();
3945
self
4046
}
4147

@@ -47,51 +53,38 @@ impl PubkyClientBuilder {
4753
.user_agent(DEFAULT_USER_AGENT)
4854
.build()
4955
.unwrap(),
50-
pkarr: PkarrClient::new(self.pkarr_settings.unwrap_or_default())
51-
.unwrap()
52-
.as_async(),
56+
pkarr: PkarrClient::new(self.pkarr_settings).unwrap().as_async(),
5357
}
5458
}
5559
}
5660

61+
impl Default for PubkyClient {
62+
fn default() -> Self {
63+
PubkyClient::builder().build()
64+
}
65+
}
66+
5767
// === Public API ===
5868

5969
impl PubkyClient {
60-
pub fn new() -> Self {
61-
Self {
62-
http: reqwest::Client::builder()
63-
.cookie_store(true)
64-
.user_agent(DEFAULT_USER_AGENT)
65-
.build()
66-
.unwrap(),
67-
pkarr: PkarrClient::new(Default::default()).unwrap().as_async(),
68-
}
69-
}
70-
7170
/// Returns a builder to edit settings before creating [PubkyClient].
7271
pub fn builder() -> PubkyClientBuilder {
7372
PubkyClientBuilder::default()
7473
}
7574

76-
pub fn test(testnet: &Testnet) -> Self {
77-
let pkarr = PkarrClient::builder()
78-
.dht_settings(DhtSettings {
79-
request_timeout: Some(Duration::from_millis(500)),
80-
bootstrap: Some(testnet.bootstrap.to_owned()),
81-
..DhtSettings::default()
82-
})
83-
.resolvers(testnet.bootstrap.clone().into())
84-
.build()
85-
.unwrap()
86-
.as_async();
87-
88-
let http = reqwest::Client::builder()
89-
.cookie_store(true)
90-
.user_agent(DEFAULT_USER_AGENT)
91-
.build()
92-
.unwrap();
93-
94-
Self { http, pkarr }
75+
/// Creates a [PubkyClient] with:
76+
/// - DHT bootstrap nodes set to the `testnet` bootstrap nodes.
77+
/// - DHT request timout set to 500 milliseconds. (unless in CI, then it is left as default)
78+
///
79+
/// For more control, you can use [PubkyClientBuilder::testnet]
80+
pub fn test(testnet: &Testnet) -> PubkyClient {
81+
let mut builder = PubkyClient::builder().testnet(testnet);
82+
83+
if std::env::var("CI").is_err() {
84+
builder = builder.dht_request_timeout(Duration::from_millis(100));
85+
}
86+
87+
builder.build()
9588
}
9689

9790
// === Auth ===

0 commit comments

Comments
 (0)