This is an automated email from the ASF dual-hosted git repository.
suyanhanx pushed a commit to branch dropbox-test
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
The following commit(s) were added to refs/heads/dropbox-test by this push:
new 2135f1a8a polish
2135f1a8a is described below
commit 2135f1a8adba3b8ef5a3e97170ed960eeafca1b5
Author: suyanhanx <[email protected]>
AuthorDate: Thu Jul 6 19:24:15 2023 +0800
polish
Signed-off-by: suyanhanx <[email protected]>
---
core/src/services/dropbox/backend.rs | 13 +++++++++----
core/src/services/dropbox/builder.rs | 2 +-
core/src/services/dropbox/core.rs | 17 ++++++++++-------
core/src/services/dropbox/error.rs | 12 ++++++++----
4 files changed, 28 insertions(+), 16 deletions(-)
diff --git a/core/src/services/dropbox/backend.rs
b/core/src/services/dropbox/backend.rs
index dea952f5b..f0ae8cde0 100644
--- a/core/src/services/dropbox/backend.rs
+++ b/core/src/services/dropbox/backend.rs
@@ -141,12 +141,17 @@ impl Accessor for DropboxBackend {
// returns last_modified and size only for files.
// FYI:
https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata
if entry_mode == EntryMode::FILE {
- let last_modified = decoded_response.client_modified;
- let date_utc_last_modified =
parse_datetime_from_rfc3339(&last_modified)?;
+ let date_utc_last_modified =
+
parse_datetime_from_rfc3339(&decoded_response.client_modified)?;
metadata.set_last_modified(date_utc_last_modified);
- if decoded_response.size.is_some() {
- let size = decoded_response.size.unwrap();
+
+ if let Some(size) = decoded_response.size {
metadata.set_content_length(size);
+ } else {
+ return Err(Error::new(
+ ErrorKind::Unexpected,
+ &format!("no size found for file {}", path),
+ ));
}
}
Ok(RpStat::new(metadata))
diff --git a/core/src/services/dropbox/builder.rs
b/core/src/services/dropbox/builder.rs
index 99eb2a237..373c83a9c 100644
--- a/core/src/services/dropbox/builder.rs
+++ b/core/src/services/dropbox/builder.rs
@@ -114,7 +114,7 @@ impl Builder for DropboxBuilder {
fn from_map(map: HashMap<String, String>) -> Self {
let mut builder = Self::default();
- builder.root(map.get("root").map(|v| v.as_str()).unwrap_or_default());
+ map.get("root").map(|v| builder.root(v));
map.get("access_token").map(|v| builder.access_token(v));
builder
}
diff --git a/core/src/services/dropbox/core.rs
b/core/src/services/dropbox/core.rs
index 14c8484c6..790d69fcd 100644
--- a/core/src/services/dropbox/core.rs
+++ b/core/src/services/dropbox/core.rs
@@ -53,7 +53,7 @@ impl DropboxCore {
let path = build_rooted_abs_path(&self.root, path);
// For dropbox, even the path is a directory,
// we still need to remove the trailing slash.
- path.strip_suffix('/').unwrap_or(&path).to_string()
+ path.trim_end_matches('/').to_string()
}
pub async fn dropbox_get(&self, path: &str) ->
Result<Response<IncomingAsyncBody>> {
@@ -66,6 +66,7 @@ impl DropboxCore {
let request = self
.build_auth_header(Request::post(&url))
.header("Dropbox-API-Arg", request_payload)
+ .header(header::CONTENT_LENGTH, 0)
.body(AsyncBody::Empty)
.map_err(new_request_build_error)?;
self.client.send(request).await
@@ -87,12 +88,11 @@ impl DropboxCore {
if let Some(size) = size {
request_builder = request_builder.header(header::CONTENT_LENGTH,
size);
}
- if let Some(mime) = content_type {
- request_builder = request_builder.header(header::CONTENT_TYPE,
mime);
- } else {
- request_builder =
- request_builder.header(header::CONTENT_TYPE,
"application/octet-stream");
- }
+ request_builder = request_builder.header(
+ header::CONTENT_TYPE,
+ content_type.unwrap_or("application/octet-stream"),
+ );
+
let request = self
.build_auth_header(request_builder)
.header(
@@ -116,6 +116,7 @@ impl DropboxCore {
let request = self
.build_auth_header(Request::post(&url))
.header(header::CONTENT_TYPE, "application/json")
+ .header(header::CONTENT_LENGTH, bs.len())
.body(AsyncBody::Bytes(bs))
.map_err(new_request_build_error)?;
self.client.send(request).await
@@ -132,6 +133,7 @@ impl DropboxCore {
let request = self
.build_auth_header(Request::post(&url))
.header(header::CONTENT_TYPE, "application/json")
+ .header(header::CONTENT_LENGTH, bs.len())
.body(AsyncBody::Bytes(bs))
.map_err(new_request_build_error)?;
self.client.send(request).await
@@ -149,6 +151,7 @@ impl DropboxCore {
let request = self
.build_auth_header(Request::post(&url))
.header(header::CONTENT_TYPE, "application/json")
+ .header(header::CONTENT_LENGTH, bs.len())
.body(AsyncBody::Bytes(bs))
.map_err(new_request_build_error)?;
self.client.send(request).await
diff --git a/core/src/services/dropbox/error.rs
b/core/src/services/dropbox/error.rs
index 460c169b3..5e5831b15 100644
--- a/core/src/services/dropbox/error.rs
+++ b/core/src/services/dropbox/error.rs
@@ -39,17 +39,20 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>)
-> Result<Error> {
_ => (ErrorKind::Unexpected, false),
};
- // We cannot get the error type from the response header when the status
code is 409.
- // Because Dropbox API v2 will put error summary in the response body, we
need to parse it
- // to get the correct error type and then error kind.
- // See
https://www.dropbox.com/developers/documentation/http/documentation#error-handling
let dropbox_error =
serde_json::from_slice::<DropboxErrorResponse>(&bs).map_err(new_json_deserialize_error);
match dropbox_error {
Ok(dropbox_error) => {
+ // We cannot get the error type from the response header when the
status code is 409.
+ // Because Dropbox API v2 will put error summary in the response
body,
+ // we need to parse it to get the correct error type and then
error kind.
+ // See
https://www.dropbox.com/developers/documentation/http/documentation#error-handling
let error_summary = dropbox_error.error_summary.as_str();
+
let mut err = Error::new(
match parts.status {
+ // 409 Conflict means that Endpoint-specific error.
+ // Look to the JSON response body for the specifics of the
error.
StatusCode::CONFLICT => {
if error_summary.contains("path/not_found")
|| error_summary.contains("path_lookup/not_found")
@@ -61,6 +64,7 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>)
-> Result<Error> {
ErrorKind::Unexpected
}
}
+ // Otherwise, we can get the error type from the response
status code.
_ => kind,
},
error_summary,