Skip to content
This repository
  • 3 commits
  • 13 files changed
  • 0 comments
  • 1 contributor
94  DIOSComment.m
@@ -46,37 +46,84 @@ + (void)commentGet:(NSDictionary *)comment
46 46
            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
47 47
            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
48 48
   
49  
-  [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]] 
50  
-                            parameters:nil 
51  
-                               success:success 
52  
-                               failure:failure];
  49
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]];
  50
+  
  51
+  if ([[DIOSSession sharedSession] signRequests]) {
  52
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  53
+                                                    method:@"GET"
  54
+                                                    params:comment
  55
+                                                   success:success
  56
+                                                   failure:failure];
  57
+  } else {
  58
+    [[DIOSSession sharedSession] getPath:path
  59
+                              parameters:nil
  60
+                                 success:success
  61
+                                 failure:failure];
  62
+  }
53 63
 }
54 64
 
55 65
 #pragma mark commentSave
56 66
 + (void)commentSave:(NSDictionary *)comment
57 67
             success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
58 68
             failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
59  
-  [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseComment] 
60  
-                             parameters:comment 
61  
-                                success:success 
62  
-                                failure:failure];
  69
+  
  70
+  NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseComment];
  71
+
  72
+  if ([[DIOSSession sharedSession] signRequests]) {
  73
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  74
+                                                    method:@"POST"
  75
+                                                    params:comment
  76
+                                                   success:success
  77
+                                                   failure:failure];
  78
+  } else {
  79
+    [[DIOSSession sharedSession] postPath:path
  80
+                               parameters:comment
  81
+                                  success:success
  82
+                                  failure:failure];
  83
+  }
63 84
 }
64 85
 
65 86
 #pragma mark commentUpdate
66 87
 + (void)commentUpdate:(NSDictionary *)comment
67 88
               success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
68 89
               failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
69  
-  [[DIOSSession sharedSession] putPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]] 
70  
-                            parameters:comment 
71  
-                               success:success
72  
-                               failure:failure];
  90
+  
  91
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]];
  92
+
  93
+  if ([[DIOSSession sharedSession] signRequests]) {
  94
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  95
+                                                    method:@"PUT"
  96
+                                                    params:comment
  97
+                                                   success:success
  98
+                                                   failure:failure];
  99
+  } else {
  100
+    [[DIOSSession sharedSession] putPath:path
  101
+                              parameters:comment
  102
+                                 success:success
  103
+                                 failure:failure];
  104
+  }
  105
+
73 106
 }
74 107
 
75 108
 #pragma mark CommentDelete
76 109
 + (void)commentDelete:(NSDictionary *)comment
77 110
               success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
78 111
               failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure; {
79  
-  [[DIOSSession sharedSession] deletePath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]] parameters:comment success:success failure:failure];
  112
+
  113
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]];
  114
+
  115
+  if ([[DIOSSession sharedSession] signRequests]) {
  116
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  117
+                                                    method:@"DELETE"
  118
+                                                    params:comment
  119
+                                                   success:success
  120
+                                                   failure:failure];
  121
+  } else {
  122
+    [[DIOSSession sharedSession] deletePath:path
  123
+                              parameters:comment
  124
+                                 success:success
  125
+                                 failure:failure];
  126
+  }
80 127
 }
81 128
 
82 129
 #pragma mark commentIndex
@@ -94,15 +141,26 @@ + (void)commentIndexWithPage:(NSString *)page
94 141
   [commentIndexDict setValue:parameteres forKey:@"parameters"];
95 142
   [commentIndexDict setValue:pageSize forKey:@"pagesize"];  
96 143
   [self commentIndex:commentIndexDict success:success failure:failure];
  144
+  [commentIndexDict release];
97 145
 }
98 146
 
99 147
 + (void)commentIndex:(NSDictionary *)params
100 148
              success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
101 149
              failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure; {
102  
-  
103  
-  [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseComment] 
104  
-                            parameters:params 
105  
-                               success:success 
106  
-                               failure:failure];
  150
+
  151
+  NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseComment];
  152
+
  153
+  if ([[DIOSSession sharedSession] signRequests]) {
  154
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  155
+                                                    method:@"GET"
  156
+                                                    params:params
  157
+                                                   success:success
  158
+                                                   failure:failure];
  159
+  } else {
  160
+    [[DIOSSession sharedSession] getPath:path
  161
+                                 parameters:params
  162
+                                    success:success
  163
+                                    failure:failure];
  164
+  }
107 165
 }
108 166
 @end
2  DIOSFile.h
@@ -40,7 +40,7 @@
40 40
 + (void) fileGet:(NSDictionary *)params
41 41
          success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
42 42
          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
43  
-+ (void) fileSave:(NSDictionary *)params
  43
++ (void)fileSave:(NSDictionary *)file
44 44
          success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
45 45
          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
46 46
 + (UIImageView *) getImageViewForFileImage:(NSDictionary *) file;
37  DIOSFile.m
@@ -44,25 +44,44 @@ + (void) fileGet:(NSDictionary *)params
44 44
          success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
45 45
          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
46 46
   
47  
-  [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseFile, [params objectForKey:@"fid"]] 
48  
-                            parameters:params 
49  
-                               success:success
50  
-                               failure:failure];
  47
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseFile, [params objectForKey:@"fid"]];
  48
+  if ([[DIOSSession sharedSession] signRequests]) {
  49
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  50
+                                                    method:@"GET"
  51
+                                                    params:params
  52
+                                                   success:success
  53
+                                                   failure:failure];
  54
+  } else {
  55
+    [[DIOSSession sharedSession] getPath:path
  56
+                              parameters:params
  57
+                                 success:success
  58
+                                 failure:failure];
  59
+  }
  60
+
51 61
 }
52 62
 
53 63
 + (void)fileSave:(NSDictionary *)file
54 64
          success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
55 65
          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
56  
-  
57  
-  [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseFile]
58  
-                             parameters:file
59  
-                                success:success 
60  
-                                failure:failure];
  66
+           NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseFile];
  67
+           if ([[DIOSSession sharedSession] signRequests]) {
  68
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  69
+                                                    method:@"POST"
  70
+                                                    params:file
  71
+                                                   success:success
  72
+                                                   failure:failure];
  73
+  } else {
  74
+    [[DIOSSession sharedSession] postPath:path
  75
+                               parameters:file
  76
+                                  success:success
  77
+                                  failure:failure];
  78
+  }
61 79
 }
62 80
 + (UIImageView *) getImageViewForFileImage:(NSDictionary *) file; {
63 81
   NSURL *url = [NSURL URLWithString:[file objectForKey:@"uri_full"]];
64 82
   UIImageView *remoteImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
65 83
   [remoteImage setImageWithURL:url];
  84
+  [remoteImage release];
66 85
   return remoteImage;
67 86
 }
68 87
 @end
10  DIOSNode.h
@@ -55,15 +55,15 @@
55 55
            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
