jsonbody outsource

This commit is contained in:
Kilian Hofmann 2016-04-09 10:18:30 +02:00
parent c9924d6ec5
commit e7f105091b
2 changed files with 93 additions and 86 deletions

View File

@ -38,6 +38,8 @@
- (void)contentsOfPath:(NSString *)path
completion:(void (^)(NSArray *data))handler
presenter:(UIViewController *)presenter;
- (void)createFolderAtPath:(NSString *)path
presenter:(UIViewController *)presenter;
#pragma mark - Setup methods

View File

@ -184,10 +184,8 @@
}
options:0
error:nil];
NSString *temp =
[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
temp = [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
request.HTTPBody = [temp dataUsingEncoding:NSASCIIStringEncoding];
request.HTTPBody = [self createJSONExcapedHTTPBody:data];
[NSURLConnection
sendAsynchronousRequest:request
@ -300,12 +298,12 @@
}
options:0
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")
.precomposedStringWithCanonicalMapping];
@ -409,12 +407,12 @@
}
options:0
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")
.precomposedStringWithCanonicalMapping];
@ -483,6 +481,83 @@
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
- (void)setupDropboxWithHandler:(void (^)(void))handler
@ -586,82 +661,12 @@
}
}
- (void)createFolderAtPath:(NSString *)path
presenter:(UIViewController *)presenter
- (NSData *)createJSONExcapedHTTPBody:(NSData *)data
{
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 alloc] initWithData:data encoding:NSASCIIStringEncoding];
temp = [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
request.HTTPBody = [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];
}];
}
}];
return [temp dataUsingEncoding:NSASCIIStringEncoding];
}
@end