1
1
use std:: time:: Duration ;
2
2
3
- use :: pkarr:: {
4
- mainline:: dht:: { DhtSettings , Testnet } ,
5
- PkarrClient , PublicKey , SignedPacket ,
6
- } ;
3
+ use :: pkarr:: { mainline:: dht:: Testnet , PkarrClient , PublicKey , SignedPacket } ;
7
4
use bytes:: Bytes ;
8
5
use pkarr:: Keypair ;
9
6
use pubky_common:: session:: Session ;
@@ -21,21 +18,30 @@ use crate::{
21
18
22
19
static DEFAULT_USER_AGENT : & str = concat ! ( env!( "CARGO_PKG_NAME" ) , "/" , env!( "CARGO_PKG_VERSION" ) , ) ;
23
20
24
- impl Default for PubkyClient {
25
- fn default ( ) -> Self {
26
- Self :: new ( )
27
- }
28
- }
29
-
30
21
#[ derive( Debug , Default ) ]
31
22
pub struct PubkyClientBuilder {
32
- pkarr_settings : Option < pkarr:: Settings > ,
23
+ pkarr_settings : pkarr:: Settings ,
33
24
}
34
25
35
26
impl PubkyClientBuilder {
36
27
/// Set Pkarr client [pkarr::Settings].
37
28
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 ( ) ;
39
45
self
40
46
}
41
47
@@ -47,51 +53,38 @@ impl PubkyClientBuilder {
47
53
. user_agent ( DEFAULT_USER_AGENT )
48
54
. build ( )
49
55
. 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 ( ) ,
53
57
}
54
58
}
55
59
}
56
60
61
+ impl Default for PubkyClient {
62
+ fn default ( ) -> Self {
63
+ PubkyClient :: builder ( ) . build ( )
64
+ }
65
+ }
66
+
57
67
// === Public API ===
58
68
59
69
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
-
71
70
/// Returns a builder to edit settings before creating [PubkyClient].
72
71
pub fn builder ( ) -> PubkyClientBuilder {
73
72
PubkyClientBuilder :: default ( )
74
73
}
75
74
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 ( )
95
88
}
96
89
97
90
// === Auth ===
0 commit comments