Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add impl tower_service::Service<http::Request<Body>> for Client and &'_ Client #2356

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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