56 56
            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
57 57
 
58  
-+ (void)nodeIndexWithPage:(NSString *)page fields:(NSString *)fields parameters:(NSArray *)parameteres pageSize:(NSString *)pageSize
  58
++ (void)nodeIndexWithPage:(NSString *)page
  59
+                   fields:(NSString *)fields
  60
+               parameters:(NSArray *)parameteres
  61
+                 pageSize:(NSString *)pageSize
59 62
                   success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
60  
-                  failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
  63
+                  failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure ;
61 64
 
62 65
 + (void)nodeIndex:(NSDictionary *)params
63 66
           success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
64 67
           failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
65 68
 
66  
-+ (void)nodeAttachFile:(NSDictionary *)params
67  
-               success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
68  
-               failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
69 69
 @end
112  DIOSNode.m
@@ -43,11 +43,21 @@ @implementation DIOSNode
43 43
 + (void)nodeGet:(NSDictionary *)node
44 44
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
45 45
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
  46
+
  47
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]];
46 48
   
47  
-  [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]] 
48  
-                            parameters:nil 
49  
-                               success:success 
50  
-                               failure:failure];
  49
+  if ([[DIOSSession sharedSession] signRequests]) {
  50
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  51
+                                    method:@"GET"
  52
+                                    params:node
  53
+                                   success:success
  54
+                                   failure:failure];
  55
+  } else {
  56
+    [[DIOSSession sharedSession] getPath:path
  57
+                              parameters:nil
  58
+                                 success:success
  59
+                                 failure:failure];
  60
+  }
51 61
 }
52 62
 
53 63
 #pragma mark nodeSave
@@ -55,10 +65,21 @@ + (void)nodeSave:(NSDictionary *)node
55 65
                                success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
56 66
                                failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
57 67
 
58  
-  [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseNode] 
59  
-                             parameters:node
60  
-                                success:success 
61  
-                                failure:failure];
  68
+  NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseNode];
  69
+  
  70
+  if ([[DIOSSession sharedSession] signRequests]) {
  71
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  72
+                                                    method:@"POST"
  73
+                                                    params:node
  74
+                                                   success:success
  75
+                                                   failure:failure];
  76
+  }
  77
+  else {
  78
+    [[DIOSSession sharedSession] postPath:path
  79
+                               parameters:node
  80
+                                  success:success 
  81
+                                  failure:failure];
  82
+  }
62 83
 }
63 84
 
64 85
 #pragma mark nodeUpdate
@@ -66,10 +87,22 @@ + (void)nodeUpdate:(NSDictionary *)node
66 87
            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
67 88
            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
68 89
   
69  
-  [[DIOSSession sharedSession] putPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]] 
70  
-                            parameters:node 
71  
-                               success:success
72  
-                               failure:failure];
  90
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]];
  91
+  
  92
+  if ([[DIOSSession sharedSession] signRequests]) {
  93
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  94
+                                                    method:@"PUT"
  95
+                                                    params:node
  96
+                                                   success:success
  97
+                                                   failure:failure];
  98
+  }
  99
+  else {
  100
+    [[DIOSSession sharedSession] putPath:path
  101
+                              parameters:node
  102
+                                 success:success
  103
+                                 failure:failure];
  104
+  }
  105
+
73 106
 }
74 107
 
75 108
 #pragma mark nodeDelete
@@ -77,14 +110,27 @@ + (void)nodeDelete:(NSDictionary *)node
77 110
            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
78 111
            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
79 112
   
80  
-  [[DIOSSession sharedSession] deletePath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]] 
81  
-                               parameters:node 
82  
-                                  success:success 
83  
-                                  failure:failure];
  113
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]];
  114
+  
  115
+  if ([[DIOSSession sharedSession] signRequests]) {
  116
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  117
+                                                    method:@"DELETE"
  118
+                                                    params:node
  119
+                                                   success:success
  120
+                                                   failure:failure];
  121
+  } else {
  122
+    [[DIOSSession sharedSession] deletePath:path
  123
+                                 parameters:node
  124
+                                    success:success
  125
+                                    failure:failure];
  126
+  }
84 127
 }
85 128
 #pragma mark nodeIndex
86 129
 //Simpler method if you didnt want to build the params :)
87  
-+ (void)nodeIndexWithPage:(NSString *)page fields:(NSString *)fields parameters:(NSArray *)parameteres pageSize:(NSString *)pageSize
  130
++ (void)nodeIndexWithPage:(NSString *)page
  131
+                   fields:(NSString *)fields
  132
+               parameters:(NSArray *)parameteres
  133
+                 pageSize:(NSString *)pageSize
88 134
                   success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
89 135
                   failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
90 136
   NSMutableDictionary *nodeIndexDict = [NSMutableDictionary new];
@@ -93,29 +139,27 @@ + (void)nodeIndexWithPage:(NSString *)page fields:(NSString *)fields parameters:
93 139
   [nodeIndexDict setValue:parameteres forKey:@"parameters"];
94 140
   [nodeIndexDict setValue:pageSize forKey:@"pagesize"];  
95 141
   [self nodeIndex:nodeIndexDict success:success failure:failure];
  142
+  [nodeIndexDict release];
96 143
 }
97 144
 
98 145
 + (void)nodeIndex:(NSDictionary *)params
99 146
           success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
100 147
           failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
101  
-  
102  
-  [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseNode] parameters:params success:success failure:failure];
103  
-}
104 148
 
105  
-#pragma mark nodeAttachFile
106  
-+ (void)nodeAttachFile:(NSDictionary *)params
107  
-               success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
108  
-               failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
109  
-  NSMutableURLRequest *request = [[DIOSSession sharedSession] multipartFormRequestWithMethod:@"POST" path:[NSString stringWithFormat:@"%@/%@/%@/attach_file?field_name=%@", kDiosEndpoint, kDiosBaseNode, [params objectForKey:@"nid"], [params objectForKey:@"field_name"]] parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
110  
-    [formData appendPartWithFileData:[params objectForKey:@"fileData"] name:[params objectForKey:@"name"] fileName:[params objectForKey:@"fileName"] mimeType:[params objectForKey:@"mimetype"]];
111  
-  }];
112  
-  
113  
-  AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
114  
-  [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
115  
-   NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
116  
-  }];
  149
+  NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseNode];
117 150
   
118  
-  [operation setCompletionBlockWithSuccess:success failure:failure];
119  
-  [operation start];
  151
+  if ([[DIOSSession sharedSession] signRequests]) {
  152
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  153
+                                                    method:@"DELETE"
  154
+                                                    params:params
  155
+                                                   success:success
  156
+                                                   failure:failure];
  157
+  } else {
  158
+    [[DIOSSession sharedSession] getPath:path
  159
+                                 parameters:params
  160
+                                    success:success
  161
+                                    failure:failure];
  162
+  }
120 163
 }
  164
+
121 165
 @end
34  DIOSSession.h
@@ -41,9 +41,43 @@
41 41
 
