2019-03-25 21:58:01 -07:00
|
|
|
use actix_http::body::Body;
|
2019-03-26 11:41:38 -07:00
|
|
|
use actix_http::client::{ConnectError, Connection, SendRequestError};
|
2019-03-25 21:58:01 -07:00
|
|
|
use actix_http::{http, RequestHead};
|
|
|
|
use actix_service::Service;
|
|
|
|
use futures::Future;
|
|
|
|
|
2019-03-26 11:41:38 -07:00
|
|
|
use crate::response::ClientResponse;
|
|
|
|
|
2019-03-25 21:58:01 -07:00
|
|
|
pub(crate) struct ConnectorWrapper<T>(pub T);
|
|
|
|
|
|
|
|
pub(crate) trait Connect {
|
|
|
|
fn send_request(
|
|
|
|
&mut self,
|
|
|
|
head: RequestHead,
|
|
|
|
body: Body,
|
|
|
|
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Connect for ConnectorWrapper<T>
|
|
|
|
where
|
|
|
|
T: Service<Request = http::Uri, Error = ConnectError>,
|
|
|
|
T::Response: Connection,
|
|
|
|
<T::Response as Connection>::Future: 'static,
|
|
|
|
T::Future: 'static,
|
|
|
|
{
|
|
|
|
fn send_request(
|
|
|
|
&mut self,
|
|
|
|
head: RequestHead,
|
|
|
|
body: Body,
|
|
|
|
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>> {
|
|
|
|
Box::new(
|
|
|
|
self.0
|
|
|
|
// connect to the host
|
|
|
|
.call(head.uri.clone())
|
|
|
|
.from_err()
|
|
|
|
// send request
|
2019-03-26 11:41:38 -07:00
|
|
|
.and_then(move |connection| connection.send_request(head, body))
|
|
|
|
.map(|(head, payload)| ClientResponse::new(head, payload)),
|
2019-03-25 21:58:01 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|