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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::{error, fmt, time::Duration};

#[cfg(feature = "dyn-clone")]
use dyn_clone::{clone_trait_object, DynClone};
pub use http::{self, Request, Response};

pub type Body = Vec<u8>;
pub const MIME_APPLICATION_JSON: &str = "application/json";

//
//
//
#[cfg(feature = "dyn-clone")]
pub trait Endpoint: DynClone {
    type RenderRequestError: error::Error + Send + Sync + 'static;

    type ParseResponseOutput;
    type ParseResponseError: error::Error + Send + Sync + 'static;

    fn render_request(&self) -> Result<Request<Body>, Self::RenderRequestError>;

    fn parse_response(
        &self,
        response: Response<Body>,
    ) -> Result<Self::ParseResponseOutput, Self::ParseResponseError>;
}

#[cfg(not(feature = "dyn-clone"))]
pub trait Endpoint {
    type RenderRequestError: error::Error + Send + Sync + 'static;

    type ParseResponseOutput;
    type ParseResponseError: error::Error + Send + Sync + 'static;

    fn render_request(&self) -> Result<Request<Body>, Self::RenderRequestError>;

    fn parse_response(
        &self,
        response: Response<Body>,
    ) -> Result<Self::ParseResponseOutput, Self::ParseResponseError>;
}

#[cfg(feature = "dyn-clone")]
clone_trait_object!(<RenderRequestError, ParseResponseOutput, ParseResponseError> Endpoint<RenderRequestError = RenderRequestError, ParseResponseOutput = ParseResponseOutput, ParseResponseError = ParseResponseError>);

impl<RenderRequestError, ParseResponseOutput, ParseResponseError> fmt::Debug
    for dyn Endpoint<
        RenderRequestError = RenderRequestError,
        ParseResponseOutput = ParseResponseOutput,
        ParseResponseError = ParseResponseError,
    >
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Endpoint").finish()
    }
}

impl<RenderRequestError, ParseResponseOutput, ParseResponseError> fmt::Debug
    for dyn Endpoint<
            RenderRequestError = RenderRequestError,
            ParseResponseOutput = ParseResponseOutput,
            ParseResponseError = ParseResponseError,
        > + Send
        + Sync
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Endpoint").finish()
    }
}

//
//
//
#[cfg(feature = "dyn-clone")]
pub trait RetryableEndpoint: DynClone {
    type RetryReason: Send + Sync + Clone;

    type RenderRequestError: error::Error + Send + Sync + 'static;

    type ParseResponseOutput;
    type ParseResponseError: error::Error + Send + Sync + '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 max_retry_count(&self) -> usize {
        3
    }

    fn next_retry_in(&self, retry: &RetryableEndpointRetry<Self::RetryReason>) -> Duration {
        match retry.count {
            0 | 1 | 2 => Duration::from_millis(500),
            _ => Duration::from_secs(1),
        }
    }
}

#[cfg(not(feature = "dyn-clone"))]
pub trait RetryableEndpoint {
    type RetryReason: Send + Sync + Clone;

    type RenderRequestError: error::Error + Send + Sync + 'static;

    type ParseResponseOutput;
    type ParseResponseError: error::Error + Send + Sync + '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 max_retry_count(&self) -> usize {
        3
    }

    fn next_retry_in(&self, retry: &RetryableEndpointRetry<Self::RetryReason>) -> Duration {
        match retry.count {
            0 | 1 | 2 => Duration::from_millis(500),
            _ => Duration::from_secs(1),
        }
    }
}

#[cfg(feature = "dyn-clone")]
clone_trait_object!(<RetryReason, RenderRequestError, ParseResponseOutput, ParseResponseError> RetryableEndpoint<RetryReason = RetryReason, RenderRequestError = RenderRequestError, ParseResponseOutput = ParseResponseOutput, ParseResponseError = ParseResponseError>);

impl<RetryReason, RenderRequestError, ParseResponseOutput, ParseResponseError> fmt::Debug
    for dyn RetryableEndpoint<
        RetryReason = RetryReason,
        RenderRequestError = RenderRequestError,
        ParseResponseOutput = ParseResponseOutput,
        ParseResponseError = ParseResponseError,
    >
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RetryableEndpoint").finish()
    }
}

impl<RetryReason, RenderRequestError, ParseResponseOutput, ParseResponseError> fmt::Debug
    for dyn RetryableEndpoint<
            RetryReason = RetryReason,
            RenderRequestError = RenderRequestError,
            ParseResponseOutput = ParseResponseOutput,
            ParseResponseError = ParseResponseError,
        > + Send
        + Sync
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RetryableEndpoint").finish()
    }
}

//
pub struct RetryableEndpointRetry<T> {
    pub count: usize,
    pub reason: T,
}

impl<T> fmt::Debug for RetryableEndpointRetry<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RetryableEndpointRetry")
            .field("count", &self.count)
            .field("reason", &self.reason)
            .finish()
    }
}

impl<T> RetryableEndpointRetry<T> {
    pub fn new(count: usize, reason: T) -> Self {
        Self { count, reason }
    }
}