42 42
 @interface DIOSSession : AFHTTPClient {
43 43
   NSDictionary *user;
  44
+  NSMutableDictionary *accessTokens;
  45
+  NSString *consumerKey;
  46
+  NSString *consumerSecret;
  47
+  NSString *tokenIdentifier;
  48
+  NSString *tokenSecret;
44 49
 }
45 50
 @property (strong, nonatomic) NSDictionary *user;
  51
+@property (nonatomic, retain) NSMutableDictionary *accessTokens;
  52
+@property (nonatomic) BOOL signRequests;
  53
+@property (nonatomic) BOOL threeLegged;
  54
+@property (nonatomic, copy) NSString *realm;
  55
+@property (nonatomic, retain) NSString *consumerKey;
  56
+@property (nonatomic, retain) NSString *consumerSecret;
  57
+@property (nonatomic, retain) NSString *tokenIdentifier;
  58
+@property (nonatomic, retain) NSString *tokenSecret;
46 59
 + (DIOSSession *)sharedSession;
  60
++ (DIOSSession *)sharedSessionWithURL:(NSString*)url;
  61
++ (DIOSSession *)sharedOauthSessionWithURL:(NSString*)url consumerKey:(NSString *)aConsumerKey secret:(NSString *)aConsumerSecret;
  62
++ (void) getRequestTokensWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  63
+                             failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
  64
++ (void) getAccessTokensWithRequestTokens:(NSDictionary *)requesTokens
  65
+                                  success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  66
+                                  failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
47 67
 
  68
+- (void) sendSignedRequestWithPath:(NSString*)path
  69
+                      method:(NSString*)method
  70
+                      params:(NSDictionary*)params
  71
+                     success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  72
+                     failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
  73
+
  74
+- (void) addHeaderValue:(NSString*)value forKey:(NSString*)key;
  75
+- (NSURLRequest *) signedRequestWithMethod:(NSString *)method
  76
+                                      path:(NSString *)path
  77
+                                parameters:(NSDictionary *)parameters;
  78
+- (id) initWithBaseURL:(NSURL *)url consumerKey:(NSString *)consumerKey secret:(NSString *)consumerSecret;
  79
+
  80
+- (void) setAccessToken:(NSString *)accessToken secret:(NSString *)secret;
  81
+- (void) setConsumerKey:(NSString *)consumerKey secret:(NSString *)secret;
48 82
 @end
49 83
 
327  DIOSSession.m
@@ -38,19 +38,94 @@
38 38
 #import "DIOSSession.h"
39 39
 #import "AFJSONRequestOperation.h"
40 40
 #import "AFPropertyListRequestOperation.h"
  41
+#include <sys/time.h>
  42
+#import <CommonCrypto/CommonHMAC.h>
  43
+static NSString* Base64EncodedStringFromData(NSData *data);
  44
+static NSString* URLEncodeString(NSString *string);
  45
+static const NSString *kOAuthSignatureMethodKey = @"oauth_signature_method";
  46
+static const NSString *kOAuthVersionKey = @"oauth_version";
  47
+static const NSString *kOAuthConsumerKey = @"oauth_consumer_key";
  48
+static const NSString *kOAuthTokenIdentifier = @"oauth_token";
  49
+static const NSString *kOAuthSignatureKey = @"oauth_signature";
  50
+
  51
+static const NSString *kOAuthSignatureTypeHMAC_SHA1 = @"HMAC-SHA1";
  52
+static const NSString *kOAuthVersion1_0 = @"1.0";
  53
+static dispatch_once_t once;
  54
+static DIOSSession *sharedSession;
  55
+@interface DIOSSession()
  56
+
  57
+- (id) initWithBaseURL:(NSURL *)url;
  58
+- (void) addGeneratedTimestampAndNonceInto:(NSMutableDictionary *)dictionary;
  59
+
  60
+- (NSString *) authorizationHeaderValueForRequest:(NSURLRequest *)request;
  61
+@end
41 62
 
42 63
 @implementation DIOSSession
43  
-@synthesize user;
  64
+@synthesize user, accessTokens, consumerKey, consumerSecret, tokenIdentifier, tokenSecret,
  65
+realm, signRequests, threeLegged;
  66
