-
Notifications
You must be signed in to change notification settings - Fork 3
/
DBRestClient.m
800 lines (626 loc) · 29.1 KB
/
DBRestClient.m
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//
// DBRestClient.m
// DropboxSDK
//
// Created by Brian Smith on 4/9/10.
// Copyright 2010 Dropbox, Inc. All rights reserved.
//
#import "DBRestClient.h"
#import "DBAccountInfo.h"
#import "DBError.h"
#import "DBMetadata.h"
#import "DBRequest.h"
#import "MPOAuthURLRequest.h"
#import "MPURLRequestParameter.h"
#import "MPOAuthSignatureParameter.h"
#import "NSString+URLEscapingAdditions.h"
NSString* kDBProtocolHTTP = @"http";
NSString* kDBProtocolHTTPS = @"https";
@interface DBRestClient ()
// This method escapes all URI escape characters except /
+ (NSString*)escapePath:(NSString*)path;
- (NSMutableURLRequest*)requestWithProtocol:(NSString*)protocol host:(NSString*)host path:(NSString*)path
parameters:(NSDictionary*)params;
- (NSMutableURLRequest*)requestWithProtocol:(NSString*)protocol host:(NSString*)host path:(NSString*)path
parameters:(NSDictionary*)params method:(NSString*)method;
- (void)checkForAuthenticationFailure:(DBRequest*)request;
@end
@implementation DBRestClient
- (id)initWithSession:(DBSession*)aSession {
return [self initWithSession:aSession root:@"dropbox"];
}
- (id)initWithSession:(DBSession*)aSession root:(NSString*)aRoot {
if ((self = [super init])) {
session = [aSession retain];
root = [aRoot retain];
requests = [[NSMutableSet alloc] init];
loadRequests = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc {
for (DBRequest* request in requests) {
[request cancel];
}
[requests release];
for (DBRequest* request in [loadRequests allValues]) {
[request cancel];
}
[loadRequests release];
[session release];
[root release];
[super dealloc];
}
@synthesize delegate;
- (void)loginWithEmail:(NSString*)email password:(NSString*)password {
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
email, @"email",
password, @"password", nil];
NSURLRequest* urlRequest = [self requestWithProtocol:kDBProtocolHTTPS host:kDBDropboxAPIHost
path:@"/token" parameters:params];
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidLogin:)]
autorelease];
[requests addObject:request];
}
- (void)requestDidLogin:(DBRequest*)request {
if (request.error) {
if ([delegate respondsToSelector:@selector(restClient:loginFailedWithError:)]) {
[delegate restClient:self loginFailedWithError:request.error];
}
} else {
NSDictionary* result = (NSDictionary*)request.resultJSON;
NSString* token = [result objectForKey:@"token"];
NSString* secret = [result objectForKey:@"secret"];
[session updateAccessToken:token accessTokenSecret:secret];
if ([delegate respondsToSelector:@selector(restClientDidLogin:)]) {
[delegate restClientDidLogin:self];
}
}
[requests removeObject:request];
}
- (void)loadMetadata:(NSString*)path withHash:(NSString*)hash
{
NSDictionary* params = nil;
if (hash) {
params = [NSDictionary dictionaryWithObject:hash forKey:@"hash"];
}
NSString* fullPath = [NSString stringWithFormat:@"/metadata/%@%@", root, path];
NSURLRequest* urlRequest =
[self requestWithProtocol:kDBProtocolHTTP host:kDBDropboxAPIHost path:fullPath parameters:params];
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidLoadMetadata:)]
autorelease];
request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:root, @"root", path, @"path", nil];
[requests addObject:request];
}
- (void)loadMetadata:(NSString*)path
{
[self loadMetadata:path withHash:nil];
}
- (void)requestDidLoadMetadata:(DBRequest*)request
{
if (request.statusCode == 304) {
if ([delegate respondsToSelector:@selector(restClient:metadataUnchangedAtPath:)]) {
NSString* path = [request.userInfo objectForKey:@"path"];
[delegate restClient:self metadataUnchangedAtPath:path];
}
} else if (request.error) {
[self checkForAuthenticationFailure:request];
if ([delegate respondsToSelector:@selector(restClient:loadMetadataFailedWithError:)]) {
[delegate restClient:self loadMetadataFailedWithError:request.error];
}
} else {
[self performSelectorInBackground:@selector(parseMetadataWithRequest:) withObject:request];
}
[requests removeObject:request];
}
- (void)parseMetadataWithRequest:(DBRequest*)request {
NSAutoreleasePool* pool = [NSAutoreleasePool new];
NSDictionary* result = (NSDictionary*)[request resultJSON];
DBMetadata* metadata = [[[DBMetadata alloc] initWithDictionary:result] autorelease];
[self performSelectorOnMainThread:@selector(didParseMetadata:) withObject:metadata waitUntilDone:NO];
[pool drain];
}
- (void)didParseMetadata:(DBMetadata*)metadata {
if ([delegate respondsToSelector:@selector(restClient:loadedMetadata:)]) {
[delegate restClient:self loadedMetadata:metadata];
}
}
- (void)loadFile:(NSString *)path intoPath:(NSString *)destinationPath
{
NSString* fullPath = [NSString stringWithFormat:@"/files/%@%@", root, path];
NSURLRequest* urlRequest =
[self requestWithProtocol:kDBProtocolHTTPS host:kDBDropboxAPIContentHost path:fullPath parameters:nil];
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidLoadFile:)]
autorelease];
request.resultFilename = destinationPath;
request.downloadProgressSelector = @selector(requestLoadProgress:);
request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
root, @"root",
path, @"path",
destinationPath, @"destinationPath", nil];
[loadRequests setObject:request forKey:path];
}
- (void)cancelFileLoad:(NSString*)path {
DBRequest* outstandingRequest = [loadRequests objectForKey:path];
if (outstandingRequest) {
[outstandingRequest cancel];
[loadRequests removeObjectForKey:path];
}
}
- (void)requestLoadProgress:(DBRequest*)request {
if ([delegate respondsToSelector:@selector(restClient:loadProgress:forFile:)]) {
[delegate restClient:self loadProgress:request.downloadProgress forFile:request.resultFilename];
}
}
- (void)restClient:(DBRestClient*)restClient loadedFile:(NSString*)destPath
contentType:(NSString*)contentType eTag:(NSString*)eTag {
// Empty selector to get the signature from
}
- (void)requestDidLoadFile:(DBRequest*)request {
NSString* path = [request.userInfo objectForKey:@"path"];
if (request.error) {
[self checkForAuthenticationFailure:request];
if ([delegate respondsToSelector:@selector(restClient:loadFileFailedWithError:)]) {
[delegate restClient:self loadFileFailedWithError:request.error];
}
} else {
NSString* filename = request.resultFilename;
NSDictionary* headers = [request.response allHeaderFields];
NSString* contentType = [headers objectForKey:@"Content-Type"];
NSString* eTag = [headers objectForKey:@"Etag"];
if ([delegate respondsToSelector:@selector(restClient:loadedFile:)]) {
[delegate restClient:self loadedFile:filename];
} else if ([delegate respondsToSelector:@selector(restClient:loadedFile:contentType:)]) {
[delegate restClient:self loadedFile:filename contentType:contentType];
} else if ([delegate respondsToSelector:@selector(restClient:loadedFile:contentType:eTag:)]) {
// This code is for the official Dropbox client to get eTag information from the server
NSMethodSignature* signature =
[self methodSignatureForSelector:@selector(restClient:loadedFile:contentType:eTag:)];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:delegate];
[invocation setSelector:@selector(restClient:loadedFile:contentType:eTag:)];
[invocation setArgument:&self atIndex:2];
[invocation setArgument:&filename atIndex:3];
[invocation setArgument:&contentType atIndex:4];
[invocation setArgument:&eTag atIndex:5];
[invocation invoke];
}
}
[loadRequests removeObjectForKey:path];
}
- (void)loadThumbnail:(NSString *)path ofSize:(NSString *)size intoPath:(NSString *)destinationPath
{
NSString* fullPath = [NSString stringWithFormat:@"/thumbnails/%@%@", root, path];
NSDictionary *params = nil;
if(size) {
params = [NSDictionary dictionaryWithObjectsAndKeys: size, @"size", nil];
}
NSURLRequest* urlRequest =
[self requestWithProtocol:kDBProtocolHTTP host:kDBDropboxAPIContentHost path:fullPath parameters:params];
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidLoadThumbnail:)]
autorelease];
request.resultFilename = destinationPath;
request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
root, @"root",
path, @"path",
destinationPath, @"destinationPath", nil];
[requests addObject:request];
}
- (void)requestDidLoadThumbnail:(DBRequest*)request
{
if (request.error) {
[self checkForAuthenticationFailure:request];
if ([delegate respondsToSelector:@selector(restClient:loadThumbnailFailedWithError:)]) {
[delegate restClient:self loadThumbnailFailedWithError:request.error];
}
} else {
if ([delegate respondsToSelector:@selector(restClient:loadedThumbnail:)]) {
[delegate restClient:self loadedThumbnail:request.resultFilename];
}
}
[requests removeObject:request];
}
NSString *createFakeSignature(DBSession *session, NSArray *params, NSString *filename, NSURL *baseUrl)
{
NSArray* extraParams = [MPURLRequestParameter parametersFromDictionary:
[NSDictionary dictionaryWithObject:filename forKey:@"file"]];
NSMutableArray* paramList = [NSMutableArray arrayWithArray:params];
[paramList addObjectsFromArray:extraParams];
[paramList sortUsingSelector:@selector(compare:)];
NSString* paramString = [MPURLRequestParameter parameterStringForParameters:paramList];
MPOAuthURLRequest* oauthRequest =
[[[MPOAuthURLRequest alloc] initWithURL:baseUrl andParameters:paramList] autorelease];
oauthRequest.HTTPMethod = @"POST";
MPOAuthSignatureParameter *signatureParameter =
[[[MPOAuthSignatureParameter alloc]
initWithText:paramString andSecret:session.credentialStore.signingKey
forRequest:oauthRequest usingMethod:session.credentialStore.signatureMethod]
autorelease];
return [signatureParameter URLEncodedParameterString];
}
NSMutableURLRequest *createRealRequest(DBSession *session, NSArray *params, NSString *urlString, NSString *signatureText)
{
NSMutableArray *paramList = [NSMutableArray arrayWithArray:params];
// Then rebuild request using that signature
[paramList sortUsingSelector:@selector(compare:)];
NSMutableString* realParamString = [[[NSMutableString alloc] initWithString:
[MPURLRequestParameter parameterStringForParameters:paramList]]
autorelease];
[realParamString appendFormat:@"&%@", signatureText];
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", urlString, realParamString]];
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
urlRequest.HTTPMethod = @"POST";
return urlRequest;
}
// Returns DBErrorNone if no errors were encountered
DBErrorCode addFileUploadToRequest(NSMutableURLRequest *urlRequest, NSString *filename, NSString *sourcePath)
{
// Create input stream
CFUUIDRef uuid = CFUUIDCreate(NULL);
NSString* stringBoundary = [(NSString*)CFUUIDCreateString(NULL, uuid) autorelease];
CFRelease(uuid);
NSString* contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSString* tempFilename =
[NSString stringWithFormat: @"%.0f.txt", [NSDate timeIntervalSinceReferenceDate] * 1000.0];
NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:tempFilename];
//setting up the body
NSMutableData* bodyData = [NSMutableData data];
[bodyData appendData:
[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
// Add data to upload
[bodyData appendData:
[[NSString stringWithFormat:
@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", filename]
dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:
[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
if (![[NSFileManager defaultManager] createFileAtPath:tempFilePath contents:bodyData attributes:nil]) {
NSLog(@"DBRestClient#uploadFileToRoot:path:filename:fromPath: failed to create file");
return DBErrorGenericError;
}
NSFileHandle* bodyFile = [NSFileHandle fileHandleForWritingAtPath:tempFilePath];
[bodyFile seekToEndOfFile];
if ([[NSFileManager defaultManager] fileExistsAtPath:sourcePath]) {
NSFileHandle* readFile = [NSFileHandle fileHandleForReadingAtPath:sourcePath];
NSData* readData;
while ((readData = [readFile readDataOfLength:1024 * 512]) != nil && [readData length] > 0) {
@try {
[bodyFile writeData:readData];
} @catch (NSException* e) {
NSLog(@"DBRestClient#uploadFileToRoot:path:filename:fromPath: failed to write data");
[readFile closeFile];
[bodyFile closeFile];
[[NSFileManager defaultManager] removeItemAtPath:tempFilePath error:nil];
return DBErrorInsufficientDiskSpace;
}
}
[readFile closeFile];
} else {
NSLog(@"DBRestClient#uploadFileToRoot:path:filename:fromPath: unable to open sourceFile");
}
@try {
[bodyFile writeData:
[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
} @catch (NSException* e) {
NSLog(@"DBRestClient#uploadFileToRoot:path:filename:fromPath: failed to write end of data");
[bodyFile closeFile];
[[NSFileManager defaultManager] removeItemAtPath:tempFilePath error:nil];
return DBErrorInsufficientDiskSpace;
}
NSString* contentLength = [NSString stringWithFormat: @"%qu", [bodyFile offsetInFile]];
[urlRequest addValue:contentLength forHTTPHeaderField: @"Content-Length"];
[bodyFile closeFile];
urlRequest.HTTPBodyStream = [NSInputStream inputStreamWithFileAtPath:tempFilePath];
return DBErrorNone;
}
- (void)uploadFile:(NSString*)filename toPath:(NSString*)path fromPath:(NSString *)sourcePath
{
if (![[NSFileManager defaultManager] fileExistsAtPath:sourcePath]) {
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:sourcePath forKey:@"sourcePath"];
NSError* error =
[NSError errorWithDomain:DBErrorDomain code:DBErrorFileNotFound userInfo:userInfo];
if ([delegate respondsToSelector:@selector(restClient:uploadFileFailedWithError:)]) {
[delegate restClient:self uploadFileFailedWithError:error];
}
return;
}
// path is the directory the file will be uploaded to, make sure it doesn't have a trailing /
// (unless it's the root dir) and is properly escaped
NSString* trimmedPath;
if ([path length] > 1 && [path characterAtIndex:[path length]-1] == '/') {
trimmedPath = [path substringToIndex:[path length]-1];
} else {
trimmedPath = path;
}
NSString* escapedPath = [DBRestClient escapePath:trimmedPath];
NSString* urlString = [NSString stringWithFormat:@"%@://%@/%@/files/%@%@",
kDBProtocolHTTPS, kDBDropboxAPIContentHost, kDBDropboxAPIVersion, root, escapedPath];
NSURL* baseUrl = [NSURL URLWithString:urlString];
NSArray* params = [session.credentialStore oauthParameters];
NSString *escapedFilename = [filename stringByReplacingOccurrencesOfString:@";" withString:@"-"];
NSString *signatureText = createFakeSignature(session, params, escapedFilename, baseUrl);
NSMutableURLRequest *urlRequest = createRealRequest(session, params, urlString, signatureText);
DBErrorCode errorCode = addFileUploadToRequest(urlRequest, escapedFilename, sourcePath);
if(errorCode == DBErrorNone) {
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidUploadFile:)]
autorelease];
request.uploadProgressSelector = @selector(requestUploadProgress:);
NSString* dropboxPath = [path stringByAppendingPathComponent:filename];
request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
root, @"root",
path, @"path",
dropboxPath, @"destinationPath",
sourcePath, @"sourcePath", nil];
[requests addObject:request];
} else {
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:sourcePath forKey:@"sourcePath"];
NSError* error =
[NSError errorWithDomain:DBErrorDomain code:errorCode userInfo:userInfo];
if ([delegate respondsToSelector:@selector(restClient:uploadFileFailedWithError:)]) {
[delegate restClient:self uploadFileFailedWithError:error];
}
}
}
- (void)requestUploadProgress:(DBRequest*)request {
NSString* sourcePath = [(NSDictionary*)request.userInfo objectForKey:@"sourcePath"];
NSString* destPath = [request.userInfo objectForKey:@"destinationPath"];
if ([delegate respondsToSelector:@selector(restClient:uploadProgress:forFile:from:)]) {
[delegate restClient:self uploadProgress:request.uploadProgress
forFile:destPath from:sourcePath];
} else if ([delegate respondsToSelector:@selector(restClient:uploadProgress:forFile:)]) {
[delegate restClient:self uploadProgress:request.uploadProgress forFile:sourcePath];
}
}
- (void)requestDidUploadFile:(DBRequest*)request {
if (request.error) {
[self checkForAuthenticationFailure:request];
if ([delegate respondsToSelector:@selector(restClient:uploadFileFailedWithError:)]) {
[delegate restClient:self uploadFileFailedWithError:request.error];
}
} else {
NSString* sourcePath = [(NSDictionary*)request.userInfo objectForKey:@"sourcePath"];
NSString* destPath = [request.userInfo objectForKey:@"destinationPath"];
if ([delegate respondsToSelector:@selector(restClient:uploadedFile:from:)]) {
[delegate restClient:self uploadedFile:destPath from:sourcePath];
} else if ([delegate respondsToSelector:@selector(restClient:uploadedFile:)]) {
[delegate restClient:self uploadedFile:sourcePath];
}
}
[requests removeObject:request];
}
- (void)moveFrom:(NSString*)from_path toPath:(NSString *)to_path
{
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
root, @"root",
from_path, @"from_path",
to_path, @"to_path", nil];
NSMutableURLRequest* urlRequest =
[self requestWithProtocol:kDBProtocolHTTP host:kDBDropboxAPIHost path:@"/fileops/move"
parameters:params method:@"POST"];
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidMovePath:)]
autorelease];
request.userInfo = params;
[requests addObject:request];
}
- (void)requestDidMovePath:(DBRequest*)request {
if (request.error) {
[self checkForAuthenticationFailure:request];
if ([delegate respondsToSelector:@selector(restClient:movePathFailedWithError:)]) {
[delegate restClient:self movePathFailedWithError:request.error];
}
} else {
NSDictionary *params = (NSDictionary *)request.userInfo;
if ([delegate respondsToSelector:@selector(restClient:movedPath:toPath:)]) {
[delegate restClient:self movedPath:[params valueForKey:@"from_path"]
toPath:[params valueForKey:@"to_path"]];
}
}
[requests removeObject:request];
}
- (void)copyFrom:(NSString*)from_path toPath:(NSString *)to_path
{
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
root, @"root",
from_path, @"from_path",
to_path, @"to_path", nil];
NSMutableURLRequest* urlRequest =
[self requestWithProtocol:kDBProtocolHTTP host:kDBDropboxAPIHost path:@"/fileops/copy"
parameters:params method:@"POST"];
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidCopyPath:)]
autorelease];
request.userInfo = params;
[requests addObject:request];
}
- (void)requestDidCopyPath:(DBRequest*)request {
if (request.error) {
[self checkForAuthenticationFailure:request];
if ([delegate respondsToSelector:@selector(restClient:copyPathFailedWithError:)]) {
[delegate restClient:self copyPathFailedWithError:request.error];
}
} else {
NSDictionary *params = (NSDictionary *)request.userInfo;
if ([delegate respondsToSelector:@selector(restClient:copiedPath:toPath:)]) {
[delegate restClient:self copiedPath:[params valueForKey:@"from_path"]
toPath:[params valueForKey:@"to_path"]];
}
}
[requests removeObject:request];
}
- (void)deletePath:(NSString*)path
{
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
root, @"root",
path, @"path", nil];
NSMutableURLRequest* urlRequest =
[self requestWithProtocol:kDBProtocolHTTP host:kDBDropboxAPIHost path:@"/fileops/delete"
parameters:params method:@"POST"];
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidDeletePath:)]
autorelease];
request.userInfo = params;
[requests addObject:request];
}
- (void)requestDidDeletePath:(DBRequest*)request {
if (request.error) {
[self checkForAuthenticationFailure:request];
if ([delegate respondsToSelector:@selector(restClient:deletePathFailedWithError:)]) {
[delegate restClient:self deletePathFailedWithError:request.error];
}
} else {
if ([delegate respondsToSelector:@selector(restClient:deletedPath:)]) {
NSString* path = [request.userInfo objectForKey:@"path"];
[delegate restClient:self deletedPath:path];
}
}
[requests removeObject:request];
}
- (void)createFolder:(NSString*)path
{
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
root, @"root",
path, @"path", nil];
NSString* fullPath = @"/fileops/create_folder";
NSMutableURLRequest* urlRequest =
[self requestWithProtocol:kDBProtocolHTTP host:kDBDropboxAPIHost path:fullPath
parameters:params method:@"POST"];
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidCreateDirectory:)]
autorelease];
request.userInfo = params;
[requests addObject:request];
}
- (void)requestDidCreateDirectory:(DBRequest*)request {
if (request.error) {
[self checkForAuthenticationFailure:request];
if ([delegate respondsToSelector:@selector(restClient:createFolderFailedWithError:)]) {
[delegate restClient:self createFolderFailedWithError:request.error];
}
} else {
NSDictionary* result = (NSDictionary*)[request resultJSON];
DBMetadata* metadata = [[[DBMetadata alloc] initWithDictionary:result] autorelease];
if ([delegate respondsToSelector:@selector(restClient:createdFolder:)]) {
[delegate restClient:self createdFolder:metadata];
}
}
[requests removeObject:request];
}
- (void)loadAccountInfo
{
NSURLRequest* urlRequest =
[self requestWithProtocol:kDBProtocolHTTP host:kDBDropboxAPIHost path:@"/account/info" parameters:nil];
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidLoadAccountInfo:)]
autorelease];
request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:root, @"root", nil];
[requests addObject:request];
}
- (void)requestDidLoadAccountInfo:(DBRequest*)request
{
if (request.error) {
[self checkForAuthenticationFailure:request];
if ([delegate respondsToSelector:@selector(restClient:loadAccountInfoFailedWithError:)]) {
[delegate restClient:self loadAccountInfoFailedWithError:request.error];
}
} else {
NSDictionary* result = (NSDictionary*)[request resultJSON];
DBAccountInfo* accountInfo = [[[DBAccountInfo alloc] initWithDictionary:result] autorelease];
if ([delegate respondsToSelector:@selector(restClient:loadedAccountInfo:)]) {
[delegate restClient:self loadedAccountInfo:accountInfo];
}
}
[requests removeObject:request];
}
- (void)createAccount:(NSString *)email password:(NSString *)password firstName:(NSString *)firstName lastName:(NSString *)lastName
{
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
email, @"email",
password, @"password",
firstName, @"first_name",
lastName, @"last_name", nil];
NSString* fullPath = @"/account";
NSMutableURLRequest* urlRequest =
[self requestWithProtocol:kDBProtocolHTTPS host:kDBDropboxAPIHost path:fullPath
parameters:params method:@"POST"];
DBRequest* request =
[[[DBRequest alloc]
initWithURLRequest:urlRequest andInformTarget:self selector:@selector(requestDidCreateAccount:)]
autorelease];
request.userInfo = params;
[requests addObject:request];
}
- (void)requestDidCreateAccount:(DBRequest *)request
{
if(request.error) {
if([delegate respondsToSelector:@selector(restClient:createAccountFailedWithError:)]) {
[delegate restClient:self createAccountFailedWithError:request.error];
}
} else {
if ([delegate respondsToSelector:@selector(restClientCreatedAccount:)]) {
[delegate restClientCreatedAccount:self];
}
}
[requests removeObject:request];
}
#pragma mark private methods
+ (NSString*)escapePath:(NSString*)path {
CFStringEncoding encoding = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);
NSString *escapedPath =
(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)path,
NULL,
(CFStringRef)@":?=,!$&'()*+;[]@#~",
encoding);
return [escapedPath autorelease];
}
- (NSMutableURLRequest*)requestWithProtocol:(NSString*)protocol host:(NSString*)host path:(NSString*)path
parameters:(NSDictionary*)params {
return [self requestWithProtocol:protocol host:host path:path parameters:params method:nil];
}
- (NSMutableURLRequest*)requestWithProtocol:(NSString*)protocol host:(NSString*)host path:(NSString*)path
parameters:(NSDictionary*)params method:(NSString*)method {
NSString* escapedPath = [DBRestClient escapePath:path];
NSString* urlString = [NSString stringWithFormat:@"%@://%@/%@%@",
protocol, host, kDBDropboxAPIVersion, escapedPath];
NSURL* url = [NSURL URLWithString:urlString];
NSArray* paramList = [session.credentialStore oauthParameters];
if ([params count] > 0) {
NSArray* extraParams = [MPURLRequestParameter parametersFromDictionary:params];
paramList = [paramList arrayByAddingObjectsFromArray:extraParams];
}
MPOAuthURLRequest* oauthRequest =
[[[MPOAuthURLRequest alloc] initWithURL:url andParameters:paramList] autorelease];
if (method) {
oauthRequest.HTTPMethod = method;
}
NSMutableURLRequest* urlRequest = [oauthRequest
urlRequestSignedWithSecret:session.credentialStore.signingKey
usingMethod:session.credentialStore.signatureMethod];
return urlRequest;
}
- (void)checkForAuthenticationFailure:(DBRequest*)request {
if (request.error && request.error.code == 401 && [request.error.domain isEqual:@"dropbox.com"]) {
[session.delegate sessionDidReceiveAuthorizationFailure:session];
}
}
@end