Skip to content

Commit d5352de

Browse files
authored
feat(volo-http): add layer for RequestBuilder (#633)
* feat(volo-http): add `layer` for `RequestBuilder` Note that the layer should generate a `OneShotService` * chore(volo-http): bump volo-http to 0.5.1 --------- Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
1 parent 2ecf4d4 commit d5352de

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

volo-http/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "volo-http"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
edition.workspace = true
55
homepage.workspace = true
66
repository.workspace = true
@@ -138,4 +138,4 @@ native-tls-vendored = ["native-tls", "volo/native-tls-vendored"]
138138

139139
[package.metadata.docs.rs]
140140
all-features = true
141-
rustdoc-args = ["--cfg", "docsrs"]
141+
rustdoc-args = ["--cfg", "docsrs"]

volo-http/src/client/request_builder.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use http::{
1111
uri::{PathAndQuery, Scheme, Uri},
1212
version::Version,
1313
};
14+
use motore::layer::Layer;
1415
use volo::{
1516
client::{Apply, OneShotService, WithOptService},
1617
net::Address,
@@ -345,17 +346,27 @@ impl<S, B> RequestBuilder<S, B> {
345346
self.request.body()
346347
}
347348

348-
/// Apply a [`CallOpt`] to the request.
349-
pub fn with_callopt(self, callopt: CallOpt) -> RequestBuilder<WithOptService<S, CallOpt>, B> {
349+
/// Add a new [`Layer`] to the front of request builder.
350+
///
351+
/// Note that the [`Layer`] generated `Service` should be a [`OneShotService`].
352+
pub fn layer<L>(self, layer: L) -> RequestBuilder<L::Service, B>
353+
where
354+
L: Layer<S>,
355+
{
350356
RequestBuilder {
351-
inner: WithOptService::new(self.inner, callopt),
357+
inner: layer.layer(self.inner),
352358
target: self.target,
353359
version: self.version,
354360
request: self.request,
355361
status: self.status,
356362
}
357363
}
358364

365+
/// Apply a [`CallOpt`] to the request.
366+
pub fn with_callopt(self, callopt: CallOpt) -> RequestBuilder<WithOptService<S, CallOpt>, B> {
367+
self.layer(WithOptLayer::new(callopt))
368+
}
369+
359370
fn set_version(&mut self) {
360371
let ver = match self.version {
361372
Some(ver) => ver,
@@ -392,3 +403,21 @@ impl<S, B> RequestBuilder<S, B> {
392403
self.inner.call(&mut cx, self.request).await
393404
}
394405
}
406+
407+
struct WithOptLayer {
408+
opt: CallOpt,
409+
}
410+
411+
impl WithOptLayer {
412+
const fn new(opt: CallOpt) -> Self {
413+
Self { opt }
414+
}
415+
}
416+
417+
impl<S> Layer<S> for WithOptLayer {
418+
type Service = WithOptService<S, CallOpt>;
419+
420+
fn layer(self, inner: S) -> Self::Service {
421+
WithOptService::new(inner, self.opt)
422+
}
423+
}

0 commit comments

Comments
 (0)