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

Hide reqwest::Body type from the Service implementation #3

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ missing_errors_doc = "allow"

[workspace.lints.rustdoc]
broken_intra_doc_links = "deny"

[patch.crates-io]
reqwest = { git = "https://github.com/alekseysidorov/reqwest.git", rev = "be4da6d9106f895052575567b08c1f024696645e" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# tower-http-client

WIP
WIP
2 changes: 1 addition & 1 deletion tower-reqwest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ util = []
[dependencies]
futures-util = "0.3"
http = "1.0"
http-body-util = "0.1"
hyper = { version = "1.0" }
include-utils = "0.2"
log = "0.4"
Expand All @@ -30,7 +31,6 @@ tower-http = { version = "0.5", features = ["set-header", "util", "request-id"]

[dev-dependencies]
anyhow = "1.0"
http-body-util = "0.1"
pretty_assertions = "1.4.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
26 changes: 18 additions & 8 deletions tower-reqwest/src/adapters/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@

use std::{future::Future, task::Poll};

use http_body_util::BodyExt;
use hyper::body::Bytes;
use pin_project::pin_project;
use tower::Service;

use crate::{HttpBody, HttpClientService, HttpResponse};
use crate::HttpClientService;

type BoxError = Box<dyn std::error::Error + Send + Sync>;

impl<S, ReqBody> Service<http::Request<ReqBody>> for HttpClientService<S>
where
// Service
S: Service<reqwest::Request>,
S::Future: Send + 'static,
S::Error: 'static,
// Request
ReqBody: hyper::body::Body<Data = Bytes> + Send + Sync + 'static,
ReqBody::Error: std::error::Error + Send + Sync,
// Response
http::Response<reqwest::Body>: From<S::Response>,
crate::Error: From<S::Error>,
HttpResponse: From<S::Response>,
HttpBody: From<ReqBody>,
{
type Response = HttpResponse;
type Response = http::Response<reqwest::Body>;
type Error = crate::Error;
type Future = ExecuteRequestFuture<S>;

Expand All @@ -30,13 +38,15 @@ where
}

fn call(&mut self, req: http::Request<ReqBody>) -> Self::Future {
let req = req.map(|body| body.map_err(BoxError::from).boxed());

let future = reqwest::Request::try_from(req).map(|reqw| self.0.call(reqw));
ExecuteRequestFuture::new(future)
}
}

#[pin_project]
/// Future that resolves to the response or failure to connect.
#[pin_project]
#[derive(Debug)]
pub struct ExecuteRequestFuture<S>
where
Expand Down Expand Up @@ -77,9 +87,9 @@ impl<S> Future for ExecuteRequestFuture<S>
where
S: Service<reqwest::Request>,
crate::Error: From<S::Error>,
HttpResponse: From<S::Response>,
http::Response<reqwest::Body>: From<S::Response>,
{
type Output = crate::Result<HttpResponse>;
type Output = crate::Result<http::Response<reqwest::Body>>;

fn poll(
self: std::pin::Pin<&mut Self>,
Expand Down Expand Up @@ -163,7 +173,7 @@ mod tests {
.method(http::Method::GET)
.uri(format!("{mock_uri}/hello"))
// TODO Make in easy to create requests without body.
.body("")?;
.body(http_body_util::Empty::new())?;

let response = ServiceBuilder::new()
.layer(HttpClientLayer)
Expand Down
2 changes: 1 addition & 1 deletion tower-reqwest/src/adapters/reqwest_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ mod tests {
.method(http::Method::GET)
.uri(format!("{mock_uri}/hello"))
// TODO Make in easy to create requests without body.
.body("")?,
.body(http_body_util::Empty::new())?,
)
.await?;

Expand Down
Loading