1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::{error, time::Duration};
pub use http::{self, Request, Response};
pub type Body = Vec<u8>;
pub const MIME_APPLICATION_JSON: &str = "application/json";
pub trait Endpoint {
type RenderRequestError: error::Error + 'static;
type ParseResponseOutput;
type ParseResponseError: error::Error + 'static;
fn render_request(&self) -> Result<Request<Body>, Self::RenderRequestError>;
fn parse_response(
&self,
response: Response<Body>,
) -> Result<Self::ParseResponseOutput, Self::ParseResponseError>;
}
pub trait RetryableEndpoint {
type RetryReason: Send + Sync + Clone;
const MAX_RETRY_COUNT: usize = 3;
type RenderRequestError: error::Error + 'static;
type ParseResponseOutput;
type ParseResponseError: error::Error + 'static;
fn render_request(
&self,
retry: Option<&RetryableEndpointRetry<Self::RetryReason>>,
) -> Result<Request<Body>, Self::RenderRequestError>;
fn parse_response(
&self,
response: Response<Body>,
retry: Option<&RetryableEndpointRetry<Self::RetryReason>>,
) -> Result<Result<Self::ParseResponseOutput, Self::RetryReason>, Self::ParseResponseError>;
fn next_retry_in(&self, retry: &RetryableEndpointRetry<Self::RetryReason>) -> Duration {
match retry.count {
0 | 1 | 2 => Duration::from_millis(500),
_ => Duration::from_secs(1),
}
}
}
#[derive(Debug)]
pub struct RetryableEndpointRetry<T> {
pub count: usize,
pub reason: T,
}
impl<T> RetryableEndpointRetry<T> {
pub fn new(count: usize, reason: T) -> Self {
Self { count, reason }
}
}