+
44 67
 + (DIOSSession *)sharedSession {
45  
-  static dispatch_once_t once;
46  
-  static DIOSSession *sharedSession;
47  
-  dispatch_once(&once, ^ { 
  68
+  dispatch_once(&once, ^ {
48 69
     sharedSession = [[self alloc] initWithBaseURL:[NSURL URLWithString:kDiosBaseUrl]];
49 70
     [sharedSession setParameterEncoding:AFJSONParameterEncoding];
50 71
   });
51 72
   return sharedSession;
52 73
 }
53 74
 
  75
++ (DIOSSession *)sharedSessionWithURL:(NSString*)url {
  76
+  dispatch_once(&once, ^ {
  77
+    sharedSession = [[self alloc] initWithBaseURL:[NSURL URLWithString:url]];
  78
+    [sharedSession setParameterEncoding:AFJSONParameterEncoding];
  79
+  });
  80
+  [sharedSession setBaseURL:[NSURL URLWithString:url]];
  81
+  return sharedSession;
  82
+}
  83
+
  84
++ (DIOSSession *)sharedOauthSessionWithURL:(NSString*)url consumerKey:(NSString *)aConsumerKey secret:(NSString *)aConsumerSecret {
  85
+  dispatch_once(&once, ^ {
  86
+    sharedSession = [[self alloc] initWithBaseURL:[NSURL URLWithString:url] consumerKey:aConsumerKey secret:aConsumerSecret];
  87
+    [sharedSession setParameterEncoding:AFJSONParameterEncoding];
  88
+  });
  89
+  [sharedSession setBaseURL:[NSURL URLWithString:url]];
  90
+  return sharedSession;
  91
+}
  92
+
  93
++ (void) getRequestTokensWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  94
+                             failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
  95
+  
  96
+  DIOSSession *client = [[DIOSSession alloc] initWithBaseURL:[[DIOSSession sharedSession] baseURL]];
  97
+  [client setConsumerKey:[[DIOSSession sharedSession] consumerKey] secret:[[DIOSSession sharedSession] consumerSecret]];
  98
+  [client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
  99
+  [client setDefaultHeader:@"Accept" value:@"text/html"];
  100
+  [client setDefaultHeader:@"Content-Type" value:@"text/html"];
  101
+  NSMutableDictionary *params = [NSMutableDictionary new];
  102
+  [params setObject:[[DIOSSession sharedSession] consumerKey] forKey:kOAuthConsumerKey];
  103
+  [params setObject:[[DIOSSession sharedSession] consumerSecret] forKey:kOAuthTokenIdentifier];
  104
+  [client sendSignedRequestWithPath:@"/oauth/request_token" method:@"GET" params:params success:success failure:failure];
  105
+}
  106
++ (void) getAccessTokensWithRequestTokens:(NSDictionary *)requestTokens
  107
+                                  success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  108
+                                  failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
  109
+
  110
+  DIOSSession *client = [[DIOSSession alloc] initWithBaseURL:[[DIOSSession sharedSession] baseURL]];
  111
+  [client setConsumerKey:[[DIOSSession sharedSession] consumerKey] secret:[[DIOSSession sharedSession] consumerSecret]];
  112
+  [client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
  113
+  [client setDefaultHeader:@"Accept" value:@"text/html"];
  114
+  [client setDefaultHeader:@"Content-Type" value:@"text/html"];
  115
+  [client setAccessToken:[requestTokens objectForKey:@"oauth_token"] secret:[requestTokens objectForKey:@"oauth_token_secret"]];
  116
+  [client sendSignedRequestWithPath:@"/oauth/access_token" method:@"GET" params:requestTokens success:success failure:failure];
  117
+}
  118
+- (void) sendSignedRequestWithPath:(NSString*)path
  119
+                            method:(NSString*)method
  120
+                            params:(NSDictionary*)params
  121
+                           success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  122
+                           failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
  123
+  NSURLRequest *request = [self signedRequestWithMethod:method path:path parameters:params];
  124
+
  125
+  AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  126
+  [self enqueueHTTPRequestOperation:operation];
  127
+}
  128
+
54 129
 - (id)initWithBaseURL:(NSURL *)url {
55 130
   self = [super initWithBaseURL:url];
56 131
   if (!self) {
@@ -64,4 +139,248 @@ - (id)initWithBaseURL:(NSURL *)url {
64 139
 	
65 140
   return self;
66 141
 }
  142
+
  143
+- (id) initWithBaseURL:(NSURL *)url consumerKey:(NSString *)aConsumerKey secret:(NSString *)aConsumerSecret {
  144
+  self = [super initWithBaseURL:url];
  145
+
  146
+  if (self) {
  147
+    self.signRequests = YES;
  148
+    self.consumerKey = aConsumerKey;
  149
+    self.consumerSecret = aConsumerSecret;
  150
+
  151
+    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
  152
+    // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
  153
+    [self setDefaultHeader:@"Accept" value:@"application/json"];
  154
+    [self setDefaultHeader:@"Content-Type" value:@"application/json"];
  155
+  }
  156
+
  157
+  return self;
  158
+}
  159
+- (NSURLRequest *) signedRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
  160
+  NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
  161
+
  162
+  NSString *authorizationHeader = [self authorizationHeaderValueForRequest:request];
  163
+  [request setValue:authorizationHeader forHTTPHeaderField:@"Authorization"];
  164
+
  165
+  return request;
  166
+}
  167
+- (NSMutableURLRequest *) requestWithMethod:(NSString *)method
  168
+                                       path:(NSString *)path
  169
+                                 parameters:(NSDictionary *)parameters {
  170
+
  171
+  NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
  172
+
  173
+  if (self.signRequests) {
  174
+    NSString *authorizationHeader = [self authorizationHeaderValueForRequest:request];
  175
+    [request setValue:authorizationHeader forHTTPHeaderField:@"Authorization"];
  176
+  }
  177
+
  178
+  return request;
  179
+}
  180
+
  181
+- (NSURLRequest *) unsignedRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
  182
+  NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
  183
+
  184
+  return request;
  185
+}
  186
+
  187
+- (void) setAccessToken:(NSString *)accessToken secret:(NSString *)secret {
  188
+  self.tokenIdentifier = accessToken;
  189
+  self.tokenSecret = secret;
  190
+  self.threeLegged = YES;
  191
+}
  192
+
  193
+- (void) setConsumerKey:(NSString *)aConsumerKey secret:(NSString *)secret {
  194
+  self.consumerKey = aConsumerKey;
  195
+  self.consumerSecret = secret;
  196
+}
  197
+- (void) addHeaderValue:(NSString*)value forKey:(NSString*)key {
  198
+  [self setDefaultHeader:key value:value];
  199
+}
  200
+
  201
+- (NSMutableDictionary *) mutableDictionaryWithOAuthInitialData {
  202
+  NSMutableDictionary *result = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  203
+                                 kOAuthSignatureTypeHMAC_SHA1, kOAuthSignatureMethodKey,
  204
+                                 kOAuthVersion1_0, kOAuthVersionKey,
  205
+                                 nil];
  206
+
  207
+  if (self.consumerKey) [result setObject:self.consumerKey forKey:kOAuthConsumerKey];
  208
+  if (self.tokenIdentifier) [result setObject:self.tokenIdentifier forKey:kOAuthTokenIdentifier];
  209
+
  210
+  [self addGeneratedTimestampAndNonceInto:result];
  211
+
  212
+  return  result;
  213
+}
  214
+
  215
+- (NSString *) stringWithOAuthParameters:(NSMutableDictionary *)oauthParams requestParameters:(NSDictionary *)parameters {
  216
+  NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:oauthParams];
  217
+  [params addEntriesFromDictionary:parameters];
  218
+
  219
+  // sorting parameters
  220
+  NSArray *sortedKeys = [[params allKeys] sortedArrayUsingComparator:^NSComparisonResult(NSString *key1, NSString *key2) {
  221
+    NSComparisonResult result = [key1 compare:key2 options:NSLiteralSearch];
  222
+    if (result == NSOrderedSame)
  223
+      result = [[params objectForKey:key1] compare:[params objectForKey:key2] options:NSLiteralSearch];
  224
+
  225
+    return result;
  226
+  }];
  227
+
  228
+  // join keys and values with =
  229
+  NSMutableArray *longListOfParameters = [NSMutableArray arrayWithCapacity:[sortedKeys count]];
  230
+  [sortedKeys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) {
  231
+    [longListOfParameters addObject:[NSString stringWithFormat:@"%@=%@", key, [params objectForKey:key]]];
  232
+  }];
  233
+
  234
+  // join components with &
  235
+  return [longListOfParameters componentsJoinedByString:@"&"];
  236
+}
  237
+
  238
