33//! See: <https://github.com/Blockstream/esplora/blob/master/API.md>
44
55use core:: str;
6- use std:: { collections :: HashMap , str:: FromStr } ;
6+ use std:: { future :: Future , str:: FromStr } ;
77
8+ use async_trait:: async_trait;
89use bitcoin:: consensus:: Decodable ;
910pub use bitcoin:: consensus:: { deserialize, serialize} ;
1011use bitcoin:: hashes:: sha256:: Hash ;
@@ -46,25 +47,25 @@ impl Request {
4647pub struct Response {
4748 pub status_code : i32 ,
4849 pub body : Vec < u8 > ,
49- pub reason : String ,
50- pub headers : HashMap < String , String > ,
51- pub url : Url ,
50+ // pub reason: String,
51+ // pub headers: HashMap<String, String>,
52+ // pub url: Url,
5253}
5354
5455impl Response {
5556 pub fn new (
5657 status_code : i32 ,
5758 body : Vec < u8 > ,
58- reason_phrase : String ,
59- headers : HashMap < String , String > ,
60- url : Url ,
59+ // reason_phrase: String,
60+ // headers: HashMap<String, String>,
61+ // url: Url,
6162 ) -> Self {
6263 Self {
6364 status_code,
6465 body,
65- reason : reason_phrase,
66- headers,
67- url,
66+ // reason: reason_phrase,
67+ // headers,
68+ // url,
6869 }
6970 }
7071
@@ -375,6 +376,7 @@ pub enum Error<E> {
375376 Client ( E ) ,
376377}
377378
379+ #[ async_trait]
378380pub trait Client {
379381 fn request ( & self , base_url : & str ) -> Request ;
380382
@@ -383,9 +385,21 @@ pub trait Client {
383385 F : FnMut ( Request ) -> Result < Response , E > ,
384386 {
385387 let request = self . request ( base_url) ;
386- let response = handler ( request) . map_err ( Error :: Client ) ?;
388+ handler ( request) . map_err ( Error :: Client )
389+ }
387390
388- Ok ( response)
391+ async fn send_async < ' a , F , Fut , E > (
392+ & ' a self ,
393+ base_url : & ' a str ,
394+ handler : & ' a mut F ,
395+ ) -> Result < Response , Error < E > >
396+ where
397+ F : FnMut ( Request ) -> Fut + Send ,
398+ Fut : Future < Output = Result < Response , E > > + Send + Sync ,
399+ Self : Sync ,
400+ {
401+ let request = self . request ( base_url) ;
402+ handler ( request) . await . map_err ( Error :: Client )
389403 }
390404
391405 fn deserialize_decodable < T : Decodable > ( & self , response : & Response ) -> Result < T , crate :: Error > ;
0 commit comments