-
Notifications
You must be signed in to change notification settings - Fork 450
/
Program.cs
408 lines (349 loc) · 15.4 KB
/
Program.cs
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
namespace SimpleTest
{
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
using Dropbox.Api;
using Dropbox.Api.Files;
using Dropbox.Api.Team;
using System.Threading;
partial class Program
{
// Add an ApiKey (from https://www.dropbox.com/developers/apps) here
// private const string ApiKey = "XXXXXXXXXXXXXXX";
[STAThread]
static int Main(string[] args)
{
var instance = new Program();
var task = Task.Run((Func<Task<int>>)instance.Run);
task.Wait();
return task.Result;
}
private async Task<int> Run()
{
DropboxCertHelper.InitializeCertPinning();
var accessToken = await this.GetAccessToken();
if (string.IsNullOrEmpty(accessToken))
{
return 1;
}
// Specify socket level timeout which decides maximum waiting time when no bytes are
// received by the socket.
var httpClient = new HttpClient(new WebRequestHandler { ReadWriteTimeout = 10 * 1000 })
{
// Specify request level timeout which decides maximum time that can be spent on
// download/upload files.
Timeout = TimeSpan.FromMinutes(20)
};
try
{
var config = new DropboxClientConfig("SimpleTestApp")
{
HttpClient = httpClient
};
var client = new DropboxClient(accessToken, config);
await RunUserTests(client);
// Tests below are for Dropbox Business endpoints. To run these tests, make sure the ApiKey is for
// a Dropbox Business app and you have an admin account to log in.
/*
var client = new DropboxTeamClient(accessToken, userAgent: "SimpleTeamTestApp", httpClient: httpClient);
await RunTeamTests(client);
*/
}
catch (HttpException e)
{
Console.WriteLine("Exception reported from RPC layer");
Console.WriteLine(" Status code: {0}", e.StatusCode);
Console.WriteLine(" Message : {0}", e.Message);
if (e.RequestUri != null)
{
Console.WriteLine(" Request uri: {0}", e.RequestUri);
}
}
return 0;
}
/// <summary>
/// Run tests for user-level operations.
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <returns>An asynchronous task.</returns>
private async Task RunUserTests(DropboxClient client)
{
await GetCurrentAccount(client);
var path = "/DotNetApi/Help";
var folder = await CreateFolder(client, path);
var list = await ListFolder(client, path);
var firstFile = list.Entries.FirstOrDefault(i => i.IsFile);
if (firstFile != null)
{
await Download(client, path, firstFile.AsFile);
}
await Upload(client, path, "Test.txt", "This is a text file");
await ChunkUpload(client, path, "Binary");
}
/// <summary>
/// Run tests for team-level operations.
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <returns>An asynchronous task.</returns>
private async Task RunTeamTests(DropboxTeamClient client)
{
var members = await client.Team.MembersListAsync();
var member = members.Members.FirstOrDefault();
if (member != null)
{
// A team client can perform action on a team member's behalf. To do this,
// just pass in team member id in to AsMember function which returns a user client.
// This client will operates on this team member's Dropbox.
var userClient = client.AsMember(member.Profile.TeamMemberId);
await RunUserTests(userClient);
}
}
/// <summary>
/// Gets the dropbox access token.
/// <para>
/// This fetches the access token from the applications settings, if it is not found there
/// (or if the user chooses to reset the settings) then the UI in <see cref="LoginForm"/> is
/// displayed to authorize the user.
/// </para>
/// </summary>
/// <returns>A valid access token or null.</returns>
private async Task<string> GetAccessToken()
{
Console.Write("Reset settings (Y/N) ");
if (Console.ReadKey().Key == ConsoleKey.Y)
{
Settings.Default.Reset();
}
Console.WriteLine();
var accessToken = Settings.Default.AccessToken;
if (string.IsNullOrEmpty(accessToken))
{
Console.WriteLine("Waiting for credentials.");
var completion = new TaskCompletionSource<Tuple<string, string>>();
var thread = new Thread(() =>
{
try
{
var app = new Application();
var login = new LoginForm(ApiKey);
app.Run(login);
if (login.Result)
{
completion.TrySetResult(Tuple.Create(login.AccessToken, login.Uid));
}
else
{
completion.TrySetCanceled();
}
}
catch(Exception e)
{
completion.TrySetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
try
{
var result = await completion.Task;
Console.WriteLine("and back...");
accessToken = result.Item1;
var uid = result.Item2;
Console.WriteLine("Uid: {0}", uid);
Settings.Default.AccessToken = accessToken;
Settings.Default.Uid = uid;
Settings.Default.Save();
}
catch (Exception e)
{
e = e.InnerException ?? e;
Console.WriteLine("Error: {0}", e.Message);
return null;
}
}
return accessToken;
}
/// <summary>
/// Gets information about the currently authorized account.
/// <para>
/// This demonstrates calling a simple rpc style api from the Users namespace.
/// </para>
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <returns>An asynchronous task.</returns>
private async Task GetCurrentAccount(DropboxClient client)
{
var full = await client.Users.GetCurrentAccountAsync();
Console.WriteLine("Account id : {0}", full.AccountId);
Console.WriteLine("Country : {0}", full.Country);
Console.WriteLine("Email : {0}", full.Email);
Console.WriteLine("Is paired : {0}", full.IsPaired ? "Yes" : "No");
Console.WriteLine("Locale : {0}", full.Locale);
Console.WriteLine("Name");
Console.WriteLine(" Display : {0}", full.Name.DisplayName);
Console.WriteLine(" Familiar : {0}", full.Name.FamiliarName);
Console.WriteLine(" Given : {0}", full.Name.GivenName);
Console.WriteLine(" Surname : {0}", full.Name.Surname);
Console.WriteLine("Referral link : {0}", full.ReferralLink);
if (full.Team != null)
{
Console.WriteLine("Team");
Console.WriteLine(" Id : {0}", full.Team.Id);
Console.WriteLine(" Name : {0}", full.Team.Name);
}
else
{
Console.WriteLine("Team - None");
}
}
/// <summary>
/// Creates the specified folder.
/// </summary>
/// <remarks>This demonstrates calling an rpc style api in the Files namespace.</remarks>
/// <param name="path">The path of the folder to create.</param>
/// <param name="client">The Dropbox client.</param>
/// <returns>The result from the ListFolderAsync call.</returns>
private async Task<FolderMetadata> CreateFolder(DropboxClient client, string path)
{
Console.WriteLine("--- Creating Folder ---");
var folderArg = new CreateFolderArg(path);
var folder = await client.Files.CreateFolderAsync(folderArg);
Console.WriteLine("Folder: " + path + " created!");
return folder;
}
/// <summary>
/// Lists the items within a folder.
/// </summary>
/// <remarks>This demonstrates calling an rpc style api in the Files namespace.</remarks>
/// <param name="path">The path to list.</param>
/// <param name="client">The Dropbox client.</param>
/// <returns>The result from the ListFolderAsync call.</returns>
private async Task<ListFolderResult> ListFolder(DropboxClient client, string path)
{
Console.WriteLine("--- Files ---");
var list = await client.Files.ListFolderAsync(path);
// show folders then files
foreach (var item in list.Entries.Where(i => i.IsFolder))
{
Console.WriteLine("D {0}/", item.Name);
}
foreach (var item in list.Entries.Where(i => i.IsFile))
{
var file = item.AsFile;
Console.WriteLine("F{0,8} {1}",
file.Size,
item.Name);
}
if (list.HasMore)
{
Console.WriteLine(" ...");
}
return list;
}
/// <summary>
/// Downloads a file.
/// </summary>
/// <remarks>This demonstrates calling a download style api in the Files namespace.</remarks>
/// <param name="client">The Dropbox client.</param>
/// <param name="folder">The folder path in which the file should be found.</param>
/// <param name="file">The file to download within <paramref name="folder"/>.</param>
/// <returns></returns>
private async Task Download(DropboxClient client, string folder, FileMetadata file)
{
Console.WriteLine("Download file...");
using (var response = await client.Files.DownloadAsync(folder + "/" + file.Name))
{
Console.WriteLine("Downloaded {0} Rev {1}", response.Response.Name, response.Response.Rev);
Console.WriteLine("------------------------------");
Console.WriteLine(await response.GetContentAsStringAsync());
Console.WriteLine("------------------------------");
}
}
/// <summary>
/// Uploads given content to a file in Dropbox.
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <param name="folder">The folder to upload the file.</param>
/// <param name="fileName">The name of the file.</param>
/// <param name="fileContent">The file content.</param>
/// <returns></returns>
private async Task Upload(DropboxClient client, string folder, string fileName, string fileContent)
{
Console.WriteLine("Upload file...");
using (var stream = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(fileContent)))
{
var response = await client.Files.UploadAsync(folder + "/" + fileName, WriteMode.Overwrite.Instance, body: stream);
Console.WriteLine("Uploaded Id {0} Rev {1}", response.Id, response.Rev);
}
}
/// <summary>
/// Uploads a big file in chunk. The is very helpful for uploading large file in slow network condition
/// and also enable capability to track upload progerss.
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <param name="folder">The folder to upload the file.</param>
/// <param name="fileName">The name of the file.</param>
/// <returns></returns>
private async Task ChunkUpload(DropboxClient client, string folder, string fileName)
{
Console.WriteLine("Chunk upload file...");
// Chunk size is 128KB.
const int chunkSize = 128 * 1024;
// Create a random file of 1MB in size.
var fileContent = new byte[1024 * 1024];
new Random().NextBytes(fileContent);
using (var stream = new MemoryStream(fileContent))
{
int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);
byte[] buffer = new byte[chunkSize];
string sessionId = null;
for (var idx = 0; idx < numChunks; idx++)
{
Console.WriteLine("Start uploading chunk {0}", idx);
var byteRead = stream.Read(buffer, 0, chunkSize);
using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
{
if (idx == 0)
{
var result = await client.Files.UploadSessionStartAsync(body: memStream);
sessionId = result.SessionId;
}
else
{
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));
if (idx == numChunks - 1)
{
await client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + fileName), memStream);
}
else
{
await client.Files.UploadSessionAppendV2Async(cursor, body: memStream);
}
}
}
}
}
}
/// <summary>
/// List all members in the team.
/// </summary>
/// <param name="client">The Dropbox team client.</param>
/// <returns>The result from the MembersListAsync call.</returns>
private async Task<MembersListResult> ListTeamMembers(DropboxTeamClient client)
{
var members = await client.Team.MembersListAsync();
foreach (var member in members.Members)
{
Console.WriteLine("Member id : {0}", member.Profile.TeamMemberId);
Console.WriteLine("Name : {0}", member.Profile.Name);
Console.WriteLine("Email : {0}", member.Profile.Email);
}
return members;
}
}
}