+- (NSString *) authorizationHeaderValueForRequest:(NSURLRequest *)request {
  239
+  NSURL *url = request.URL;
  240
+  NSString *fixedURL = [self baseURLforAddress:url];
  241
+  NSMutableDictionary *oauthParams = [self mutableDictionaryWithOAuthInitialData];
  242
+  // adding oauth_ extra params to the header
  243
+  NSArray *parameterComponents = [[request.URL query] componentsSeparatedByString:@"&"];
  244
+  NSMutableDictionary *parameters = [NSMutableDictionary new];
  245
+  for(NSString *component in parameterComponents) {
  246
+    NSArray *subComponents = [component componentsSeparatedByString:@"="];
  247
+    if ([subComponents count] == 2) {
  248
+      [parameters setObject:[subComponents objectAtIndex:1] forKey:[subComponents objectAtIndex:0]];
  249
+    }
  250
+  }
  251
+  NSData *body = [request HTTPBody];
  252
+  NSString *htttpBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
  253
+  NSArray *subComponents = [htttpBody componentsSeparatedByString:@"="];
  254
+  if ([subComponents count] == 2) {
  255
+    [parameters setObject:[subComponents objectAtIndex:1] forKey:[subComponents objectAtIndex:0]];
  256
+  }
  257
+
  258
+  NSString *allParameters = [self stringWithOAuthParameters:oauthParams requestParameters:parameters];
  259
+  // adding HTTP method and URL
  260
+  NSString *signatureBaseString = [NSString stringWithFormat:@"%@&%@&%@", [request.HTTPMethod uppercaseString], URLEncodeString(fixedURL), URLEncodeString(allParameters)];
  261
+
  262
+  NSString *signature = [self signatureForBaseString:signatureBaseString];
  263
+
  264
+  // add to OAuth params
  265
+  [oauthParams setObject:signature forKey:kOAuthSignatureKey];
  266
+
  267
+  // build OAuth Authorization Header
  268
+  NSMutableArray *headerParams = [NSMutableArray arrayWithCapacity:[oauthParams count]];
  269
+  [oauthParams enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
  270
+    [headerParams addObject:[NSString stringWithFormat:@"%@=\"%@\"", key, URLEncodeString(obj)]];
  271
+  }];
  272
+
  273
+  // let's use the base URL if a realm was not set
  274
+  NSString *oauthRealm = self.realm;
  275
+  if (!oauthRealm) oauthRealm = [self baseURLforAddress:[self baseURL]];
  276
+
  277
+  NSString *result = [NSString stringWithFormat:@"OAuth realm=\"%@\",%@", oauthRealm, [headerParams componentsJoinedByString:@","]];
  278
+
  279
+  return result;
  280
+}
  281
+
  282
+- (void)addGeneratedTimestampAndNonceInto:(NSMutableDictionary *)dictionary {
  283
+  NSUInteger epochTime = (NSUInteger)[[NSDate date] timeIntervalSince1970];
  284
+  NSString *timestamp = [NSString stringWithFormat:@"%d", epochTime];
  285
+  CFUUIDRef theUUID = CFUUIDCreate(NULL);
  286
+  CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  287
+  NSString *nonce = (NSString *)string;
  288
+  CFRelease(theUUID);
  289
+
  290
+  [dictionary setObject:nonce forKey:@"oauth_nonce"];
  291
+  [dictionary setObject:timestamp forKey:@"oauth_timestamp"];
  292
+}
  293
+
  294
+- (NSString *) signatureForBaseString:(NSString *)baseString {
  295
+  NSString *key = [NSString stringWithFormat:@"%@&%@", self.consumerSecret != nil ? URLEncodeString(self.consumerSecret) : @"", self.tokenSecret != nil ? URLEncodeString(self.tokenSecret) : @""];
  296
+  const char *keyBytes = [key cStringUsingEncoding:NSUTF8StringEncoding];
  297
+  const char *baseStringBytes = [baseString cStringUsingEncoding:NSUTF8StringEncoding];
  298
+  unsigned char digestBytes[CC_SHA1_DIGEST_LENGTH];
  299
+
  300
+  CCHmac(kCCHmacAlgSHA1, keyBytes, strlen(keyBytes), baseStringBytes, strlen(baseStringBytes), digestBytes);
  301
+
  302
+  NSData *digestData = [NSData dataWithBytes:digestBytes length:CC_SHA1_DIGEST_LENGTH];
  303
+  return Base64EncodedStringFromData(digestData);
  304
+}
  305
+
  306
+- (NSString *) baseURLforAddress:(NSURL *)url {
  307
+  NSAssert1([url host] != nil, @"URL host missing: %@", [url absoluteString]);
  308
+
  309
+  // Port need only be present if it's not the default
  310
+  NSString *hostString;
  311
+  if (([url port] == nil)
  312
+      || ([[[url scheme] lowercaseString] isEqualToString:@"http"] && ([[url port] integerValue] == 80))
  313
+      || ([[[url scheme] lowercaseString] isEqualToString:@"https"] && ([[url port] integerValue] == 443))) {
  314
+    hostString = [[url host] lowercaseString];
  315
+  } else {
  316
+    hostString = [NSString stringWithFormat:@"%@:%@", [[url host] lowercaseString], [url port]];
  317
+  }
  318
+
  319
+  return [NSString stringWithFormat:@"%@://%@%@", [[url scheme] lowercaseString], hostString, [[url absoluteURL] path]];
  320
+}
  321
+
  322
+//
  323
+//  The function below is based on
  324
+//
  325
+//  NSString+URLEncode.h
  326
+//
  327
+//  Created by Scott James Remnant on 6/1/11.
  328
+//  Copyright 2011 Scott James Remnant <scott@netsplit.com>. All rights reserved.
  329
+//
  330
+static NSString *URLEncodeString(NSString *string) {
  331
+  // See http://en.wikipedia.org/wiki/Percent-encoding and RFC3986
  332
+  // Hyphen, Period, Understore & Tilde are expressly legal
  333
+  const CFStringRef legalURLCharactersToBeEscaped = CFSTR(":/=,!$&'()*+;[]@#?");
  334
+
  335
+  return ( NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, ( CFStringRef)string, NULL, legalURLCharactersToBeEscaped, kCFStringEncodingUTF8);
  336
+}
67 337
 @end
  338
+// The function below was inspired on
  339
+//
  340
+// AFOAuth2Client.m
  341
+//
  342
+// Copyright (c) 2011 Mattt Thompson (http://mattt.me/)
  343
+//
  344
+// Permission is hereby granted, free of charge, to any person obtaining a copy
  345
+// of this software and associated documentation files (the "Software"), to deal
  346
+// in the Software without restriction, including without limitation the rights
  347
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  348
+// copies of the Software, and to permit persons to whom the Software is
  349
+// furnished to do so, subject to the following conditions:
  350
+//
  351
+// The above copyright notice and this permission notice shall be included in
  352
+// all copies or substantial portions of the Software.
  353
+//
  354
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  355
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  356
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  357
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  358
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  359
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  360
+// THE SOFTWARE.
  361
+//
  362
+static NSString * Base64EncodedStringFromData(NSData *data) {
  363
+  NSUInteger length = [data length];
  364
+  NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
  365
+
  366
+  uint8_t *input = (uint8_t *)[data bytes];
  367
+  uint8_t *output = (uint8_t *)[mutableData mutableBytes];
  368
+
  369
+  for (NSUInteger i = 0; i < length; i += 3) {
  370
+    NSUInteger value = 0;
  371
+    for (NSUInteger j = i; j < (i + 3); j++) {
  372
+      value <<= 8;
  373
+      if (j < length) value |= (0xFF & input[j]);
  374
+    }
  375
+
  376
+    static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  377
+
  378
+    NSUInteger idx = (i / 3) * 4;
  379
+    output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
  380
+    output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
  381
+    output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6)  & 0x3F] : '=';
  382
