1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 01:52:57 +01:00

move with_connector method to ClientRequestBuilder

This commit is contained in:
Nikolay Kim 2018-02-27 15:14:33 -08:00
parent a7bf635158
commit 1f063e4136
2 changed files with 37 additions and 16 deletions

View File

@ -27,6 +27,14 @@ pub struct ClientRequest {
encoding: ContentEncoding, encoding: ContentEncoding,
response_decompress: bool, response_decompress: bool,
buffer_capacity: Option<(usize, usize)>, buffer_capacity: Option<(usize, usize)>,
conn: ConnectionType,
}
enum ConnectionType {
Default,
Connector(Addr<Unsync, ClientConnector>),
Connection(Connection),
} }
impl Default for ClientRequest { impl Default for ClientRequest {
@ -43,6 +51,7 @@ impl Default for ClientRequest {
encoding: ContentEncoding::Auto, encoding: ContentEncoding::Auto,
response_decompress: true, response_decompress: true,
buffer_capacity: None, buffer_capacity: None,
conn: ConnectionType::Default,
} }
} }
} }
@ -190,18 +199,14 @@ impl ClientRequest {
} }
/// Send request /// Send request
pub fn send(self) -> SendRequest { ///
SendRequest::new(self) /// This method returns future that resolves to a ClientResponse
pub fn send(mut self) -> SendRequest {
match mem::replace(&mut self.conn, ConnectionType::Default) {
ConnectionType::Default => SendRequest::new(self),
ConnectionType::Connector(conn) => SendRequest::with_connector(self, conn),
ConnectionType::Connection(conn) => SendRequest::with_connection(self, conn),
} }
/// Send request using custom connector
pub fn with_connector(self, conn: Addr<Unsync, ClientConnector>) -> SendRequest {
SendRequest::with_connector(self, conn)
}
/// Send request using existing Connection
pub fn with_connection(self, conn: Connection) -> SendRequest {
SendRequest::with_connection(self, conn)
} }
} }
@ -451,6 +456,22 @@ impl ClientRequestBuilder {
self self
} }
/// Send request using custom connector
pub fn with_connector(&mut self, conn: Addr<Unsync, ClientConnector>) -> &mut Self {
if let Some(parts) = parts(&mut self.request, &self.err) {
parts.conn = ConnectionType::Connector(conn);
}
self
}
/// Send request using existing Connection
pub fn with_connection(&mut self, conn: Connection) -> &mut Self {
if let Some(parts) = parts(&mut self.request, &self.err) {
parts.conn = ConnectionType::Connection(conn);
}
self
}
/// This method calls provided closure with builder reference if value is true. /// This method calls provided closure with builder reference if value is true.
pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Self pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Self
where F: FnOnce(&mut ClientRequestBuilder) where F: FnOnce(&mut ClientRequestBuilder)

View File

@ -194,6 +194,7 @@ impl WsClient {
self.request.set_header(header::UPGRADE, "websocket"); self.request.set_header(header::UPGRADE, "websocket");
self.request.set_header(header::CONNECTION, "upgrade"); self.request.set_header(header::CONNECTION, "upgrade");
self.request.set_header("SEC-WEBSOCKET-VERSION", "13"); self.request.set_header("SEC-WEBSOCKET-VERSION", "13");
self.request.with_connector(self.conn.clone());
if let Some(protocols) = self.protocols.take() { if let Some(protocols) = self.protocols.take() {
self.request.set_header("SEC-WEBSOCKET-PROTOCOL", protocols.as_str()); self.request.set_header("SEC-WEBSOCKET-PROTOCOL", protocols.as_str());
@ -215,7 +216,7 @@ impl WsClient {
} }
// start handshake // start handshake
WsClientHandshake::new(request, &self.conn, self.max_size) WsClientHandshake::new(request, self.max_size)
} }
} }
} }
@ -235,8 +236,7 @@ pub struct WsClientHandshake {
} }
impl WsClientHandshake { impl WsClientHandshake {
fn new(mut request: ClientRequest, fn new(mut request: ClientRequest, max_size: usize) -> WsClientHandshake
conn: &Addr<Unsync, ClientConnector>, max_size: usize) -> WsClientHandshake
{ {
// Generate a random key for the `Sec-WebSocket-Key` header. // Generate a random key for the `Sec-WebSocket-Key` header.
// a base64-encoded (see Section 4 of [RFC4648]) value that, // a base64-encoded (see Section 4 of [RFC4648]) value that,
@ -256,7 +256,7 @@ impl WsClientHandshake {
WsClientHandshake { WsClientHandshake {
key, key,
max_size, max_size,
request: Some(request.with_connector(conn.clone())), request: Some(request.send()),
tx: Some(tx), tx: Some(tx),
error: None, error: None,
} }