opendal/services/dropbox/
builder.rs1use jiff::Timestamp;
19use std::fmt::Debug;
20use std::fmt::Formatter;
21use std::sync::Arc;
22use tokio::sync::Mutex;
23
24use super::DEFAULT_SCHEME;
25use super::backend::DropboxBackend;
26use super::core::DropboxCore;
27use super::core::DropboxSigner;
28use crate::raw::*;
29use crate::services::DropboxConfig;
30use crate::*;
31impl Configurator for DropboxConfig {
32 type Builder = DropboxBuilder;
33
34 #[allow(deprecated)]
35 fn into_builder(self) -> Self::Builder {
36 DropboxBuilder {
37 config: self,
38 http_client: None,
39 }
40 }
41}
42
43#[doc = include_str!("docs.md")]
45#[derive(Default)]
46pub struct DropboxBuilder {
47 config: DropboxConfig,
48
49 #[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
50 http_client: Option<HttpClient>,
51}
52
53impl Debug for DropboxBuilder {
54 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
55 f.debug_struct("Builder")
56 .field("root", &self.config.root)
57 .finish()
58 }
59}
60
61impl DropboxBuilder {
62 pub fn root(mut self, root: &str) -> Self {
66 self.config.root = if root.is_empty() {
67 None
68 } else {
69 Some(root.to_string())
70 };
71
72 self
73 }
74
75 pub fn access_token(mut self, access_token: &str) -> Self {
82 self.config.access_token = Some(access_token.to_string());
83 self
84 }
85
86 pub fn refresh_token(mut self, refresh_token: &str) -> Self {
92 self.config.refresh_token = Some(refresh_token.to_string());
93 self
94 }
95
96 pub fn client_id(mut self, client_id: &str) -> Self {
100 self.config.client_id = Some(client_id.to_string());
101 self
102 }
103
104 pub fn client_secret(mut self, client_secret: &str) -> Self {
108 self.config.client_secret = Some(client_secret.to_string());
109 self
110 }
111
112 #[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
119 #[allow(deprecated)]
120 pub fn http_client(mut self, http_client: HttpClient) -> Self {
121 self.http_client = Some(http_client);
122 self
123 }
124}
125
126impl Builder for DropboxBuilder {
127 type Config = DropboxConfig;
128
129 fn build(self) -> Result<impl Access> {
130 let root = normalize_root(&self.config.root.unwrap_or_default());
131
132 let signer = match (self.config.access_token, self.config.refresh_token) {
133 (Some(access_token), None) => DropboxSigner {
134 access_token,
135 expires_in: Timestamp::MAX,
137 ..Default::default()
138 },
139 (None, Some(refresh_token)) => {
140 let client_id = self.config.client_id.ok_or_else(|| {
141 Error::new(
142 ErrorKind::ConfigInvalid,
143 "client_id must be set when refresh_token is set",
144 )
145 .with_context("service", Scheme::Dropbox)
146 })?;
147 let client_secret = self.config.client_secret.ok_or_else(|| {
148 Error::new(
149 ErrorKind::ConfigInvalid,
150 "client_secret must be set when refresh_token is set",
151 )
152 .with_context("service", Scheme::Dropbox)
153 })?;
154
155 DropboxSigner {
156 refresh_token,
157 client_id,
158 client_secret,
159 ..Default::default()
160 }
161 }
162 (Some(_), Some(_)) => {
163 return Err(Error::new(
164 ErrorKind::ConfigInvalid,
165 "access_token and refresh_token can not be set at the same time",
166 )
167 .with_context("service", Scheme::Dropbox));
168 }
169 (None, None) => {
170 return Err(Error::new(
171 ErrorKind::ConfigInvalid,
172 "access_token or refresh_token must be set",
173 )
174 .with_context("service", Scheme::Dropbox));
175 }
176 };
177
178 Ok(DropboxBackend {
179 core: Arc::new(DropboxCore {
180 info: {
181 let am = AccessorInfo::default();
182 am.set_scheme(DEFAULT_SCHEME)
183 .set_root(&root)
184 .set_native_capability(Capability {
185 stat: true,
186
187 read: true,
188
189 write: true,
190
191 create_dir: true,
192
193 delete: true,
194
195 list: true,
196 list_with_recursive: true,
197
198 copy: true,
199
200 rename: true,
201
202 shared: true,
203
204 ..Default::default()
205 });
206
207 #[allow(deprecated)]
209 if let Some(client) = self.http_client {
210 am.update_http_client(|_| client);
211 }
212
213 am.into()
214 },
215 root,
216 signer: Arc::new(Mutex::new(signer)),
217 }),
218 })
219 }
220}