@@ -117,6 +117,8 @@ pub struct Builder {
117117 pub proxy : Option < String > ,
118118 /// Socket timeout.
119119 pub timeout : Option < u64 > ,
120+ /// HTTP headers to set on every request made to Esplora server
121+ pub headers : HashMap < String , String > ,
120122}
121123
122124impl Builder {
@@ -126,6 +128,7 @@ impl Builder {
126128 base_url : base_url. to_string ( ) ,
127129 proxy : None ,
128130 timeout : None ,
131+ headers : HashMap :: new ( ) ,
129132 }
130133 }
131134
@@ -141,6 +144,12 @@ impl Builder {
141144 self
142145 }
143146
147+ /// Add a header to set on each request
148+ pub fn header ( mut self , key : & str , value : & str ) -> Self {
149+ self . headers . insert ( key. to_string ( ) , value. to_string ( ) ) ;
150+ self
151+ }
152+
144153 /// build a blocking client from builder
145154 #[ cfg( feature = "blocking" ) ]
146155 pub fn build_blocking ( self ) -> BlockingClient {
@@ -181,6 +190,10 @@ pub enum Error {
181190 HeaderHeightNotFound ( u32 ) ,
182191 /// Header hash not found
183192 HeaderHashNotFound ( BlockHash ) ,
193+ /// Invalid HTTP Header name specified
194+ InvalidHttpHeaderName ( String ) ,
195+ /// Invalid HTTP Header value specified
196+ InvalidHttpHeaderValue ( String ) ,
184197}
185198
186199impl fmt:: Display for Error {
@@ -261,6 +274,13 @@ mod test {
261274
262275 #[ cfg( all( feature = "blocking" , feature = "async" ) ) ]
263276 async fn setup_clients ( ) -> ( BlockingClient , AsyncClient ) {
277+ setup_clients_with_headers ( HashMap :: new ( ) ) . await
278+ }
279+
280+ #[ cfg( all( feature = "blocking" , feature = "async" ) ) ]
281+ async fn setup_clients_with_headers (
282+ headers : HashMap < String , String > ,
283+ ) -> ( BlockingClient , AsyncClient ) {
264284 PREMINE
265285 . get_or_init ( || async {
266286 let _miner = MINER . lock ( ) . await ;
@@ -270,7 +290,11 @@ mod test {
270290
271291 let esplora_url = ELECTRSD . esplora_url . as_ref ( ) . unwrap ( ) ;
272292
273- let builder = Builder :: new ( & format ! ( "http://{}" , esplora_url) ) ;
293+ let mut builder = Builder :: new ( & format ! ( "http://{}" , esplora_url) ) ;
294+ if !headers. is_empty ( ) {
295+ builder. headers = headers;
296+ }
297+
274298 let blocking_client = builder. build_blocking ( ) ;
275299
276300 let builder_async = Builder :: new ( & format ! ( "http://{}" , esplora_url) ) ;
@@ -851,4 +875,40 @@ mod test {
851875 let blocks_genesis_async = async_client. get_blocks ( Some ( 0 ) ) . await . unwrap ( ) ;
852876 assert_eq ! ( blocks_genesis, blocks_genesis_async) ;
853877 }
878+
879+ #[ cfg( all( feature = "blocking" , feature = "async" ) ) ]
880+ #[ tokio:: test]
881+ async fn test_get_tx_with_http_header ( ) {
882+ let headers = [ (
883+ "Authorization" . to_string ( ) ,
884+ "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" . to_string ( ) ,
885+ ) ]
886+ . into ( ) ;
887+ let ( blocking_client, async_client) = setup_clients_with_headers ( headers) . await ;
888+
889+ let address = BITCOIND
890+ . client
891+ . get_new_address ( Some ( "test" ) , Some ( AddressType :: Legacy ) )
892+ . unwrap ( )
893+ . assume_checked ( ) ;
894+ let txid = BITCOIND
895+ . client
896+ . send_to_address (
897+ & address,
898+ Amount :: from_sat ( 1000 ) ,
899+ None ,
900+ None ,
901+ None ,
902+ None ,
903+ None ,
904+ None ,
905+ )
906+ . unwrap ( ) ;
907+ let _miner = MINER . lock ( ) . await ;
908+ generate_blocks_and_wait ( 1 ) ;
909+
910+ let tx = blocking_client. get_tx ( & txid) . unwrap ( ) ;
911+ let tx_async = async_client. get_tx ( & txid) . await . unwrap ( ) ;
912+ assert_eq ! ( tx, tx_async) ;
913+ }
854914}
0 commit comments