3
3
//! See: <https://github.com/Blockstream/esplora/blob/master/API.md>
4
4
5
5
use core:: str;
6
- use std:: { collections :: HashMap , str:: FromStr } ;
6
+ use std:: { future :: Future , str:: FromStr } ;
7
7
8
+ use async_trait:: async_trait;
8
9
use bitcoin:: consensus:: Decodable ;
9
10
pub use bitcoin:: consensus:: { deserialize, serialize} ;
10
11
use bitcoin:: hashes:: sha256:: Hash ;
@@ -46,25 +47,25 @@ impl Request {
46
47
pub struct Response {
47
48
pub status_code : i32 ,
48
49
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,
52
53
}
53
54
54
55
impl Response {
55
56
pub fn new (
56
57
status_code : i32 ,
57
58
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,
61
62
) -> Self {
62
63
Self {
63
64
status_code,
64
65
body,
65
- reason : reason_phrase,
66
- headers,
67
- url,
66
+ // reason: reason_phrase,
67
+ // headers,
68
+ // url,
68
69
}
69
70
}
70
71
@@ -375,6 +376,9 @@ pub enum Error<E> {
375
376
Client ( E ) ,
376
377
}
377
378
379
+ // pub type FutureResult<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'a>>;
380
+
381
+ #[ async_trait]
378
382
pub trait Client {
379
383
fn request ( & self , base_url : & str ) -> Request ;
380
384
@@ -383,11 +387,38 @@ pub trait Client {
383
387
F : FnMut ( Request ) -> Result < Response , E > ,
384
388
{
385
389
let request = self . request ( base_url) ;
386
- let response = handler ( request) . map_err ( Error :: Client ) ?;
390
+ handler ( request) . map_err ( Error :: Client )
391
+ }
387
392
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 )
389
405
}
390
406
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
+
391
422
fn deserialize_decodable < T : Decodable > ( & self , response : & Response ) -> Result < T , crate :: Error > ;
392
423
393
424
fn deserialize_json < T : serde:: de:: DeserializeOwned > (
0 commit comments