From 023d7fd03086dd4e4a7c0a4e9a2d51b39dcf62e4 Mon Sep 17 00:00:00 2001 From: wavty Date: Fri, 15 Sep 2023 16:08:52 +0800 Subject: [PATCH] refactor(services/gcs): Rewrite `gcs` methods signature by using OpXxxx the gcs part of issue: #3064 --- core/src/services/gcs/backend.rs | 26 ++++---------- core/src/services/gcs/core.rs | 61 ++++++++++++-------------------- 2 files changed, 28 insertions(+), 59 deletions(-) diff --git a/core/src/services/gcs/backend.rs b/core/src/services/gcs/backend.rs index d8ad7b9cefbd..57ce7cfbd515 100644 --- a/core/src/services/gcs/backend.rs +++ b/core/src/services/gcs/backend.rs @@ -385,10 +385,7 @@ impl Accessor for GcsBackend { } async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> { - let resp = self - .core - .gcs_get_object(path, args.range(), args.if_match(), args.if_none_match()) - .await?; + let resp = self.core.gcs_get_object(path, &args).await?; if resp.status().is_success() { let meta = parse_into_metadata(path, resp.headers())?; @@ -422,10 +419,7 @@ impl Accessor for GcsBackend { return Ok(RpStat::new(Metadata::new(EntryMode::DIR))); } - let resp = self - .core - .gcs_get_object_metadata(path, args.if_match(), args.if_none_match()) - .await?; + let resp = self.core.gcs_get_object_metadata(path, &args).await?; if resp.status().is_success() { // read http response body @@ -549,19 +543,11 @@ impl Accessor for GcsBackend { async fn presign(&self, path: &str, args: OpPresign) -> Result { // We will not send this request out, just for signing. let mut req = match args.operation() { - PresignOperation::Stat(v) => { + PresignOperation::Stat(v) => self.core.gcs_head_object_xml_request(path, v)?, + PresignOperation::Read(v) => self.core.gcs_get_object_xml_request(path, v)?, + PresignOperation::Write(v) => { self.core - .gcs_head_object_xml_request(path, v.if_match(), v.if_none_match())? - } - PresignOperation::Read(v) => self.core.gcs_get_object_xml_request( - path, - v.range(), - v.if_match(), - v.if_none_match(), - )?, - PresignOperation::Write(_) => { - self.core - .gcs_insert_object_xml_request(path, None, AsyncBody::Empty)? + .gcs_insert_object_xml_request(path, v, AsyncBody::Empty)? } }; diff --git a/core/src/services/gcs/core.rs b/core/src/services/gcs/core.rs index c9eeb9cb8da7..5720db2cbfaf 100644 --- a/core/src/services/gcs/core.rs +++ b/core/src/services/gcs/core.rs @@ -146,13 +146,7 @@ impl GcsCore { } impl GcsCore { - pub fn gcs_get_object_request( - &self, - path: &str, - range: BytesRange, - if_match: Option<&str>, - if_none_match: Option<&str>, - ) -> Result> { + pub fn gcs_get_object_request(&self, path: &str, args: &OpRead) -> Result> { let p = build_abs_path(&self.root, path); let url = format!( @@ -164,14 +158,14 @@ impl GcsCore { let mut req = Request::get(&url); - if let Some(if_match) = if_match { + if let Some(if_match) = args.if_match() { req = req.header(IF_MATCH, if_match); } - if let Some(if_none_match) = if_none_match { + if let Some(if_none_match) = args.if_none_match() { req = req.header(IF_NONE_MATCH, if_none_match); } - if !range.is_full() { - req = req.header(http::header::RANGE, range.to_header()); + if !args.range().is_full() { + req = req.header(http::header::RANGE, args.range().to_header()); } let req = req @@ -185,9 +179,7 @@ impl GcsCore { pub fn gcs_get_object_xml_request( &self, path: &str, - range: BytesRange, - if_match: Option<&str>, - if_none_match: Option<&str>, + args: &OpRead, ) -> Result> { let p = build_abs_path(&self.root, path); @@ -195,14 +187,14 @@ impl GcsCore { let mut req = Request::get(&url); - if let Some(if_match) = if_match { + if let Some(if_match) = args.if_match() { req = req.header(IF_MATCH, if_match); } - if let Some(if_none_match) = if_none_match { + if let Some(if_none_match) = args.if_none_match() { req = req.header(IF_NONE_MATCH, if_none_match); } - if !range.is_full() { - req = req.header(http::header::RANGE, range.to_header()); + if !args.range().is_full() { + req = req.header(http::header::RANGE, args.range().to_header()); } let req = req @@ -215,11 +207,9 @@ impl GcsCore { pub async fn gcs_get_object( &self, path: &str, - range: BytesRange, - if_match: Option<&str>, - if_none_match: Option<&str>, + args: &OpRead, ) -> Result> { - let mut req = self.gcs_get_object_request(path, range, if_match, if_none_match)?; + let mut req = self.gcs_get_object_request(path, args)?; self.sign(&mut req).await?; self.send(req).await @@ -316,7 +306,7 @@ impl GcsCore { pub fn gcs_insert_object_xml_request( &self, path: &str, - content_type: Option<&str>, + args: &OpWrite, body: AsyncBody, ) -> Result> { let p = build_abs_path(&self.root, path); @@ -325,7 +315,7 @@ impl GcsCore { let mut req = Request::put(&url); - if let Some(content_type) = content_type { + if let Some(content_type) = args.content_type() { req = req.header(CONTENT_TYPE, content_type); } @@ -342,12 +332,7 @@ impl GcsCore { Ok(req) } - pub fn gcs_head_object_request( - &self, - path: &str, - if_match: Option<&str>, - if_none_match: Option<&str>, - ) -> Result> { + pub fn gcs_head_object_request(&self, path: &str, args: &OpStat) -> Result> { let p = build_abs_path(&self.root, path); let url = format!( @@ -359,11 +344,11 @@ impl GcsCore { let mut req = Request::get(&url); - if let Some(if_none_match) = if_none_match { + if let Some(if_none_match) = args.if_none_match() { req = req.header(IF_NONE_MATCH, if_none_match); } - if let Some(if_match) = if_match { + if let Some(if_match) = args.if_match() { req = req.header(IF_MATCH, if_match); } @@ -378,8 +363,7 @@ impl GcsCore { pub fn gcs_head_object_xml_request( &self, path: &str, - if_match: Option<&str>, - if_none_match: Option<&str>, + args: &OpStat, ) -> Result> { let p = build_abs_path(&self.root, path); @@ -387,11 +371,11 @@ impl GcsCore { let mut req = Request::head(&url); - if let Some(if_none_match) = if_none_match { + if let Some(if_none_match) = args.if_none_match() { req = req.header(IF_NONE_MATCH, if_none_match); } - if let Some(if_match) = if_match { + if let Some(if_match) = args.if_match() { req = req.header(IF_MATCH, if_match); } @@ -405,10 +389,9 @@ impl GcsCore { pub async fn gcs_get_object_metadata( &self, path: &str, - if_match: Option<&str>, - if_none_match: Option<&str>, + args: &OpStat, ) -> Result> { - let mut req = self.gcs_head_object_request(path, if_match, if_none_match)?; + let mut req = self.gcs_head_object_request(path, args)?; self.sign(&mut req).await?;