From 291254fa73f4d953717d14cb567ab0593ff935f8 Mon Sep 17 00:00:00 2001 From: David Barsky <me@davidbarsky.com> Date: Tue, 15 Jan 2019 20:53:55 -0500 Subject: [PATCH] feat(client): Implement Tower's Service for Client --- Cargo.toml | 1 + src/client/mod.rs | 22 ++++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 24 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index c56f79e9b3..f16399f07b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ tokio-reactor = { version = "0.1", optional = true } tokio-tcp = { version = "0.1", optional = true } tokio-threadpool = { version = "0.1.3", optional = true } tokio-timer = { version = "0.2", optional = true } +tower-service = "0.2" want = "0.0.6" [dev-dependencies] diff --git a/src/client/mod.rs b/src/client/mod.rs index 7ae870b84e..df23aa5bcd 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -88,6 +88,7 @@ use futures::sync::oneshot; use http::{Method, Request, Response, Uri, Version}; use http::header::{HeaderValue, HOST}; use http::uri::Scheme; +use tower_service::Service; use body::{Body, Payload}; use common::{lazy as hyper_lazy, Lazy}; @@ -594,6 +595,27 @@ impl<C, B> fmt::Debug for Client<C, B> { } } +impl<C, B> Service<Request<B>> for Client<C, B> +where + C: Connect + Sync + 'static, + C::Transport: 'static, + C::Future: 'static, + B: Payload + Send + 'static, + B::Data: Send, +{ + type Response = Response<Body>; + type Error = ::Error; + type Future = ResponseFuture; + + fn poll_ready(&mut self) -> Poll<(), Self::Error> { + Ok(Async::Ready(())) + } + + fn call(&mut self, req: Request<B>) -> Self::Future { + self.request(req) + } +} + /// A `Future` that will resolve to an HTTP Response. /// /// This is returned by `Client::request` (and `Client::get`). diff --git a/src/lib.rs b/src/lib.rs index 04bb08e93b..b070bca82d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,7 @@ extern crate time; #[cfg(feature = "runtime")] extern crate tokio_tcp; #[cfg(feature = "runtime")] extern crate tokio_threadpool; #[cfg(feature = "runtime")] extern crate tokio_timer; +extern crate tower_service; extern crate want; #[cfg(all(test, feature = "nightly"))]