jsonbody outsource
This commit is contained in:
parent
c9924d6ec5
commit
e7f105091b
@ -38,6 +38,8 @@
|
|||||||
- (void)contentsOfPath:(NSString *)path
|
- (void)contentsOfPath:(NSString *)path
|
||||||
completion:(void (^)(NSArray *data))handler
|
completion:(void (^)(NSArray *data))handler
|
||||||
presenter:(UIViewController *)presenter;
|
presenter:(UIViewController *)presenter;
|
||||||
|
- (void)createFolderAtPath:(NSString *)path
|
||||||
|
presenter:(UIViewController *)presenter;
|
||||||
|
|
||||||
#pragma mark - Setup methods
|
#pragma mark - Setup methods
|
||||||
|
|
||||||
|
|||||||
@ -184,10 +184,8 @@
|
|||||||
}
|
}
|
||||||
options:0
|
options:0
|
||||||
error:nil];
|
error:nil];
|
||||||
NSString *temp =
|
|
||||||
[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
|
request.HTTPBody = [self createJSONExcapedHTTPBody:data];
|
||||||
temp = [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
|
|
||||||
request.HTTPBody = [temp dataUsingEncoding:NSASCIIStringEncoding];
|
|
||||||
|
|
||||||
[NSURLConnection
|
[NSURLConnection
|
||||||
sendAsynchronousRequest:request
|
sendAsynchronousRequest:request
|
||||||
@ -300,12 +298,12 @@
|
|||||||
}
|
}
|
||||||
options:0
|
options:0
|
||||||
error:nil];
|
error:nil];
|
||||||
NSString *temp = [[NSString alloc] initWithData:data2
|
|
||||||
encoding:NSASCIIStringEncoding];
|
|
||||||
temp =
|
|
||||||
[temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
|
|
||||||
|
|
||||||
[request setValue:temp.precomposedStringWithCanonicalMapping
|
[request setValue:[[NSString alloc]
|
||||||
|
initWithData:[self
|
||||||
|
createJSONExcapedHTTPBody:data2]
|
||||||
|
encoding:NSASCIIStringEncoding]
|
||||||
|
.precomposedStringWithCanonicalMapping
|
||||||
forHTTPHeaderField:(@"Dropbox-API-Arg")
|
forHTTPHeaderField:(@"Dropbox-API-Arg")
|
||||||
.precomposedStringWithCanonicalMapping];
|
.precomposedStringWithCanonicalMapping];
|
||||||
|
|
||||||
@ -409,12 +407,12 @@
|
|||||||
}
|
}
|
||||||
options:0
|
options:0
|
||||||
error:nil];
|
error:nil];
|
||||||
NSString *temp = [[NSString alloc] initWithData:data2
|
|
||||||
encoding:NSASCIIStringEncoding];
|
|
||||||
temp =
|
|
||||||
[temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
|
|
||||||
|
|
||||||
[request setValue:temp.precomposedStringWithCanonicalMapping
|
[request setValue:[[NSString alloc]
|
||||||
|
initWithData:[self
|
||||||
|
createJSONExcapedHTTPBody:data2]
|
||||||
|
encoding:NSASCIIStringEncoding]
|
||||||
|
.precomposedStringWithCanonicalMapping
|
||||||
forHTTPHeaderField:(@"Dropbox-API-Arg")
|
forHTTPHeaderField:(@"Dropbox-API-Arg")
|
||||||
.precomposedStringWithCanonicalMapping];
|
.precomposedStringWithCanonicalMapping];
|
||||||
|
|
||||||
@ -483,6 +481,83 @@
|
|||||||
handler();
|
handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
- (void)createFolderAtPath:(NSString *)path
|
||||||
|
presenter:(UIViewController *)presenter
|
||||||
|
{
|
||||||
|
if (path == nil) {
|
||||||
|
path = _rootDirectory;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
path = [NSString stringWithFormat:@"/%@", path];
|
||||||
|
}
|
||||||
|
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
|
||||||
|
request.URL = [NSURL
|
||||||
|
URLWithString:@"https://api.dropboxapi.com/2/files/create_folder"];
|
||||||
|
[request addValue:[NSString stringWithFormat:@"Bearer %@", _token]
|
||||||
|
.precomposedStringWithCanonicalMapping
|
||||||
|
forHTTPHeaderField:(@"Authorization")
|
||||||
|
.precomposedStringWithCanonicalMapping];
|
||||||
|
[request addValue:(@"application/json")
|
||||||
|
.precomposedStringWithCanonicalMapping
|
||||||
|
forHTTPHeaderField:(@"Content-Type")
|
||||||
|
.precomposedStringWithCanonicalMapping];
|
||||||
|
request.HTTPMethod = @"POST";
|
||||||
|
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
|
||||||
|
@"path" : path.precomposedStringWithCanonicalMapping
|
||||||
|
}
|
||||||
|
options:0
|
||||||
|
error:nil];
|
||||||
|
|
||||||
|
request.HTTPBody = [self createJSONExcapedHTTPBody:data];
|
||||||
|
|
||||||
|
[NSURLConnection
|
||||||
|
sendAsynchronousRequest:request
|
||||||
|
queue:[[NSOperationQueue alloc] init]
|
||||||
|
completionHandler:^(NSURLResponse *response, NSData *data,
|
||||||
|
NSError *error) {
|
||||||
|
if (error == nil) {
|
||||||
|
if ([[NSJSONSerialization JSONObjectWithData:data
|
||||||
|
options:0
|
||||||
|
error:nil]
|
||||||
|
valueForKey:@"error_summary"] == nil) {
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
|
||||||
|
[SharedDeclerations
|
||||||
|
presentErrorAlert:
|
||||||
|
[NSError
|
||||||
|
errorWithDomain:DropboxErrorDomain
|
||||||
|
code:100
|
||||||
|
userInfo:@{
|
||||||
|
[[NSJSONSerialization
|
||||||
|
JSONObjectWithData:data
|
||||||
|
options:0
|
||||||
|
error:nil]
|
||||||
|
valueForKey:
|
||||||
|
DropboxErrorUserInfo] :
|
||||||
|
NSLocalizedDescriptionKey
|
||||||
|
}]
|
||||||
|
presenter:presenter
|
||||||
|
.navigationController
|
||||||
|
.topViewController];
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (error != nil) {
|
||||||
|
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
|
||||||
|
|
||||||
|
[SharedDeclerations
|
||||||
|
presentErrorAlert:error
|
||||||
|
presenter:presenter.navigationController
|
||||||
|
.topViewController];
|
||||||
|
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
#pragma mark - Setup methods
|
#pragma mark - Setup methods
|
||||||
|
|
||||||
- (void)setupDropboxWithHandler:(void (^)(void))handler
|
- (void)setupDropboxWithHandler:(void (^)(void))handler
|
||||||
@ -586,82 +661,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)createFolderAtPath:(NSString *)path
|
- (NSData *)createJSONExcapedHTTPBody:(NSData *)data
|
||||||
presenter:(UIViewController *)presenter
|
|
||||||
{
|
{
|
||||||
if (path == nil) {
|
|
||||||
path = _rootDirectory;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
path = [NSString stringWithFormat:@"/%@", path];
|
|
||||||
}
|
|
||||||
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
|
|
||||||
request.URL = [NSURL
|
|
||||||
URLWithString:@"https://api.dropboxapi.com/2/files/create_folder"];
|
|
||||||
[request addValue:[NSString stringWithFormat:@"Bearer %@", _token]
|
|
||||||
.precomposedStringWithCanonicalMapping
|
|
||||||
forHTTPHeaderField:(@"Authorization")
|
|
||||||
.precomposedStringWithCanonicalMapping];
|
|
||||||
[request addValue:(@"application/json")
|
|
||||||
.precomposedStringWithCanonicalMapping
|
|
||||||
forHTTPHeaderField:(@"Content-Type")
|
|
||||||
.precomposedStringWithCanonicalMapping];
|
|
||||||
request.HTTPMethod = @"POST";
|
|
||||||
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
|
|
||||||
@"path" : path.precomposedStringWithCanonicalMapping
|
|
||||||
}
|
|
||||||
options:0
|
|
||||||
error:nil];
|
|
||||||
NSString *temp =
|
NSString *temp =
|
||||||
[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
|
[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
|
||||||
temp = [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
|
temp = [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
|
||||||
request.HTTPBody = [temp dataUsingEncoding:NSASCIIStringEncoding];
|
return [temp dataUsingEncoding:NSASCIIStringEncoding];
|
||||||
|
|
||||||
[NSURLConnection
|
|
||||||
sendAsynchronousRequest:request
|
|
||||||
queue:[[NSOperationQueue alloc] init]
|
|
||||||
completionHandler:^(NSURLResponse *response, NSData *data,
|
|
||||||
NSError *error) {
|
|
||||||
if (error == nil) {
|
|
||||||
if ([[NSJSONSerialization JSONObjectWithData:data
|
|
||||||
options:0
|
|
||||||
error:nil]
|
|
||||||
valueForKey:@"error_summary"] == nil) {
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
|
|
||||||
[SharedDeclerations
|
|
||||||
presentErrorAlert:
|
|
||||||
[NSError
|
|
||||||
errorWithDomain:DropboxErrorDomain
|
|
||||||
code:100
|
|
||||||
userInfo:@{
|
|
||||||
[[NSJSONSerialization
|
|
||||||
JSONObjectWithData:data
|
|
||||||
options:0
|
|
||||||
error:nil]
|
|
||||||
valueForKey:
|
|
||||||
DropboxErrorUserInfo] :
|
|
||||||
NSLocalizedDescriptionKey
|
|
||||||
}]
|
|
||||||
presenter:presenter
|
|
||||||
.navigationController
|
|
||||||
.topViewController];
|
|
||||||
}];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (error != nil) {
|
|
||||||
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
|
|
||||||
|
|
||||||
[SharedDeclerations
|
|
||||||
presentErrorAlert:error
|
|
||||||
presenter:presenter.navigationController
|
|
||||||
.topViewController];
|
|
||||||
|
|
||||||
}];
|
|
||||||
}
|
|
||||||
|
|
||||||
}];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user