1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

allow to send request using custom connector

This commit is contained in:
Nikolay Kim 2018-02-19 13:41:21 -08:00
parent 548f4e4d62
commit f2f1798215
3 changed files with 20 additions and 3 deletions

View File

@ -14,10 +14,14 @@
* Added StaticFiles::index_file() * Added StaticFiles::index_file()
* Added http client
* Added basic websocket client * Added basic websocket client
* Added TestServer::ws(), test websockets client * Added TestServer::ws(), test websockets client
* Added TestServer test http client
* Allow to override content encoding on application level * Allow to override content encoding on application level

View File

@ -43,14 +43,21 @@ enum State {
pub struct SendRequest { pub struct SendRequest {
req: ClientRequest, req: ClientRequest,
state: State, state: State,
conn: Addr<Unsync, ClientConnector>,
} }
impl SendRequest { impl SendRequest {
pub(crate) fn new(req: ClientRequest) -> SendRequest { pub(crate) fn new(req: ClientRequest) -> SendRequest {
SendRequest::with_connector(req, ClientConnector::from_registry())
}
pub(crate) fn with_connector(req: ClientRequest, conn: Addr<Unsync, ClientConnector>)
-> SendRequest
{
SendRequest{ SendRequest{
req: req, req: req,
state: State::New, state: State::New,
} conn: conn}
} }
} }
@ -64,8 +71,7 @@ impl Future for SendRequest {
match state { match state {
State::New => State::New =>
self.state = State::Connect( self.state = State::Connect(self.conn.send(Connect(self.req.uri().clone()))),
ClientConnector::from_registry().send(Connect(self.req.uri().clone()))),
State::Connect(mut conn) => match conn.poll() { State::Connect(mut conn) => match conn.poll() {
Ok(Async::NotReady) => { Ok(Async::NotReady) => {
self.state = State::Connect(conn); self.state = State::Connect(conn);

View File

@ -1,6 +1,7 @@
use std::{fmt, mem}; use std::{fmt, mem};
use std::io::Write; use std::io::Write;
use actix::{Addr, Unsync};
use cookie::{Cookie, CookieJar}; use cookie::{Cookie, CookieJar};
use bytes::{BytesMut, BufMut}; use bytes::{BytesMut, BufMut};
use http::{HeaderMap, Method, Version, Uri, HttpTryFrom, Error as HttpError}; use http::{HeaderMap, Method, Version, Uri, HttpTryFrom, Error as HttpError};
@ -12,6 +13,7 @@ use body::Body;
use error::Error; use error::Error;
use headers::ContentEncoding; use headers::ContentEncoding;
use super::pipeline::SendRequest; use super::pipeline::SendRequest;
use super::connector::ClientConnector;
/// An HTTP Client Request /// An HTTP Client Request
pub struct ClientRequest { pub struct ClientRequest {
@ -176,6 +178,11 @@ impl ClientRequest {
pub fn send(self) -> SendRequest { pub fn send(self) -> SendRequest {
SendRequest::new(self) SendRequest::new(self)
} }
pub fn with_connector(self, conn: Addr<Unsync, ClientConnector>) -> SendRequest {
SendRequest::with_connector(self, conn)
}
} }
impl fmt::Debug for ClientRequest { impl fmt::Debug for ClientRequest {