+    output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0)  & 0x3F] : '=';
  383
+  }
  384
+
  385
+  return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
  386
+}
60  DIOSTaxonomy.m
@@ -50,15 +50,27 @@ + (void)getTreeWithVid:(NSString *)vid
50 50
   [params setValue:parent forKey:@"parent"];
51 51
   [params setValue:maxDepth forKey:@"max_depth"];
52 52
   [self getTreeWithParams:params success:success failure:failure];
  53
+  [params release];
53 54
 }
54 55
 + (void)getTreeWithParams:(NSDictionary *)params
55 56
                   success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
56 57
                   failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
  58
+
  59
+  NSString *path = [NSString stringWithFormat:@"%@/%@/getTree", kDiosEndpoint, kDiosBaseTaxonmyVocabulary];
57 60
   
58  
-  [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@/getTree", kDiosEndpoint, kDiosBaseTaxonmyVocabulary] 
59  
-                             parameters:params 
60  
-                                success:success
61  
-                                failure:failure];
  61
+  if ([[DIOSSession sharedSession] signRequests]) {
  62
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  63
+                                                    method:@"POST"
  64
+                                                    params:params
  65
+                                                   success:success
  66
+                                                   failure:failure];
  67
+  } else {
  68
+    [[DIOSSession sharedSession] postPath:path
  69
+                               parameters:params
  70
+                                  success:success
  71
+                                  failure:failure];
  72
+  }
  73
+
62 74
 }
63 75
 + (void)selectNodesWithTid:(NSString *)tid
64 76
                   andLimit:(NSString *)limit 
@@ -73,22 +85,44 @@ + (void)selectNodesWithTid:(NSString *)tid
73 85
   [params setValue:pager forKey:@"pager"];
74 86
   [params setValue:order forKey:@"prder"];
75 87
   [self selectNodesWithParams:params success:success failure:failure];
  88
+  [params release];
76 89
 }
77 90
 + (void)selectNodesWithParams:(NSDictionary *)params
78 91
                       success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
79 92
                       failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
80  
-  
81  
-  [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@/selectNodes", kDiosEndpoint, kDiosBaseTaxonmyTerm] 
82  
-                             parameters:params 
83  
-                                success:success 
84  
-                                failure:failure];
  93
+
  94
+  NSString *path = [NSString stringWithFormat:@"%@/%@/selectNodes", kDiosEndpoint, kDiosBaseTaxonmyTerm];
  95
+
  96
+  if ([[DIOSSession sharedSession] signRequests]) {
  97
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  98
+                                                    method:@"POST"
  99
+                                                    params:params
  100
+                                                   success:success
  101
+                                                   failure:failure];
  102
+  } else {
  103
+    [[DIOSSession sharedSession] postPath:path
  104
+                               parameters:params
  105
+                                  success:success
  106
+                                  failure:failure];
  107
+  }
85 108
 }
86 109
 + (void)getTermWithTid:(NSString *)tid
87 110
                success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
88 111
                failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
89  
-  [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseTaxonmyTerm, tid] 
90  
-                            parameters:nil 
91  
-                               success:success 
92  
-                               failure:failure];
  112
+
  113
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseTaxonmyTerm, tid];
  114
+
  115
+  if ([[DIOSSession sharedSession] signRequests]) {
  116
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  117
+                                                    method:@"GET"
  118
+                                                    params:nil
  119
+                                                   success:success
  120
+                                                   failure:failure];
  121
+  } else {
  122
+    [[DIOSSession sharedSession] getPath:path
  123
+                               parameters:nil
  124
+                                  success:success
  125
+                                  failure:failure];
  126
+  }
93 127
 }
94 128
 @end
167  DIOSUser.m
@@ -44,11 +44,22 @@ @implementation DIOSUser
44 44
 + (void)userGet:(NSDictionary *)user
45 45
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
46 46
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
47  
-  
48  
-  [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]] 
49  
-                            parameters:nil 
50  
-                               success:success 
51  
-                               failure:failure];
  47
+
  48
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]];
  49
+
  50
+  if ([[DIOSSession sharedSession] signRequests]) {
  51
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  52
+                                                    method:@"GET"
  53
+                                                    params:user
  54
+                                                   success:success
  55
+                                                   failure:failure];
  56
+  }
  57
+  else {
  58
+    [[DIOSSession sharedSession] getPath:path
  59
+                               parameters:user
  60
+                                  success:success
  61
+                                  failure:failure];
  62
+  }
52 63
 }
53 64
 
54 65
 
@@ -56,11 +67,22 @@ + (void)userGet:(NSDictionary *)user
56 67
 + (void)userSave:(NSDictionary *)user
57 68
          success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
58 69
          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
59  
-  
60  
-  [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseUser] 
61  
-                             parameters:user 
62  
-                                success:success 
63  
-                                failure:failure];
  70
+
  71
+  NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseUser];
  72
+
  73
+  if ([[DIOSSession sharedSession] signRequests]) {
  74
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  75
+                                                    method:@"POST"
  76
+                                                    params:user
  77
+                                                   success:success
  78
+                                                   failure:failure];
  79
+  }
  80
+  else {
  81
+    [[DIOSSession sharedSession] postPath:path
  82
+                              parameters:user
  83
+                                 success:success
  84
+                                 failure:failure];
  85
+  }
64 86
 }
65 87
 
66 88
 #pragma mark userRegister
@@ -68,21 +90,43 @@ + (void)userRegister:(NSDictionary *)user
68 90
          success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
69 91
          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
70 92
   
71  
-  [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@/register", kDiosEndpoint, kDiosBaseUser] 
72  
-                             parameters:user 
73  
-                                success:success 
74  
-                                failure:failure];
  93
+  NSString *path = [NSString stringWithFormat:@"%@/%@/register", kDiosEndpoint, kDiosBaseUser];
  94
+
  95
+  if ([[DIOSSession sharedSession] signRequests]) {
  96
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  97
+                                                    method:@"POST"
  98
+                                                    params:user
  99
+                                                   success:success
  100
+                                                   failure:failure];
  101
+  }
  102
+  else {
  103
+    [[DIOSSession sharedSession] postPath:path
  104
+                               parameters:user
  105
+                                  success:success
  106
+                                  failure:failure];
  107
+  }
75 108
 }
76 109
 
77 110
 #pragma mark userUpdate
78 111
 + (void)userUpdate:(NSDictionary *)user
79 112
            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
80 113
            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
81  
-  
82  
-  [[DIOSSession sharedSession] putPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]] 
83  
-                            parameters:user 
84  
-                               success:success 
85  
-                               failure:failure];
  114
+
  115
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]];
  116
+
  117
+  if ([[DIOSSession sharedSession] signRequests]) {
  118
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  119
+                                                    method:@"PUT"
  120
+                                                    params:user
  121
+                                                   success:success
  122
+                                                   failure:failure];
  123
