1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-01-23 23:34:35 +01:00
actix-extras/awc/src/connect.rs

42 lines
1.2 KiB
Rust
Raw Normal View History

2019-03-25 21:58:01 -07:00
use actix_http::body::Body;
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;
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
.and_then(move |connection| connection.send_request(head, body))
.map(|(head, payload)| ClientResponse::new(head, payload)),
2019-03-25 21:58:01 -07:00
)
}
}