Skip to content

Commit

Permalink
feat: Add impl Service<http::Request<Body>> for Client and `&'_ C…
Browse files Browse the repository at this point in the history
…lient`
  • Loading branch information
alekseysidorov committed Jul 18, 2024
1 parent c660535 commit 6be9fac
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

- Implement `danger_accept_invalid_hostnames` for `rustls`.
- Add `impl Service<http::Request<Body>>` for `Client` and `&'_ Client`.

## v0.12.5

Expand Down
56 changes: 56 additions & 0 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,62 @@ impl tower_service::Service<Request> for &'_ Client {
}
}

impl tower_service::Service<http::Request<crate::Body>> for Client {
type Response = http::Response<crate::Body>;
type Error = crate::Error;
type Future = MappedPending;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: http::Request<crate::Body>) -> Self::Future {
match req.try_into() {
Ok(req) => MappedPending::new(self.execute_request(req)),
Err(err) => MappedPending::new(Pending::new_err(err)),
}
}
}

impl tower_service::Service<http::Request<crate::Body>> for &'_ Client {
type Response = http::Response<crate::Body>;
type Error = crate::Error;
type Future = MappedPending;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: http::Request<crate::Body>) -> Self::Future {
match req.try_into() {
Ok(req) => MappedPending::new(self.execute_request(req)),
Err(err) => MappedPending::new(Pending::new_err(err)),
}
}
}

pin_project! {
pub struct MappedPending {
#[pin]
inner: Pending,
}
}

impl MappedPending {
fn new(inner: Pending) -> MappedPending {
Self { inner }
}
}

impl Future for MappedPending {
type Output = Result<http::Response<crate::Body>, crate::Error>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let inner = self.project().inner;
inner.poll(cx).map_ok(Into::into)
}
}

impl fmt::Debug for ClientBuilder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut builder = f.debug_struct("ClientBuilder");
Expand Down

0 comments on commit 6be9fac

Please sign in to comment.