+  }
  124
+  else {
  125
+    [[DIOSSession sharedSession] putPath:path
  126
+                              parameters:user
  127
+                                 success:success
  128
+                                 failure:failure];
  129
+  }
86 130
 }
87 131
 
88 132
 #pragma mark UserDelete
@@ -90,10 +134,21 @@ + (void)userDelete:(NSDictionary *)user
90 134
            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
91 135
            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
92 136
   
93  
-  [[DIOSSession sharedSession] deletePath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]] 
94  
-                               parameters:user 
95  
-                                  success:success
96  
-                                  failure:failure];
  137
+  NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]];
  138
+
  139
+  if ([[DIOSSession sharedSession] signRequests]) {
  140
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  141
+                                                    method:@"DELETE"
  142
+                                                    params:user
  143
+                                                   success:success
  144
+                                                   failure:failure];
  145
+  }
  146
+  else {
  147
+    [[DIOSSession sharedSession] deletePath:path
  148
+                                 parameters:user
  149
+                                    success:success
  150
+                                    failure:failure];
  151
+  }
97 152
 }
98 153
 
99 154
 
@@ -111,15 +166,28 @@ + (void)userIndexWithPage:(NSString *)page
111 166
   [userIndexDict setValue:parameteres forKey:@"parameters"];
112 167
   [userIndexDict setValue:pageSize forKey:@"pagesize"];  
113 168
   [self userIndex:userIndexDict success:success failure:failure];
  169
+  [userIndexDict release];
114 170
 }
115 171
 
116 172
 + (void)userIndex:(NSDictionary *)params
117 173
           success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
118 174
           failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
119  
-  [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseUser] 
120  
-                            parameters:params 
121  
-                               success:success 
122  
-                               failure:failure];
  175
+
  176
+  NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseUser];
  177
+
  178
+  if ([[DIOSSession sharedSession] signRequests]) {
  179
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  180
+                                                    method:@"GET"
  181
+                                                    params:params
  182
+                                                   success:success
  183
+                                                   failure:failure];
  184
+  }
  185
+  else {
  186
+    [[DIOSSession sharedSession] getPath:path
  187
+                              parameters:params
  188
+                                 success:success
  189
+                                 failure:failure];
  190
+  }
123 191
 }
124 192
 
125 193
 #pragma mark userLogin
@@ -128,23 +196,50 @@ + (void)userLoginWithUsername:(NSString *)username andPassword:(NSString *)passw
128 196
                       failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
129 197
   
130 198
   NSDictionary *params = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:username, password, nil] forKeys:[NSArray arrayWithObjects:@"username", @"password", nil]];
131  
-  [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@/login", kDiosEndpoint, kDiosBaseUser] 
132  
-                             parameters:params 
133  
-                                success:success 
134  
-                                failure:failure];
  199
+
  200
+  NSString *path = [NSString stringWithFormat:@"%@/%@/login", kDiosEndpoint, kDiosBaseUser];
  201
+
  202
+  if ([[DIOSSession sharedSession] signRequests]) {
  203
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  204
+                                                    method:@"POST"
  205
+                                                    params:params
  206
+                                                   success:success
  207
+                                                   failure:failure];
  208
+  }
  209
+  else {
  210
+    [[DIOSSession sharedSession] postPath:path
  211
+                               parameters:params
  212
+                                  success:success
  213
+                                  failure:failure];
  214
+  }
135 215
 }
136 216
 + (void)userLogin:(NSDictionary *)user
137 217
           success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
138 218
           failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
139  
-  [self userLoginWithUsername:[user objectForKey:@"name"] andPassword:[user objectForKey:@"pass"] success:success failure:failure];
  219
+  [self userLoginWithUsername:[user objectForKey:@"name"]
  220
+                  andPassword:[user objectForKey:@"pass"]
  221
+                      success:success
  222
+                      failure:failure];
140 223
 }
141 224
 
142 225
 #pragma mark userLogout
143 226
 + (void)userLogoutWithSuccessBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
144 227
                            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
145  
-  [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@/logout", kDiosEndpoint, kDiosBaseUser] 
146  
-                             parameters:nil 
147  
-                                success:success 
148  
-                                failure:failure];
  228
+  
  229
+  NSString *path = [NSString stringWithFormat:@"%@/%@/logout", kDiosEndpoint, kDiosBaseUser];
  230
+
  231
+  if ([[DIOSSession sharedSession] signRequests]) {
  232
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  233
+                                                    method:@"POST"
  234
+                                                    params:nil
  235
+                                                   success:success
  236
+                                                   failure:failure];
  237
+  }
  238
+  else {
  239
+    [[DIOSSession sharedSession] postPath:path
  240
+                               parameters:nil
  241
+                                  success:success
  242
+                                  failure:failure];
  243
+  }
149 244
 }
150 245
 @end
3  DIOSView.h
@@ -44,6 +44,7 @@
44 44
  @param failure The block to be executed on the completion of an unsuccessful request. This block has no return value and takes two arguments: the receiver operation and the error that occured during the request.
45 45
  
46 46
  */
47  
-+ (void)viewGet:(NSDictionary *)params success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  47
++ (void)viewGet:(NSDictionary *)params
  48
+        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
48 49
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
49 50
 @end
19  DIOSView.m
@@ -36,8 +36,10 @@
36 36
 #import "DIOSView.h"
37 37
 #import "DIOSSession.h"
38 38
 @implementation DIOSView
39  
-+ (void)viewGet:(NSDictionary *)params success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  39
++ (void)viewGet:(NSDictionary *)params
  40
+        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
40 41
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
  42
+  
41 43
 	NSMutableString *path = [NSMutableString stringWithFormat:@"%@/%@/%@?", kDiosEndpoint, kDiosBaseView, [params objectForKey:@"view_name"]];
42 44
 	for (NSString *key in params) {
43 45
 		id value = [params objectForKey:key];
@@ -45,6 +47,19 @@ + (void)viewGet:(NSDictionary *)params success:(void (^)(AFHTTPRequestOperation
45 47
 			[path appendFormat:@"%@=%@&", key, value];
46 48
 		}
47 49
 	}
48  
-	[[DIOSSession sharedSession] getPath:path parameters:nil success:success failure:failure];
  50
+
  51
+  if ([[DIOSSession sharedSession] signRequests]) {
  52
+    [[DIOSSession sharedSession] sendSignedRequestWithPath:path
  53
+                                                    method:@"GET"
  54
+                                                    params:nil
  55
+                                                   success:success
  56
+                                                   failure:failure];
  57
+  }
  58
+  else {
  59
+    [[DIOSSession sharedSession] getPath:path
  60
+                               parameters:nil
  61
+                                  success:success
  62
+                                  failure:failure];
  63
+  }
49 64
 }
50 65
 @end
95  README.md
Source Rendered
@@ -3,23 +3,22 @@ Drupal iOS SDK - Connect your iOS/OS X app to Drupal
3 3
 What you need to know
