14
14
use std:: collections:: HashMap ;
15
15
use std:: marker:: PhantomData ;
16
16
use std:: str:: FromStr ;
17
- use std:: time:: Duration ; //-----------------------added----------------
18
- use std:: convert:: TryInto ; //-------------------added-----------------
19
-
20
17
use bitcoin:: consensus:: { deserialize, serialize, Decodable , Encodable } ;
21
18
use bitcoin:: hashes:: { sha256, Hash } ;
22
19
use bitcoin:: hex:: { DisplayHex , FromHex } ;
@@ -64,58 +61,22 @@ pub struct AsyncClient<S = DefaultSleeper> {
64
61
impl < S : Sleeper > AsyncClient < S > {
65
62
/// Build an async client from a builder
66
63
pub fn from_builder ( builder : Builder ) -> Result < Self , Error > {
67
-
68
- //----------------------------------------not needed since no client struct in async minreq------------------------------------
69
- // let mut client_builder = Client::builder();
70
-
71
- // #[cfg(not(target_arch = "wasm32"))]
72
- // if let Some(proxy) = &builder.proxy {
73
- // client_builder = client_builder.proxy(reqwest::Proxy::all(proxy)?);
74
- // }
75
-
76
- // #[cfg(not(target_arch = "wasm32"))]
77
- // if let Some(timeout) = builder.timeout {
78
- // client_builder = client_builder.timeout(core::time::Duration::from_secs(timeout));
79
- // }
80
-
81
- // if !builder.headers.is_empty() {
82
- // let mut headers = header::HeaderMap::new();
83
- // for (k, v) in builder.headers {
84
- // let header_name = header::HeaderName::from_lowercase(k.to_lowercase().as_bytes())
85
- // .map_err(|_| Error::InvalidHttpHeaderName(k))?;
86
- // let header_value = header::HeaderValue::from_str(&v)
87
- // .map_err(|_| Error::InvalidHttpHeaderValue(v))?;
88
- // headers.insert(header_name, header_value);
89
- // }
90
- // client_builder = client_builder.default_headers(headers);
91
- // }
92
-
93
- // Ok(AsyncClient {
94
- // url: builder.base_url,
95
- // client: client_builder.build()?,
96
- // max_retries: builder.max_retries,
97
- // marker: PhantomData,
98
- // })
99
-
100
- //--------------------------------------------------------------------------------------
101
64
Ok ( AsyncClient {
102
65
url : builder. base_url ,
103
66
max_retries : builder. max_retries ,
104
67
headers : builder. headers ,
105
68
marker : PhantomData ,
106
69
} )
107
-
108
-
109
70
}
110
71
111
- pub fn from_client ( url : String , client : Client ) -> Self {
72
+ pub fn from_client ( url : String , headers : HashMap < String , String > , ) -> Self {
112
73
AsyncClient {
113
74
url,
114
75
headers,
115
76
max_retries : crate :: DEFAULT_MAX_RETRIES ,
116
77
marker : PhantomData ,
117
78
}
118
- }
79
+ }
119
80
/// Make an HTTP GET request to given URL, deserializing to any `T` that
120
81
/// implement [`bitcoin::consensus::Decodable`].
121
82
///
@@ -131,13 +92,12 @@ impl<S: Sleeper> AsyncClient<S> {
131
92
let url = format ! ( "{}{}" , self . url, path) ;
132
93
let response = self . get_with_retry ( & url) . await ?;
133
94
134
- if ! ( response. status_code == 200 ) {
95
+ if response. status_code > 299 {
135
96
return Err ( Error :: HttpResponse {
136
97
status : response. status_code as u16 ,
137
98
message : response. as_str ( ) . unwrap ( ) . to_string ( ) ,
138
99
} ) ;
139
100
}
140
-
141
101
Ok ( deserialize :: < T > ( & response. as_bytes ( ) ) ?)
142
102
}
143
103
@@ -171,13 +131,12 @@ impl<S: Sleeper> AsyncClient<S> {
171
131
let url = format ! ( "{}{}" , self . url, path) ;
172
132
let response = self . get_with_retry ( & url) . await ?;
173
133
174
- if ! ( response. status_code == 200 ) {
134
+ if response. status_code > 299 {
175
135
return Err ( Error :: HttpResponse {
176
136
status : response. status_code as u16 ,
177
137
message : response. as_str ( ) . unwrap ( ) . to_string ( ) ,
178
138
} ) ;
179
139
}
180
-
181
140
serde_json:: from_str ( & response. as_str ( ) . unwrap ( ) . to_string ( ) ) . map_err ( Error :: Json )
182
141
}
183
142
@@ -213,13 +172,12 @@ impl<S: Sleeper> AsyncClient<S> {
213
172
let url = format ! ( "{}{}" , self . url, path) ;
214
173
let response = self . get_with_retry ( & url) . await ?;
215
174
216
- if ! ( response. status_code == 200 ) {
175
+ if response. status_code > 299 {
217
176
return Err ( Error :: HttpResponse {
218
177
status : response. status_code as u16 ,
219
178
message : response. as_str ( ) . unwrap ( ) . to_string ( ) ,
220
179
} ) ;
221
180
}
222
-
223
181
let hex_str =response. as_str ( ) . unwrap ( ) . to_string ( ) ;
224
182
Ok ( deserialize ( & Vec :: from_hex ( & hex_str) ?) ?)
225
183
}
@@ -250,13 +208,12 @@ impl<S: Sleeper> AsyncClient<S> {
250
208
let url = format ! ( "{}{}" , self . url, path) ;
251
209
let response = self . get_with_retry ( & url) . await ?;
252
210
253
- if ! ( response. status_code == 200 ) {
211
+ if response. status_code > 299 {
254
212
return Err ( Error :: HttpResponse {
255
213
status : response. status_code as u16 ,
256
214
message : response. as_str ( ) . unwrap ( ) . to_string ( ) ,
257
215
} ) ;
258
216
}
259
- // let x=response.as_str().unwrap().to_string();
260
217
Ok ( response. as_str ( ) . unwrap ( ) . to_string ( ) )
261
218
}
262
219
@@ -293,7 +250,13 @@ impl<S: Sleeper> AsyncClient<S> {
293
250
request = request. with_header ( key, value) ;
294
251
}
295
252
296
- let _ = request. send ( ) . await . map_err ( Error :: AsyncMinreq ) ?;
253
+ let response = request. send ( ) . await . map_err ( Error :: AsyncMinreq ) ?;
254
+ if response. status_code >299 {
255
+ return Err ( Error :: HttpResponse {
256
+ status : response. status_code as u16 ,
257
+ message : response. as_str ( ) . unwrap ( ) . to_string ( ) ,
258
+ } ) ;
259
+ }
297
260
Ok ( ( ) )
298
261
}
299
262
@@ -489,39 +452,26 @@ impl<S: Sleeper> AsyncClient<S> {
489
452
490
453
loop {
491
454
let mut request = Request :: new ( Method :: Get , url) ;
492
- // Apply headers from the builder.
455
+ // Applying headers from the builder.
493
456
for ( key, value) in & self . headers {
494
457
request = request. with_header ( key, value) ;
495
458
}
496
- // request.
497
-
498
- let res = request. send ( ) . await . map_err ( Error :: AsyncMinreq ) ;
499
-
500
- match res {
501
- Ok ( body) => {
502
-
503
-
504
- return Ok ( body) ;
505
-
506
- } ,
507
- Err ( e) => {
508
-
509
- if attempts < self . max_retries {
510
- S :: sleep ( delay) . await ;
511
- attempts += 1 ;
512
- delay *= 2 ;
513
- continue ;
514
- }
515
- return Err ( e) ;
459
+
460
+ match request. send ( ) . await ? {
461
+ resp if attempts < self . max_retries && is_status_retryable ( resp. status_code ) => {
462
+ S :: sleep ( delay) . await ;
463
+ attempts += 1 ;
464
+ delay *= 2 ;
516
465
}
466
+ resp => return Ok ( resp) ,
517
467
}
518
468
}
519
469
}
520
470
}
521
471
522
- // fn is_status_retryable(status: reqwest::StatusCode ) -> bool {
523
- // RETRYABLE_ERROR_CODES.contains(&status.as_u16( ))
524
- // }
472
+ fn is_status_retryable ( status : i32 ) -> bool {
473
+ RETRYABLE_ERROR_CODES . contains ( & ( status as u16 ) )
474
+ }
525
475
526
476
pub trait Sleeper : ' static {
527
477
type Sleep : std:: future:: Future < Output = ( ) > ;
@@ -538,4 +488,4 @@ impl Sleeper for DefaultSleeper {
538
488
fn sleep ( dur : std:: time:: Duration ) -> Self :: Sleep {
539
489
tokio:: time:: sleep ( dur)
540
490
}
541
- }
491
+ }
0 commit comments