22
33use bitcoin:: { Address , BlockHash } ;
44use reqwest:: { Client , Response } ;
5+ use serde:: de:: DeserializeOwned ;
56use url:: Url ;
67
78use crate :: builder:: MempoolClientBuilder ;
89use crate :: error:: Error ;
910use crate :: response:: {
1011 AddressStats , BlockInfo , BlockInfoV1 , DifficultyAdjustment , FeeRecommendations , HashrateStats ,
11- MempoolBlockFees , MempoolStats , Prices ,
12+ MempoolBlockFees , MempoolResponse , MempoolStats , Prices ,
1213} ;
1314#[ cfg( feature = "ws" ) ]
1415use crate :: websocket:: { self , MempoolSubscription , MempoolSubscriptionRequest } ;
@@ -86,18 +87,25 @@ impl MempoolClient {
8687 Self { client, url }
8788 }
8889
90+ async fn get_response < T > ( & self , url : Url ) -> Result < T , Error >
91+ where
92+ T : DeserializeOwned ,
93+ {
94+ let response: Response = self . client . get ( url) . send ( ) . await ?;
95+ let response: MempoolResponse < T > = response. json ( ) . await ?;
96+ response. into_result ( )
97+ }
98+
8999 /// Get details about difficulty adjustment.
90100 pub async fn get_difficulty_adjustment ( & self ) -> Result < DifficultyAdjustment , Error > {
91101 let url: Url = self . url . join ( "/api/v1/difficulty-adjustment" ) ?;
92- let response: Response = self . client . get ( url) . send ( ) . await ?;
93- Ok ( response. json ( ) . await ?)
102+ self . get_response ( url) . await
94103 }
95104
96105 /// Get bitcoin latest price denominated in main currencies.
97106 pub async fn get_prices ( & self ) -> Result < Prices , Error > {
98107 let url: Url = self . url . join ( "/api/v1/prices" ) ?;
99- let response: Response = self . client . get ( url) . send ( ) . await ?;
100- Ok ( response. json ( ) . await ?)
108+ self . get_response ( url) . await
101109 }
102110
103111 /// Get details about an address.
@@ -106,15 +114,13 @@ impl MempoolClient {
106114 . url
107115 . join ( "/api/address/" ) ?
108116 . join ( address. to_string ( ) . as_str ( ) ) ?;
109- let response: Response = self . client . get ( url) . send ( ) . await ?;
110- Ok ( response. json ( ) . await ?)
117+ self . get_response ( url) . await
111118 }
112119
113120 /// Get the height of the last block.
114121 pub async fn get_block_tip_height ( & self ) -> Result < u32 , Error > {
115122 let url: Url = self . url . join ( "/api/blocks/tip/height" ) ?;
116- let response: Response = self . client . get ( url) . send ( ) . await ?;
117- Ok ( response. json ( ) . await ?)
123+ self . get_response ( url) . await
118124 }
119125
120126 /// Get the block information
@@ -123,8 +129,7 @@ impl MempoolClient {
123129 . url
124130 . join ( "/api/block/" ) ?
125131 . join ( hash. to_string ( ) . as_str ( ) ) ?;
126- let response: Response = self . client . get ( url) . send ( ) . await ?;
127- Ok ( response. json ( ) . await ?)
132+ self . get_response ( url) . await
128133 }
129134
130135 /// Get the block information (v1)
@@ -133,8 +138,7 @@ impl MempoolClient {
133138 . url
134139 . join ( "/api/v1/block/" ) ?
135140 . join ( hash. to_string ( ) . as_str ( ) ) ?;
136- let response: Response = self . client . get ( url) . send ( ) . await ?;
137- Ok ( response. json ( ) . await ?)
141+ self . get_response ( url) . await
138142 }
139143
140144 /// Get the details on the past 10 blocks.
@@ -148,8 +152,7 @@ impl MempoolClient {
148152 url = url. join ( start_height. to_string ( ) . as_str ( ) ) ?;
149153 }
150154
151- let response: Response = self . client . get ( url) . send ( ) . await ?;
152- Ok ( response. json ( ) . await ?)
155+ self . get_response ( url) . await
153156 }
154157
155158 /// Get network-wide hashrate and difficulty figures over the last 3 days.
@@ -158,29 +161,25 @@ impl MempoolClient {
158161 . url
159162 . join ( "/api/v1/mining/hashrate/" ) ?
160163 . join ( period. as_str ( ) ) ?;
161- let response: Response = self . client . get ( url) . send ( ) . await ?;
162- Ok ( response. json ( ) . await ?)
164+ self . get_response ( url) . await
163165 }
164166
165167 /// Get currently suggested fees for new transactions.
166168 pub async fn get_recommended_fees ( & self ) -> Result < FeeRecommendations , Error > {
167169 let url: Url = self . url . join ( "/api/v1/fees/recommended" ) ?;
168- let response: Response = self . client . get ( url) . send ( ) . await ?;
169- Ok ( response. json ( ) . await ?)
170+ self . get_response ( url) . await
170171 }
171172
172173 /// Get current mempool backlog statistics.
173174 pub async fn get_mempool ( & self ) -> Result < MempoolStats , Error > {
174175 let url: Url = self . url . join ( "/api/mempool" ) ?;
175- let response: Response = self . client . get ( url) . send ( ) . await ?;
176- Ok ( response. json ( ) . await ?)
176+ self . get_response ( url) . await
177177 }
178178
179179 /// Get current mempool as projected blocks.
180180 pub async fn get_mempool_blocks_fees ( & self ) -> Result < Vec < MempoolBlockFees > , Error > {
181181 let url: Url = self . url . join ( "/api/v1/fees/mempool-blocks" ) ?;
182- let response: Response = self . client . get ( url) . send ( ) . await ?;
183- Ok ( response. json ( ) . await ?)
182+ self . get_response ( url) . await
184183 }
185184
186185 /// Subscribe to mempool space websocket.
0 commit comments