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

Test Body interop #2

Closed
wants to merge 5 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
49 changes: 9 additions & 40 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,55 +1,24 @@
[package]
name = "tower-reqwest"
description = "Adapter between reqwest and tower-http crates."
version = "0.1.2"
[workspace]
resolver = "2"
members = ["tower-http-client", "tower-http-client-core", "tower-reqwest"]

[workspace.package]
version = "0.2.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/alekseysidorov/tower-reqwest"
documentation = "https://docs.rs/crate/tower-reqwest"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["util", "reqwest-middleware"]
reqwest-middleware = ["dep:reqwest-middleware"]
util = []

[dependencies]
futures-util = "0.3"
http = "1.0"
hyper = { version = "1.0" }
include-utils = "0.2"
log = "0.4"
pin-project = "1.0"
reqwest = { version = "0.12", features = ["stream"] }
reqwest-middleware = { version = "0.3.0", optional = true }
thiserror = "1.0"
tower = { version = "0.4" }
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"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] }
wiremock = "0.6.0"

[lints.rust]
[workspace.lints.rust]
missing_docs = "warn"
missing_debug_implementations = "warn"
unsafe_code = "forbid"

[lints.clippy]
[workspace.lints.clippy]
pedantic = "warn"
module_name_repetitions = "allow"
missing_panics_doc = "warn"
missing_errors_doc = "allow"

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

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
58 changes: 2 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,3 @@
# tower-reqwest
# tower-http-client

[![tests](https://github.com/alekseysidorov/tower-reqwest/actions/workflows/ci.yml/badge.svg)](https://github.com/alekseysidorov/tower-reqwest/actions/workflows/ci.yml)
[![crates.io](https://img.shields.io/crates/v/tower-reqwest.svg)](https://crates.io/crates/tower-reqwest)
[![Documentation](https://docs.rs/tower-reqwest/badge.svg)](https://docs.rs/tower-reqwest)
[![MIT/Apache-2 licensed](https://img.shields.io/crates/l/tower-reqwest)](./LICENSE)

<!-- ANCHOR: description -->

This library provides adapters to use [reqwest] client with the [tower_http]
layers.

## Warning

This crate is currently in early stage of development and is not ready for
production use.

## Example

```rust
use http::{header::USER_AGENT, HeaderValue};
use http_body_util::BodyExt;
use serde_json::Value;
use tower::ServiceBuilder;
use tower_http::ServiceBuilderExt;
use tower_reqwest::{util::HttpClientExt, HttpClientLayer};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = ServiceBuilder::new()
// Add some layers.
.override_request_header(USER_AGENT, HeaderValue::from_static("tower-reqwest"))
// Make client compatible with the `tower-http` layers.
.layer(HttpClientLayer)
.service(reqwest::Client::new());
// Execute request by using this service.
let response = client
.execute(
http::request::Builder::new()
.method(http::Method::GET)
.uri("http://ip.jsontest.com")
.body("")?,
)
.await?;

let bytes = response.into_body().collect().await?.to_bytes();
let value: Value = serde_json::from_slice(&bytes)?;
println!("{value:#?}");

Ok(())
}
```

<!-- ANCHOR_END: description -->

[reqwest]: https://github.com/seanmonstar/reqwest
[tower_http]: https://github.com/tower-rs/tower-http
WIP
8 changes: 8 additions & 0 deletions tower-http-client-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "tower-http-client-core"
version.workspace = true
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
14 changes: 14 additions & 0 deletions tower-http-client-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
8 changes: 8 additions & 0 deletions tower-http-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "tower-http-client"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
14 changes: 14 additions & 0 deletions tower-http-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
45 changes: 45 additions & 0 deletions tower-reqwest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "tower-reqwest"
description = "Adapter between reqwest and tower-http crates."

version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
documentation.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["util", "reqwest-middleware"]
reqwest-middleware = ["dep:reqwest-middleware"]
util = []

[dependencies]
futures-util = "0.3"
http = "1.0"
hyper = { version = "1.0" }
include-utils = "0.2"
log = "0.4"
pin-project = "1.0"
reqwest = { version = "0.12", features = ["stream"] }
reqwest-middleware = { version = "0.3.0", optional = true }
thiserror = "1.0"
tower = { version = "0.4" }
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"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] }
wiremock = "0.6.0"

[lints]
workspace = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
57 changes: 57 additions & 0 deletions tower-reqwest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# tower-reqwest

[![tests](https://github.com/alekseysidorov/tower-reqwest/actions/workflows/ci.yml/badge.svg)](https://github.com/alekseysidorov/tower-reqwest/actions/workflows/ci.yml)
[![crates.io](https://img.shields.io/crates/v/tower-reqwest.svg)](https://crates.io/crates/tower-reqwest)
[![Documentation](https://docs.rs/tower-reqwest/badge.svg)](https://docs.rs/tower-reqwest)
[![MIT/Apache-2 licensed](https://img.shields.io/crates/l/tower-reqwest)](./LICENSE)

<!-- ANCHOR: description -->

This library provides adapters to use [reqwest] client with the [tower_http]
layers.

## Warning

This crate is currently in early stage of development and is not ready for
production use.

## Example

```rust
use http::{header::USER_AGENT, HeaderValue};
use http_body_util::BodyExt;
use serde_json::Value;
use tower::ServiceBuilder;
use tower_http::ServiceBuilderExt;
use tower_reqwest::{util::HttpClientExt, HttpClientLayer};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = ServiceBuilder::new()
// Add some layers.
.override_request_header(USER_AGENT, HeaderValue::from_static("tower-reqwest"))
// Make client compatible with the `tower-http` layers.
.layer(HttpClientLayer)
.service(reqwest::Client::new());
// Execute request by using this service.
let response = client
.execute(
http::request::Builder::new()
.method(http::Method::GET)
.uri("http://ip.jsontest.com")
.body("")?,
)
.await?;

let bytes = response.into_body().collect().await?.to_bytes();
let value: Value = serde_json::from_slice(&bytes)?;
println!("{value:#?}");

Ok(())
}
```

<!-- ANCHOR_END: description -->

[reqwest]: https://github.com/seanmonstar/reqwest
[tower_http]: https://github.com/tower-rs/tower-http
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ where
) -> std::task::Poll<Self::Output> {
let this = self.project();
match this.inner.project() {
InnerProj::Future { fut } => fut
.poll(cx)
.map_ok(HttpResponse::from)
.map_err(crate::Error::from),
InnerProj::Future { fut } => {
fut.poll(cx).map_ok(From::from).map_err(crate::Error::from)
}
InnerProj::Error { error } => {
let error = error.take().expect("Polled after ready");
Poll::Ready(Err(error))
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/util.rs → tower-reqwest/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub trait HttpClientExt: Clone {
fn execute<B>(
&self,
request: http::Request<B>,
) -> impl Future<Output = crate::Result<HttpResponse>>
) -> impl Future<Output = crate::Result<http::Response<HttpBody>>>
where
B: Into<HttpBody>;
}
Expand All @@ -29,7 +29,7 @@ where
fn execute<B>(
&self,
request: http::Request<B>,
) -> impl Future<Output = crate::Result<HttpResponse>>
) -> impl Future<Output = crate::Result<http::Response<HttpBody>>>
where
B: Into<HttpBody>,
{
Expand Down
Loading