You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2023/05/25 05:36:36 UTC

[incubator-opendal] branch main updated: refactor(core): Implement RFC-2299 for presign_read_with (#2314)

This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 6fb8db61 refactor(core): Implement RFC-2299 for presign_read_with (#2314)
6fb8db61 is described below

commit 6fb8db61ade6dde4cdf07e7a24d486a901a1dfee
Author: clundro <in...@outlook.com>
AuthorDate: Thu May 25 13:36:31 2023 +0800

    refactor(core): Implement RFC-2299 for presign_read_with (#2314)
    
    * support presign_read_with
    
    Signed-off-by: clundro <in...@outlook.com>
    
    * remove unused
    
    Signed-off-by: clundro <in...@outlook.com>
    
    * change closure (type,type)
    
    Signed-off-by: clundro <in...@outlook.com>
    
    ---------
    
    Signed-off-by: clundro <in...@outlook.com>
---
 core/src/types/operator/operator.rs         | 32 ++++++++++--------
 core/src/types/operator/operator_futures.rs | 52 +++++++++++++++++++++++++++++
 core/tests/behavior/write.rs                | 14 +++-----
 3 files changed, 75 insertions(+), 23 deletions(-)

diff --git a/core/src/types/operator/operator.rs b/core/src/types/operator/operator.rs
index 10676e8d..d39bfc91 100644
--- a/core/src/types/operator/operator.rs
+++ b/core/src/types/operator/operator.rs
@@ -1497,24 +1497,30 @@ impl Operator {
     ///
     /// #[tokio::main]
     /// async fn test(op: Operator) -> Result<()> {
-    ///     let args = OpRead::new()
-    ///         .with_override_content_disposition("attachment; filename=\"othertext.txt\"");
-    ///     let signed_req = op.presign_read_with("test.txt", args, Duration::from_secs(3600)).await?;
+    ///     let signed_req = op
+    ///         .presign_read_with("test.txt", Duration::from_secs(3600))
+    ///         .override_content_disposition("attachment; filename=\"othertext.txt\"")
+    ///         .await?;
     /// #    Ok(())
     /// # }
     /// ```
-    pub async fn presign_read_with(
-        &self,
-        path: &str,
-        op: OpRead,
-        expire: Duration,
-    ) -> Result<PresignedRequest> {
+    pub fn presign_read_with(&self, path: &str, expire: Duration) -> FuturePresignRead {
         let path = normalize_path(path);
 
-        let op = OpPresign::new(op, expire);
-
-        let rp = self.inner().presign(&path, op).await?;
-        Ok(rp.into_presigned_request())
+        let fut = FuturePresignRead(OperatorFuture::new(
+            self.inner().clone(),
+            path,
+            (OpRead::default(), expire),
+            |inner, path, (args, dur)| {
+                let fut = async move {
+                    let op = OpPresign::new(args, dur);
+                    let rp = inner.presign(&path, op).await?;
+                    Ok(rp.into_presigned_request())
+                };
+                Box::pin(fut)
+            },
+        ));
+        fut
     }
 
     /// Presign an operation for write.
diff --git a/core/src/types/operator/operator_futures.rs b/core/src/types/operator/operator_futures.rs
index 2d9a1194..b11bb0cc 100644
--- a/core/src/types/operator/operator_futures.rs
+++ b/core/src/types/operator/operator_futures.rs
@@ -25,6 +25,7 @@ use std::mem;
 use std::ops::RangeBounds;
 use std::pin::Pin;
 use std::task::{Context, Poll};
+use std::time::Duration;
 
 use futures::future::BoxFuture;
 use futures::Future;
@@ -215,6 +216,57 @@ impl Future for FutureAppender {
     }
 }
 
+/// Future that generated by [`Operator::presign_read_with`].
+///
+/// Users can add more options by public functions provided by this struct.
+pub struct FuturePresignRead(pub(crate) OperatorFuture<(OpRead, Duration), PresignedRequest>);
+
+impl FuturePresignRead {
+    /// Create a new OpRead with range.
+    pub fn range(mut self, v: BytesRange) -> Self {
+        self.0 = self.0.map_args(|(args, dur)| (args.with_range(v), dur));
+        self
+    }
+
+    /// Sets the content-disposition header that should be send back by the remote read operation.
+    pub fn override_content_disposition(mut self, v: &str) -> Self {
+        self.0 = self
+            .0
+            .map_args(|(args, dur)| (args.with_override_content_disposition(v), dur));
+        self
+    }
+
+    /// Sets the cache-control header that should be send back by the remote read operation.
+    pub fn override_cache_control(mut self, v: &str) -> Self {
+        self.0 = self
+            .0
+            .map_args(|(args, dur)| (args.with_override_cache_control(v), dur));
+        self
+    }
+
+    /// Set the If-Match of the option
+    pub fn if_match(mut self, v: &str) -> Self {
+        self.0 = self.0.map_args(|(args, dur)| (args.with_if_match(v), dur));
+        self
+    }
+
+    /// Set the If-None-Match of the option
+    pub fn if_none_match(mut self, v: &str) -> Self {
+        self.0 = self
+            .0
+            .map_args(|(args, dur)| (args.with_if_none_match(v), dur));
+        self
+    }
+}
+
+impl Future for FuturePresignRead {
+    type Output = Result<PresignedRequest>;
+
+    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+        self.0.poll_unpin(cx)
+    }
+}
+
 /// Future that generated by [`Operator::read_with`].
 ///
 /// Users can add more options by public functions provided by this struct.
diff --git a/core/tests/behavior/write.rs b/core/tests/behavior/write.rs
index 4a9d5875..b7906542 100644
--- a/core/tests/behavior/write.rs
+++ b/core/tests/behavior/write.rs
@@ -25,7 +25,6 @@ use futures::StreamExt;
 use http::StatusCode;
 use log::debug;
 use log::warn;
-use opendal::ops::OpRead;
 use opendal::EntryMode;
 use opendal::ErrorKind;
 use opendal::Operator;
@@ -869,12 +868,9 @@ pub async fn test_read_with_override_cache_control(op: Operator) -> Result<()> {
         .expect("write must succeed");
 
     let target_cache_control = "no-cache, no-store, must-revalidate";
-
-    let mut op_read = OpRead::default();
-    op_read = op_read.with_override_cache_control(target_cache_control);
-
     let signed_req = op
-        .presign_read_with(&path, op_read, Duration::from_secs(60))
+        .presign_read_with(&path, Duration::from_secs(60))
+        .override_cache_control(target_cache_control)
         .await
         .expect("sign must succeed");
 
@@ -923,11 +919,9 @@ pub async fn test_read_with_override_content_disposition(op: Operator) -> Result
 
     let target_content_disposition = "attachment; filename=foo.txt";
 
-    let mut op_read = OpRead::default();
-    op_read = op_read.with_override_content_disposition(target_content_disposition);
-
     let signed_req = op
-        .presign_read_with(&path, op_read, Duration::from_secs(60))
+        .presign_read_with(&path, Duration::from_secs(60))
+        .override_content_disposition(target_content_disposition)
         .await
         .expect("presign must succeed");