-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdemo-async.rs
184 lines (165 loc) · 6.35 KB
/
demo-async.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![deny(rust_2018_idioms)]
//! This example illustrates a few basic Dropbox API operations: getting an OAuth2 token, listing
//! the contents of a folder recursively, and fetching a file given its path.
use dropbox_sdk::async_routes::files;
use dropbox_sdk::default_async_client::{NoauthDefaultClient, UserAuthDefaultClient};
use tokio_util::compat::FuturesAsyncReadCompatExt;
enum Operation {
Usage,
List(String),
Download(String),
Stat(String),
}
fn parse_args() -> Operation {
let mut ctor: Option<fn(String) -> Operation> = None;
for arg in std::env::args().skip(1) {
match arg.as_str() {
"--help" | "-h" => return Operation::Usage,
"--list" => {
ctor = Some(Operation::List);
}
"--download" => {
ctor = Some(Operation::Download);
}
"--stat" => {
ctor = Some(Operation::Stat);
}
path if path.starts_with('/') => {
return if let Some(ctor) = ctor {
ctor(arg)
} else {
eprintln!("Either --download or --list must be specified");
Operation::Usage
};
}
_ => {
eprintln!("Unrecognized option {arg:?}");
eprintln!();
return Operation::Usage;
}
}
}
Operation::Usage
}
#[tokio::main]
async fn main() {
env_logger::init();
let op = parse_args();
if let Operation::Usage = op {
eprintln!("usage: {} [option]", std::env::args().next().unwrap());
eprintln!(" options:");
eprintln!(" --help | -h view this text");
eprintln!(" --download <path> copy the contents of <path> to stdout");
eprintln!(" --list <path> recursively list all files under <path>");
eprintln!(" --stat <path> list all metadata of <path>");
eprintln!();
eprintln!(" If a Dropbox OAuth token is given in the environment variable");
eprintln!(" DBX_OAUTH_TOKEN, it will be used, otherwise you will be prompted for");
eprintln!(" authentication interactively.");
std::process::exit(1);
}
let mut auth = dropbox_sdk::oauth2::get_auth_from_env_or_prompt();
if auth.save().is_none() {
auth.obtain_access_token_async(NoauthDefaultClient::default())
.await
.unwrap();
eprintln!("Next time set these environment variables to reuse this authorization:");
eprintln!(" DBX_CLIENT_ID={}", auth.client_id());
eprintln!(" DBX_OAUTH={}", auth.save().unwrap());
}
let client = UserAuthDefaultClient::new(auth);
match op {
Operation::Usage => (), // handled above
Operation::Download(path) => {
eprintln!("Copying file to stdout: {}", path);
eprintln!();
match files::download(&client, &files::DownloadArg::new(path), None, None).await {
Ok(result) => {
match tokio::io::copy(
&mut result.body.expect("there must be a response body").compat(),
&mut tokio::io::stdout(),
)
.await
{
Ok(n) => {
eprintln!("Downloaded {n} bytes");
}
Err(e) => {
eprintln!("I/O error: {e}");
}
}
}
Err(e) => {
eprintln!("Error from files/download: {e}");
}
}
}
Operation::List(mut path) => {
eprintln!("Listing recursively: {path}");
// Special case: the root folder is empty string. All other paths need to start with '/'.
if path == "/" {
path.clear();
}
let mut result = match files::list_folder(
&client,
&files::ListFolderArg::new(path).with_recursive(true),
)
.await
{
Ok(result) => result,
Err(e) => {
eprintln!("Error from files/list_folder: {e}");
return;
}
};
let mut num_entries = result.entries.len();
let mut num_pages = 1;
loop {
for entry in result.entries {
match entry {
files::Metadata::Folder(entry) => {
println!("Folder: {}", entry.path_display.unwrap_or(entry.name));
}
files::Metadata::File(entry) => {
println!("File: {}", entry.path_display.unwrap_or(entry.name));
}
files::Metadata::Deleted(entry) => {
panic!("unexpected deleted entry: {:?}", entry);
}
}
}
if !result.has_more {
break;
}
result = match files::list_folder_continue(
&client,
&files::ListFolderContinueArg::new(result.cursor),
)
.await
{
Ok(result) => {
num_pages += 1;
num_entries += result.entries.len();
result
}
Err(e) => {
eprintln!("Error from files/list_folder_continue: {e}");
break;
}
}
}
eprintln!("{num_entries} entries from {num_pages} result pages");
}
Operation::Stat(path) => {
eprintln!("listing metadata for: {path}");
let arg = files::GetMetadataArg::new(path)
.with_include_media_info(true)
.with_include_deleted(true)
.with_include_has_explicit_shared_members(true);
match files::get_metadata(&client, &arg).await {
Ok(result) => println!("{result:#?}"),
Err(e) => eprintln!("Error from files/get_metadata: {e}"),
}
}
}
}