Xuanwo commented on code in PR #2264:
URL:
https://github.com/apache/incubator-opendal/pull/2264#discussion_r1195218783
##########
core/src/services/dropbox/builder.rs:
##########
@@ -0,0 +1,68 @@
+use std::collections::HashMap;
+use std::fmt::{Debug, Formatter};
+
+use log::debug;
+
+use super::backend::DropboxBackend;
+use crate::raw::{normalize_root, HttpClient};
+use crate::Scheme;
+use crate::*;
+
+#[derive(Default)]
+pub struct DropboxBuilder {
+ access_token: Option<String>,
+ http_client: Option<HttpClient>,
+}
+
+impl Debug for DropboxBuilder {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("Backend").finish()
Review Comment:
DropboxBuilder is not `Backend`
##########
core/src/services/dropbox/backend.rs:
##########
@@ -0,0 +1,62 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use async_trait::async_trait;
+use http::StatusCode;
+
+use std::{fmt::Debug, sync::Arc};
+
+use crate::{
+ ops::{OpDelete, OpRead, OpWrite},
+ raw::{
+ parse_into_metadata, Accessor, AccessorInfo, HttpClient,
IncomingAsyncBody, RpDelete,
+ RpRead, RpWrite,
+ },
+ types::Result,
+ Capability, Error, ErrorKind,
+};
+use super::{core::DropboxCore};
+
+#[derive(Clone, Debug)]
+pub struct DropboxBackend {
+ core: Arc<DropboxCore>,
+}
+
+
+impl DropboxBackend {
+ pub(crate) fn new(access_token: String, http_client: HttpClient) -> Self {
+ DropboxBackend {
+ core: Arc::new(DropboxCore {
+ token: access_token,
+ client: http_client,
+ }),
+ }
+ }
+}
+
+#[async_trait]
+impl Accessor for DropboxBackend {
+ type Reader = IncomingAsyncBody;
+ type BlockingReader = ();
+ type Writer = ();
+ type BlockingWriter = ();
+ type Pager = ();
+ type BlockingPager = ();
+ // async fn delete(&self, path: &str, _: OpDelete) -> Result<RpDelete> {
Review Comment:
This change seems not meet the title: `Support
create/read/rename/copy/delete for Dropbox`, we should implement `Accessor`
APIs.
##########
core/src/services/dropbox/core.rs:
##########
@@ -0,0 +1,195 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::fmt::Formatter;
+use std::rc::Rc;
+
+use crate::raw::{new_json_deserialize_error, RpCopy};
+use crate::raw::percent_encode_path;
+use crate::raw::HttpClient;
+use crate::Error;
+use crate::ErrorKind;
+
+use http::request::Builder;
+use http::StatusCode;
+use http::{header, Request, Response};
+use serde::{Deserialize, Serialize};
+use tokio::sync::Mutex;
+use bytes::Bytes;
+
+use crate::{
+ raw::{new_request_build_error, AsyncBody, IncomingAsyncBody},
+ types::Result,
+};
+use crate::ops::OpCopy;
+
+pub struct DropboxCore {
+ pub token: String,
+ pub client: HttpClient,
+}
+
+impl Debug for DropboxCore {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ let mut de = f.debug_struct("DropboxCore");
+ de.finish()
+ }
+}
+
+impl DropboxCore {
+ pub async fn dropbox_get(&self, path: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url: String =
"https://content.dropboxapi.com/2/files/download".to_string();
+ let download_args = DropboxDownloadArgs {
+ path: path.to_string(),
+ };
+ let request = self
+ .header_params(self.sign(Request::post(&url)),
&serde_json::to_string(&download_args).unwrap())
+ .body(AsyncBody::Empty)
+ .map_err(new_request_build_error)?;
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_update(
+ &self,
+ path: &str,
+ size: Option<usize>,
+ content_type: Option<&str>,
+ body: AsyncBody,
+ ) -> Result<Response<IncomingAsyncBody>> {
+ let url = "https://content.dropboxapi.com/2/files/upload".to_string();
+ let args = DropboxUploadArgs {
+ path: path.to_string(),
+ mode: "overwrite".to_string(),
+ mute: true,
+ autorename: false,
+ strict_conflict: false,
+ };
+ let mut request_builder = Request::post(&url);
+ 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);
+ }
+ let request = self
+ .header_params(self.sign(request_builder),
&serde_json::to_string(&args).unwrap())
+ .body(body)
+ .map_err(new_request_build_error)?;
+
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_delete(&self, path: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url = "https://api.dropboxapi.com/2/files/delete_v2".to_string();
+ let args = DropboxDeleteArgs {
+ path: path.to_string(),
+ };
+ let request = self
+ .sign(Request::post(&url))
+ .header(header::CONTENT_TYPE, "application/json")
+
.body(AsyncBody::Bytes(Bytes::from(serde_json::to_string(&args).unwrap())))
+ .map_err(new_request_build_error)?;
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_copy(&self, from: &str, to: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url = "https://api.dropboxapi.com/2/files/copy_v2".to_string();
+ let args = DropboxCopyArgs {
+ from_path: from.to_string(),
+ to_path: to.to_string(),
+ allow_ownership_transfer: false,
+ allow_shared_folder: false,
+ autorename: false,
+ };
+ let request = self
+ .sign(Request::post(&url))
+ .header(header::CONTENT_TYPE, "application/json")
+
.body(AsyncBody::Bytes(Bytes::from(serde_json::to_string(&args).unwrap())))
+ .map_err(new_request_build_error)?;
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_move(&self, from: &str, to: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url = "https://api.dropboxapi.com/2/files/move_v2".to_string();
+ let args = DropboxRenameArgs {
+ from_path: from.to_string(),
+ to_path: to.to_string(),
+ allow_ownership_transfer: false,
+ allow_shared_folder: false,
+ autorename: false,
+ };
+ let request = self
+ .sign(Request::post(&url))
+ .header(header::CONTENT_TYPE, "application/json")
+
.body(AsyncBody::Bytes(Bytes::from(serde_json::to_string(&args).unwrap())))
+ .map_err(new_request_build_error)?;
+ self.client.send(request).await
+ }
+
+
+
+
+ pub fn sign(&self, mut req: Builder) -> Builder {
+ let auth_header_content = format!("Bearer {}", self.token);
+ req = req.header(header::AUTHORIZATION, auth_header_content);
+ req
+ }
+
+ fn header_params(&self, mut req: Builder, args: &String) -> Builder {
+ req = req.header("Dropbox-API-Arg", args);
Review Comment:
I prefer to write directly instead of adding a new API.
##########
core/src/services/dropbox/core.rs:
##########
@@ -0,0 +1,195 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::fmt::Formatter;
+use std::rc::Rc;
+
+use crate::raw::{new_json_deserialize_error, RpCopy};
+use crate::raw::percent_encode_path;
+use crate::raw::HttpClient;
+use crate::Error;
+use crate::ErrorKind;
+
+use http::request::Builder;
+use http::StatusCode;
+use http::{header, Request, Response};
+use serde::{Deserialize, Serialize};
+use tokio::sync::Mutex;
+use bytes::Bytes;
+
+use crate::{
+ raw::{new_request_build_error, AsyncBody, IncomingAsyncBody},
+ types::Result,
+};
+use crate::ops::OpCopy;
+
+pub struct DropboxCore {
+ pub token: String,
+ pub client: HttpClient,
+}
+
+impl Debug for DropboxCore {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ let mut de = f.debug_struct("DropboxCore");
+ de.finish()
+ }
+}
+
+impl DropboxCore {
+ pub async fn dropbox_get(&self, path: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url: String =
"https://content.dropboxapi.com/2/files/download".to_string();
+ let download_args = DropboxDownloadArgs {
+ path: path.to_string(),
+ };
+ let request = self
+ .header_params(self.sign(Request::post(&url)),
&serde_json::to_string(&download_args).unwrap())
+ .body(AsyncBody::Empty)
+ .map_err(new_request_build_error)?;
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_update(
+ &self,
+ path: &str,
+ size: Option<usize>,
+ content_type: Option<&str>,
+ body: AsyncBody,
+ ) -> Result<Response<IncomingAsyncBody>> {
+ let url = "https://content.dropboxapi.com/2/files/upload".to_string();
+ let args = DropboxUploadArgs {
+ path: path.to_string(),
+ mode: "overwrite".to_string(),
+ mute: true,
+ autorename: false,
+ strict_conflict: false,
+ };
+ let mut request_builder = Request::post(&url);
+ 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);
+ }
+ let request = self
+ .header_params(self.sign(request_builder),
&serde_json::to_string(&args).unwrap())
+ .body(body)
+ .map_err(new_request_build_error)?;
+
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_delete(&self, path: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url = "https://api.dropboxapi.com/2/files/delete_v2".to_string();
+ let args = DropboxDeleteArgs {
+ path: path.to_string(),
+ };
+ let request = self
+ .sign(Request::post(&url))
+ .header(header::CONTENT_TYPE, "application/json")
+
.body(AsyncBody::Bytes(Bytes::from(serde_json::to_string(&args).unwrap())))
+ .map_err(new_request_build_error)?;
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_copy(&self, from: &str, to: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url = "https://api.dropboxapi.com/2/files/copy_v2".to_string();
+ let args = DropboxCopyArgs {
+ from_path: from.to_string(),
+ to_path: to.to_string(),
+ allow_ownership_transfer: false,
+ allow_shared_folder: false,
+ autorename: false,
+ };
+ let request = self
+ .sign(Request::post(&url))
+ .header(header::CONTENT_TYPE, "application/json")
+
.body(AsyncBody::Bytes(Bytes::from(serde_json::to_string(&args).unwrap())))
+ .map_err(new_request_build_error)?;
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_move(&self, from: &str, to: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url = "https://api.dropboxapi.com/2/files/move_v2".to_string();
+ let args = DropboxRenameArgs {
+ from_path: from.to_string(),
+ to_path: to.to_string(),
+ allow_ownership_transfer: false,
+ allow_shared_folder: false,
+ autorename: false,
+ };
+ let request = self
+ .sign(Request::post(&url))
+ .header(header::CONTENT_TYPE, "application/json")
+
.body(AsyncBody::Bytes(Bytes::from(serde_json::to_string(&args).unwrap())))
Review Comment:
I prefer to split the process of building body content and request.
```rust
let args = DropboxRenameArgs {
from_path: from.to_string(),
to_path: to.to_string(),
allow_ownership_transfer: false,
allow_shared_folder: false,
autorename: false,
};
let bs = serde_json::to_vec(args)?;
let request = self
.sign(Request::post(&url))
.header(header::CONTENT_TYPE, "application/json")
.header(header::CONTENT_LENGTH, bs.len())
.body(AsyncBody::Bytes(bs))
```
##########
core/src/services/dropbox/core.rs:
##########
@@ -0,0 +1,195 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::fmt::Formatter;
+use std::rc::Rc;
+
+use crate::raw::{new_json_deserialize_error, RpCopy};
+use crate::raw::percent_encode_path;
+use crate::raw::HttpClient;
+use crate::Error;
+use crate::ErrorKind;
+
+use http::request::Builder;
+use http::StatusCode;
+use http::{header, Request, Response};
+use serde::{Deserialize, Serialize};
+use tokio::sync::Mutex;
+use bytes::Bytes;
+
+use crate::{
+ raw::{new_request_build_error, AsyncBody, IncomingAsyncBody},
+ types::Result,
+};
+use crate::ops::OpCopy;
+
+pub struct DropboxCore {
+ pub token: String,
+ pub client: HttpClient,
+}
+
+impl Debug for DropboxCore {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ let mut de = f.debug_struct("DropboxCore");
+ de.finish()
+ }
+}
+
+impl DropboxCore {
+ pub async fn dropbox_get(&self, path: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url: String =
"https://content.dropboxapi.com/2/files/download".to_string();
+ let download_args = DropboxDownloadArgs {
+ path: path.to_string(),
+ };
+ let request = self
+ .header_params(self.sign(Request::post(&url)),
&serde_json::to_string(&download_args).unwrap())
+ .body(AsyncBody::Empty)
+ .map_err(new_request_build_error)?;
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_update(
+ &self,
+ path: &str,
+ size: Option<usize>,
+ content_type: Option<&str>,
+ body: AsyncBody,
+ ) -> Result<Response<IncomingAsyncBody>> {
+ let url = "https://content.dropboxapi.com/2/files/upload".to_string();
+ let args = DropboxUploadArgs {
+ path: path.to_string(),
+ mode: "overwrite".to_string(),
+ mute: true,
+ autorename: false,
+ strict_conflict: false,
+ };
+ let mut request_builder = Request::post(&url);
+ 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);
+ }
+ let request = self
+ .header_params(self.sign(request_builder),
&serde_json::to_string(&args).unwrap())
+ .body(body)
+ .map_err(new_request_build_error)?;
+
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_delete(&self, path: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url = "https://api.dropboxapi.com/2/files/delete_v2".to_string();
+ let args = DropboxDeleteArgs {
+ path: path.to_string(),
+ };
+ let request = self
+ .sign(Request::post(&url))
+ .header(header::CONTENT_TYPE, "application/json")
+
.body(AsyncBody::Bytes(Bytes::from(serde_json::to_string(&args).unwrap())))
+ .map_err(new_request_build_error)?;
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_copy(&self, from: &str, to: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url = "https://api.dropboxapi.com/2/files/copy_v2".to_string();
+ let args = DropboxCopyArgs {
+ from_path: from.to_string(),
+ to_path: to.to_string(),
+ allow_ownership_transfer: false,
+ allow_shared_folder: false,
+ autorename: false,
+ };
+ let request = self
+ .sign(Request::post(&url))
+ .header(header::CONTENT_TYPE, "application/json")
+
.body(AsyncBody::Bytes(Bytes::from(serde_json::to_string(&args).unwrap())))
+ .map_err(new_request_build_error)?;
+ self.client.send(request).await
+ }
+
+ pub async fn dropbox_move(&self, from: &str, to: &str) ->
Result<Response<IncomingAsyncBody>> {
+ let url = "https://api.dropboxapi.com/2/files/move_v2".to_string();
+ let args = DropboxRenameArgs {
Review Comment:
We can implement `Default` for `DropboxRenameArgs` so that we can:
```
let args = DropboxRenameArgs {
from_path: from.to_string(),
to_path: to.to_string(),
..Default::default(),
};
```
##########
core/src/services/mod.rs:
##########
@@ -152,6 +152,11 @@ mod gdrive;
#[cfg(feature = "services-gdrive")]
pub use gdrive::Gdrive;
+#[cfg(feature = "services-dropbox")]
+mod dropbox;
+#[cfg(feature = "services-dropbox")]
Review Comment:
Please make sure this feature has been added into `Cargo.toml`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]