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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::error::Error as StdError;
use std::fmt;

use super::IoError;
use bytes::Bytes;
use futures::future::BoxFuture;
use http::{Request, Response};

#[cfg(feature = "reqwest")]
pub use reqwest_dispatch_http_request::ReqwestDispatchHttpRequest;

use super::BytesStream;

pub type ResponseFuture<'a> = BoxFuture<'a, Result<Response<BytesStream>, RemoteCallError>>;

/// A common trait for dispatching Http requests.
///
/// This trait is used to enable pluggable
/// HTTP clients
pub trait DispatchHttpRequest {
    fn dispatch<'a>(&'a self, req: Request<Bytes>) -> ResponseFuture<'a>;
}

/// An error with can be caused by a remote call.
///
/// This is a low level error.
#[derive(Debug)]
pub struct RemoteCallError {
    message: Option<String>,
    cause: Option<Box<dyn StdError + Send + Sync + 'static>>,
    detail: RemoteCallErrorDetail,
}

impl RemoteCallError {
    pub fn new_io() -> Self {
        Self {
            message: None,
            cause: None,
            detail: RemoteCallErrorDetail::Io,
        }
    }

    pub fn new_other() -> Self {
        Self {
            message: None,
            cause: None,
            detail: RemoteCallErrorDetail::Other,
        }
    }

    pub fn with_message<M: Into<String>>(mut self, message: M) -> Self {
        self.message = Some(message.into());
        self
    }

    pub fn with_cause<E: StdError + Send + Sync + 'static>(mut self, cause: E) -> Self {
        self.cause = Some(Box::new(cause));
        self
    }

    pub fn is_io(&self) -> bool {
        self.detail.is_io()
    }

    pub fn is_other(&self) -> bool {
        self.detail.is_other()
    }

    pub fn message(&self) -> Option<&str> {
        self.message.as_deref()
    }
}

pub type RemoteCallResult<T> = Result<T, RemoteCallError>;

impl fmt::Display for RemoteCallError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.detail {
            RemoteCallErrorDetail::Io => {
                write!(f, "io error")?;
            }
            RemoteCallErrorDetail::Other => {
                write!(f, "other error")?;
            }
        }

        if let Some(ref message) = self.message {
            write!(f, " - message: {}", message)?;
        }

        Ok(())
    }
}

impl StdError for RemoteCallError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        self.cause.as_ref().map(|e| &**e as &dyn StdError)
    }
}

impl From<IoError> for RemoteCallError {
    fn from(err: IoError) -> Self {
        Self::new_io().with_message(err.into_string())
    }
}

impl From<http::header::InvalidHeaderValue> for RemoteCallError {
    fn from(err: http::header::InvalidHeaderValue) -> Self {
        RemoteCallError::new_other()
            .with_message("invalid header value")
            .with_cause(err)
    }
}

impl From<http::uri::InvalidUri> for RemoteCallError {
    fn from(err: http::uri::InvalidUri) -> Self {
        RemoteCallError::new_other()
            .with_message("invalid URI")
            .with_cause(err)
    }
}

impl From<tokio::time::Elapsed> for RemoteCallError {
    fn from(err: tokio::time::Elapsed) -> Self {
        RemoteCallError::new_io()
            .with_message("a timeout occurred")
            .with_cause(err)
    }
}

#[derive(Debug)]
enum RemoteCallErrorDetail {
    Other,
    Io,
}

impl RemoteCallErrorDetail {
    pub fn is_io(&self) -> bool {
        match self {
            RemoteCallErrorDetail::Io => true,
            _ => false,
        }
    }

    pub fn is_other(&self) -> bool {
        match self {
            RemoteCallErrorDetail::Other => true,
            _ => false,
        }
    }
}

#[cfg(feature = "reqwest")]
mod reqwest_dispatch_http_request {
    use futures::{stream::TryStreamExt, FutureExt, StreamExt};
    use http::{Request, Response};
    use reqwest::{Client, Request as RRequest};

    use super::*;

    #[derive(Clone)]
    pub struct ReqwestDispatchHttpRequest {
        client: Client,
    }

    impl DispatchHttpRequest for ReqwestDispatchHttpRequest {
        fn dispatch(&self, req: Request<Bytes>) -> ResponseFuture {
            async move {
                let (parts, body) = req.into_parts();

                let url = parts.uri.to_string().parse().map_err(|err| {
                    RemoteCallError::new_other()
                        .with_message("invalid url")
                        .with_cause(err)
                })?;

                let mut request = RRequest::new(parts.method, url);

                for (k, v) in parts.headers {
                    if let Some(k) = k {
                        request.headers_mut().append(k, v);
                    }
                }

                *request.body_mut() = Some(body.into());

                let reqwest_response = self.client.execute(request).await?;

                let status = reqwest_response.status();
                let headers = reqwest_response.headers().clone();
                let version = reqwest_response.version();

                let bytes_stream = reqwest_response
                    .bytes_stream()
                    .map_err(|err| IoError::new(err.to_string()))
                    .boxed();
                let mut response = Response::new(bytes_stream);

                *response.status_mut() = status;
                *response.headers_mut() = headers;
                *response.version_mut() = version;

                Ok(response)
            }
            .boxed()
        }
    }

    impl Default for ReqwestDispatchHttpRequest {
        fn default() -> Self {
            let client = Client::new();
            Self { client }
        }
    }

    impl From<reqwest::Error> for RemoteCallError {
        fn from(err: reqwest::Error) -> Self {
            if err.is_timeout() {
                return RemoteCallError::new_io()
                    .with_message("Request timeout")
                    .with_cause(err);
            }

            RemoteCallError::new_other()
                .with_message(err.to_string())
                .with_cause(err)
        }
    }
}