4 4
 ================================
5 5
 The Drupal iOS SDK is a standard set of libraries for communicating to Drupal from any iOS device. Its extremely simple.
6  
-If you wanted to get some nodes in a view you can do so by calling class methods on the DIOSView Object.  
7  
-Heres an example:
  6
+If you wanted to get a node you can do so calling some class methods on DIOSNode, creating an 
  7
+NSDictionary and running the nodeGet method.  Heres an example:
8 8
 
9 9
 ```obj-c
10  
-  NSDictionary *params = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"articles", nid, nil] forKeys:[NSArray arrayWithObjects:@"view_name", @"args", nil]];
11  
-  [DIOSView viewGet:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
12  
-    [self set_sites:responseObject];
13  
-    [self.tableView reloadData];
14  
-    [self stopLoading];
15  
-  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
16  
-    #warning We should let the user know rather than dumping a log
17  
-    [self stopLoading];
18  
-    NSLog(@"Something went wrong: %@", [error localizedDescription]);
19  
-  }];
  10
+    NSMutableDictionary *nodeData = [NSMutableDictionary new];
  11
+    [nodeData setValue:@"12" forKey:@"nid"];
  12
+    [DIOSNode nodeGet:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
  13
+      //Do Something with the responseObject
  14
+      NSLog(@"%@", responseObject);
  15
+    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  16
+      //we failed, uh-oh lets error log this.
  17
+      NSLog(@"%@,  %@", [error localizedDescription], [operation responseString]);    
  18
+    }];
20 19
     
21 20
 ```
22  
-For every DIOS Object you make, any method calls that are available to you use blocks. 
  21
+For every DIOS call you make, any method calls that are available to you use blocks. 
23 22
 This allows us to define what happens when we have a request that fails or succeeds. 
24 23
 If the request was successful the result would be something like this:
25 24
 
@@ -27,7 +26,7 @@ If the request was successful the result would be something like this:
27 26
     
28 27
 However if it failed, the error might look like this:
29 28
 
30  
-    Expected status code in (200-299), got 404,  "View does not exist"
  29
+    Expected status code in (200-299), got 404,  "Node 5 could not be found"
31 30
     
32 31
 What you need to get started
33 32
 ================================
@@ -55,9 +54,75 @@ Branches are in this format
55 54
 
56 55
 OAuth
57 56
 --------------------
58  
-its coming.
  57
+If you want to use oAuth theres only one thing you need to do for 2-legged
  58
+```obj-c
  59
+  [DIOSSession sharedOauthSessionWithURL:@"http://d7.workhabit.com" consumerKey:@"yTkyapFEPAdjkW7G2euvJHhmmsURaYJP" secret:@"ZzJymFtvgCbXwFeEhivtF67M5Pcj4NwJ"];
  60
+```
  61
+This will create your shared session with the baseURL and attach your consumer key and secret.
  62
+
  63
+3-legged requires that you get some request tokens, and convert them into access tokens.
  64
+DIOS provides methods to do this, as an Example, this code will grab some request tokens and load a webview to be displayed
  65
+
  66
+```obj-c
  67
+  [DIOSSession getRequestTokensWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  68
+    requestTokens = [NSMutableDictionary new];
  69
+    NSArray *arr = [operation.responseString componentsSeparatedByCharactersInSet:
  70
+                    [NSCharacterSet characterSetWithCharactersInString:@"=&"]];
  71
+    if([arr count] == 4) {
  72
+      [requestTokens setObject:[arr objectAtIndex:1] forKey:[arr objectAtIndex:0]];
  73
+      [requestTokens setObject:[arr objectAtIndex:3] forKey:[arr objectAtIndex:2]];
  74
+    } else {
  75
+      NSLog(@"failed ahh");
  76
+    }
  77
+    [_window addSubview:oauthWebView];
  78
+    NSString *urlToLoad = [NSString stringWithFormat:@"%@/oauth/authorize?%@", [[DIOSSession sharedSession] baseURL], operation.responseString];
  79
+    NSURL *url = [NSURL URLWithString:urlToLoad];
  80
+    NSLog(@"loading url :%@", urlToLoad);
  81
+    //URL Requst Object
  82
+    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
59 83
 
  84
+    //Load the request in the UIWebView.
  85
+    [oauthWebView loadRequest:requestObj];
  86
+  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  87
+    NSLog(@"failed");
  88
+  }];
  89
+```
  90
+
  91
+If you want to get back a notificaiton when the request tokens have been authorized youll need to register a URL
  92
+for your application and make sure it is defined in your oAuth consumer which you created on your Drupal website
60 93
 
  94
+Again, another example here, we registered our app url and this method gets called when it does.
  95
+
  96
+```obj-c
  97
+- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
  98
+{
  99
+  //If our request tokens were validated, this will get called.
  100
+  if ([[url absoluteString] rangeOfString:[requestTokens objectForKey:@"oauth_token"]].location != NSNotFound) {
  101
+    [DIOSSession getAccessTokensWithRequestTokens:requestTokens success:^(AFHTTPRequestOperation *operation, id responseObject) {
  102
+      NSArray *arr = [operation.responseString componentsSeparatedByCharactersInSet:
  103
+                      [NSCharacterSet characterSetWithCharactersInString:@"=&"]];
  104
+      if([arr count] == 4) {
  105
+        //Lets set our access tokens now
  106
+        [[DIOSSession sharedSession] setAccessToken:[arr objectAtIndex:1] secret:[arr objectAtIndex:3]];
  107
+        NSDictionary *node = [NSDictionary dictionaryWithObject:@"1" forKey:@"nid"];
  108
+        [DIOSNode nodeGet:node success:^(AFHTTPRequestOperation *operation, id responseObject) {
  109
+          NSLog(@"%@", responseObject);
  110
+        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  111
+          NSLog(@"%@", [error localizedDescription]);
  112
+        }];
  113
+      } else {
  114
+        NSLog(@"failed ahh");
  115
+      }
  116
+      NSLog(@"successfully added accessTokens");
  117
+      [oauthWebView removeFromSuperview];
  118
+    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  119
+        NSLog(@"getting access tokens failed");
  120
+      [oauthWebView removeFromSuperview];
  121
+    }];
  122
+  }
  123
+  return YES;
  124
+}
  125
+```
61 126
 Documentation
62 127
 -----------
63 128
 [Can be found here](https://github.com/workhabitinc/drupal-ios-sdk/wiki/drupal-ios-sdk-2.0)
2  Settings.h
... ...
@@ -1,5 +1,5 @@
1 1
 #define kDiosBaseUrl @"http://d7.workhabit.com"
2  
-#define kDiosEndpoint @"api"
  2
+#define kDiosEndpoint @"test"
3 3
 #define kDiosBaseNode @"node"
4 4
 #define kDiosBaseComment @"comment"
5 5
 #define kDiosBaseUser @"user"

No commit comments for this range

Something went wrong with that request. Please try again.