From 5fe7326fa02a35409423172a7dfa63c0470f9cb0 Mon Sep 17 00:00:00 2001 From: tu6ge <772364230@qq.com> Date: Sun, 18 Dec 2022 14:04:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(builder):=20=E6=9B=B4=E6=94=B9=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E5=8F=AF=E8=A7=81=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pub 改为 pub(crate) --- examples/custom.rs | 2 +- src/blocking/builder.rs | 12 +++++++----- src/builder.rs | 14 ++++++++------ src/client.rs | 28 ++++++++++++++++++++++++---- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/examples/custom.rs b/examples/custom.rs index 18c7e59..0492661 100644 --- a/examples/custom.rs +++ b/examples/custom.rs @@ -2,7 +2,7 @@ use std::env; use aliyun_oss_client::{ builder::BuilderError, - traits::{RefineObject, RefineObjectList}, + decode::{RefineObject, RefineObjectList}, Client, }; use dotenv::dotenv; diff --git a/src/blocking/builder.rs b/src/blocking/builder.rs index f231985..e3d3443 100644 --- a/src/blocking/builder.rs +++ b/src/blocking/builder.rs @@ -43,7 +43,8 @@ pub struct RequestBuilder { } impl RequestBuilder { - pub fn header(self, key: K, value: V) -> Self + #[allow(dead_code)] + pub(crate) fn header(self, key: K, value: V) -> Self where HeaderName: TryFrom, >::Error: Into, @@ -56,28 +57,29 @@ impl RequestBuilder { } } - pub fn headers(self, headers: HeaderMap) -> Self { + pub(crate) fn headers(self, headers: HeaderMap) -> Self { RequestBuilder { inner: self.inner.headers(headers), ..self } } - pub fn body>(self, body: T) -> Self { + pub(crate) fn body>(self, body: T) -> Self { RequestBuilder { inner: self.inner.body(body), ..self } } - pub fn timeout(self, timeout: Duration) -> Self { + pub(crate) fn timeout(self, timeout: Duration) -> Self { RequestBuilder { inner: self.inner.timeout(timeout), ..self } } - pub fn build(self) -> reqwest::Result { + #[allow(dead_code)] + pub(crate) fn build(self) -> reqwest::Result { self.inner.build() } diff --git a/src/builder.rs b/src/builder.rs index a5e716f..e6f67f4 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -76,7 +76,8 @@ pub struct RequestBuilder { } impl RequestBuilder { - pub fn header(self, key: K, value: V) -> Self + #[allow(dead_code)] + pub(crate) fn header(self, key: K, value: V) -> Self where HeaderName: TryFrom, >::Error: Into, @@ -89,28 +90,29 @@ impl RequestBuilder { } } - pub fn headers(self, headers: HeaderMap) -> Self { + pub(crate) fn headers(self, headers: HeaderMap) -> Self { RequestBuilder { inner: self.inner.headers(headers), ..self } } - pub fn body>(self, body: T) -> Self { + pub(crate) fn body>(self, body: T) -> Self { RequestBuilder { inner: self.inner.body(body), ..self } } - pub fn timeout(self, timeout: Duration) -> Self { + pub(crate) fn timeout(self, timeout: Duration) -> Self { RequestBuilder { inner: self.inner.timeout(timeout), ..self } } - pub fn build(self) -> reqwest::Result { + #[allow(dead_code)] + pub(crate) fn build(self) -> reqwest::Result { self.inner.build() } @@ -152,7 +154,7 @@ pub enum BuilderError { } #[async_trait] -pub trait RequestHandler { +pub(crate) trait RequestHandler { async fn handle_error(self) -> Result; } diff --git a/src/client.rs b/src/client.rs index 28e2f61..ce57bde 100644 --- a/src/client.rs +++ b/src/client.rs @@ -17,6 +17,7 @@ use std::env; use std::rc::Rc; #[cfg(test)] use std::sync::Arc; +use std::time::Duration; /// # 构造请求的客户端结构体 /// Clone 特征不是必须的 @@ -30,6 +31,7 @@ where client_middleware: M, endpoint: EndPoint, bucket: BucketName, + timeout: Option, } impl Client { @@ -95,6 +97,7 @@ impl Client { client_middleware: M::default(), endpoint, bucket, + timeout: None, } } @@ -117,6 +120,11 @@ impl Client { pub fn get_endpoint_url(&self) -> Url { self.endpoint.to_url() } + + /// 设置 timeout + pub fn timeout(&mut self, timeout: Duration) { + self.timeout = Some(timeout); + } } #[cfg(not(test))] @@ -210,10 +218,16 @@ impl AlignBuilder for Client { auth_builder.canonicalized_resource(resource); auth_builder.extend_headers(HeaderMap::from_iter(headers)); - Ok(self + let mut builder = self .client_middleware .request(method, url) - .headers(auth_builder.get_headers()?)) + .headers(auth_builder.get_headers()?); + + if let Some(timeout) = self.timeout { + builder = builder.timeout(timeout); + }; + + Ok(builder) } } @@ -264,9 +278,15 @@ impl crate::file::blocking::AlignBuilder for Client