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,9 @@ pub enum Error<E> {
375376 Client ( E ) ,
376377}
377378
379+ // pub type FutureResult<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'a>>;
380+
381+ #[ async_trait]
378382pub trait Client {
379383 fn request ( & self , base_url : & str ) -> Request ;
380384
@@ -383,11 +387,38 @@ pub trait Client {
383387 F : FnMut ( Request ) -> Result < Response , E > ,
384388 {
385389 let request = self . request ( base_url) ;
386- let response = handler ( request) . map_err ( Error :: Client ) ?;
390+ handler ( request) . map_err ( Error :: Client )
391+ }
387392
388- Ok ( response)
393+ async fn send_async < ' a , F , Fut , E > (
394+ & ' a self ,
395+ base_url : & ' a str ,
396+ handler : & ' a mut F ,
397+ ) -> Result < Response , Error < E > >
398+ where
399+ F : FnMut ( Request ) -> Fut + Send ,
400+ Fut : Future < Output = Result < Response , E > > + Send + Sync ,
401+ Self : Sync ,
402+ {
403+ let request = self . request ( base_url) ;
404+ handler ( request) . await . map_err ( Error :: Client )
389405 }
390406
407+ // fn send_async<'a, F, E>(
408+ // &'a self,
409+ // base_url: &'a str,
410+ // handler: &'a mut F,
411+ // ) -> FutureResult<'a, Response, Error<E>>
412+ // where
413+ // F: FnMut(Request) -> FutureResult<'a, Response, E> + Send + Sync,
414+ // Self: Sync,
415+ // {
416+ // Box::pin(async move {
417+ // let request = self.request(base_url);
418+ // handler(request).await.map_err(Error::Client)
419+ // })
420+ // }
421+
391422 fn deserialize_decodable < T : Decodable > ( & self , response : & Response ) -> Result < T , crate :: Error > ;
392423
393424 fn deserialize_json < T : serde:: de:: DeserializeOwned > (
0 commit comments