@@ -27,30 +27,28 @@ pub struct EclairConnection {
27
27
pub api_password : String ,
28
28
}
29
29
30
- impl EclairConnection {
31
- fn construct_url ( & self , endpoint : & str ) -> Result < Url , Box < dyn Error > > {
32
- Ok ( Url :: parse ( & self . base_url ) ?. join ( endpoint) ?)
33
- }
34
- }
35
-
36
30
pub struct EclairNode {
37
- connection : EclairConnection ,
31
+ base_url : Url ,
32
+ api_username : String ,
33
+ api_password : String ,
38
34
http_client : Client ,
39
35
info : NodeInfo ,
40
36
network : Network ,
41
37
}
42
38
43
39
impl EclairNode {
44
40
async fn request_static < T : for < ' de > Deserialize < ' de > > (
45
- connection : & EclairConnection ,
46
41
client : & Client ,
42
+ base_url : & Url ,
43
+ api_username : & str ,
44
+ api_password : & str ,
47
45
endpoint : & str ,
48
46
params : Option < HashMap < String , String > > ,
49
47
) -> Result < T , Box < dyn Error > > {
50
- let url = connection . construct_url ( endpoint) ?;
48
+ let url = base_url . join ( endpoint) ?;
51
49
let mut request = client
52
50
. request ( Method :: POST , url)
53
- . basic_auth ( & connection . api_username , Some ( & connection . api_password ) ) ;
51
+ . basic_auth ( api_username, Some ( api_password) ) ;
54
52
55
53
if let Some ( params) = params {
56
54
let mut form = Form :: new ( ) ;
@@ -75,15 +73,31 @@ impl EclairNode {
75
73
endpoint : & str ,
76
74
params : Option < HashMap < String , String > > ,
77
75
) -> Result < T , Box < dyn Error > > {
78
- EclairNode :: request_static ( & self . connection , & self . http_client , endpoint, params) . await
76
+ EclairNode :: request_static (
77
+ & self . http_client ,
78
+ & self . base_url ,
79
+ & self . api_username ,
80
+ & self . api_password ,
81
+ endpoint,
82
+ params,
83
+ )
84
+ . await
79
85
}
80
86
81
87
pub async fn new ( connection : EclairConnection ) -> Result < Self , LightningError > {
88
+ let base_url = Url :: parse ( & connection. base_url )
89
+ . map_err ( |err| LightningError :: GetInfoError ( err. to_string ( ) ) ) ?;
82
90
let client = Client :: new ( ) ;
83
- let info: GetInfoResponse =
84
- EclairNode :: request_static ( & connection, & client, "getinfo" , None )
85
- . await
86
- . map_err ( |err| LightningError :: GetInfoError ( err. to_string ( ) ) ) ?;
91
+ let info: GetInfoResponse = EclairNode :: request_static (
92
+ & client,
93
+ & base_url,
94
+ & connection. api_username ,
95
+ & connection. api_password ,
96
+ "getinfo" ,
97
+ None ,
98
+ )
99
+ . await
100
+ . map_err ( |err| LightningError :: GetInfoError ( err. to_string ( ) ) ) ?;
87
101
let pubkey = PublicKey :: from_str ( info. node_id . as_str ( ) )
88
102
. map_err ( |err| LightningError :: GetInfoError ( err. to_string ( ) ) ) ?;
89
103
let network = Network :: from_str ( match info. network . as_str ( ) {
@@ -99,8 +113,10 @@ impl EclairNode {
99
113
let features = parse_json_to_node_features ( & info. features ) ;
100
114
101
115
Ok ( Self {
116
+ base_url,
117
+ api_username : connection. api_username ,
118
+ api_password : connection. api_password ,
102
119
http_client : client,
103
- connection,
104
120
info : NodeInfo {
105
121
pubkey,
106
122
alias : info. alias ,
0 commit comments