Skip to content

Commit

Permalink
feat(builder): 更改方法的可见性
Browse files Browse the repository at this point in the history
pub 改为 pub(crate)
  • Loading branch information
tu6ge committed Dec 18, 2022
1 parent a9b3a8d commit 5fe7326
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::env;

use aliyun_oss_client::{
builder::BuilderError,
traits::{RefineObject, RefineObjectList},
decode::{RefineObject, RefineObjectList},
Client,
};
use dotenv::dotenv;
Expand Down
12 changes: 7 additions & 5 deletions src/blocking/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub struct RequestBuilder {
}

impl RequestBuilder {
pub fn header<K, V>(self, key: K, value: V) -> Self
#[allow(dead_code)]
pub(crate) fn header<K, V>(self, key: K, value: V) -> Self
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<http::Error>,
Expand All @@ -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<T: Into<Body>>(self, body: T) -> Self {
pub(crate) fn body<T: Into<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<Request> {
#[allow(dead_code)]
pub(crate) fn build(self) -> reqwest::Result<Request> {
self.inner.build()
}

Expand Down
14 changes: 8 additions & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub struct RequestBuilder {
}

impl RequestBuilder {
pub fn header<K, V>(self, key: K, value: V) -> Self
#[allow(dead_code)]
pub(crate) fn header<K, V>(self, key: K, value: V) -> Self
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<http::Error>,
Expand All @@ -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<T: Into<Body>>(self, body: T) -> Self {
pub(crate) fn body<T: Into<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<Request> {
#[allow(dead_code)]
pub(crate) fn build(self) -> reqwest::Result<Request> {
self.inner.build()
}

Expand Down Expand Up @@ -152,7 +154,7 @@ pub enum BuilderError {
}

#[async_trait]
pub trait RequestHandler {
pub(crate) trait RequestHandler {
async fn handle_error(self) -> Result<Response, BuilderError>;
}

Expand Down
28 changes: 24 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::env;
use std::rc::Rc;
#[cfg(test)]
use std::sync::Arc;
use std::time::Duration;

/// # 构造请求的客户端结构体
/// Clone 特征不是必须的
Expand All @@ -30,6 +31,7 @@ where
client_middleware: M,
endpoint: EndPoint,
bucket: BucketName,
timeout: Option<Duration>,
}

impl<M: Default + Clone> Client<M> {
Expand Down Expand Up @@ -95,6 +97,7 @@ impl<M: Default + Clone> Client<M> {
client_middleware: M::default(),
endpoint,
bucket,
timeout: None,
}
}

Expand All @@ -117,6 +120,11 @@ impl<M: Default + Clone> Client<M> {
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))]
Expand Down Expand Up @@ -210,10 +218,16 @@ impl AlignBuilder for Client<ClientWithMiddleware> {
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)
}
}

Expand Down Expand Up @@ -264,9 +278,15 @@ impl crate::file::blocking::AlignBuilder for Client<BlockingClientWithMiddleware
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)
}
}

0 comments on commit 5fe7326

Please sign in to comment.