Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: kylebrowning/drupal-ios-sdk
base: ccf0214503
...
head fork: kylebrowning/drupal-ios-sdk
compare: 8c025e810f
  • 3 commits
  • 13 files changed
  • 0 commit comments
  • 1 contributor
94 DIOSComment.m
View
@@ -46,37 +46,84 @@ + (void)commentGet:(NSDictionary *)comment
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]]
- parameters:nil
- success:success
- failure:failure];
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"GET"
+ params:comment
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] getPath:path
+ parameters:nil
+ success:success
+ failure:failure];
+ }
}
#pragma mark commentSave
+ (void)commentSave:(NSDictionary *)comment
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseComment]
- parameters:comment
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseComment];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"POST"
+ params:comment
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] postPath:path
+ parameters:comment
+ success:success
+ failure:failure];
+ }
}
#pragma mark commentUpdate
+ (void)commentUpdate:(NSDictionary *)comment
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] putPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]]
- parameters:comment
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"PUT"
+ params:comment
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] putPath:path
+ parameters:comment
+ success:success
+ failure:failure];
+ }
+
}
#pragma mark CommentDelete
+ (void)commentDelete:(NSDictionary *)comment
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure; {
- [[DIOSSession sharedSession] deletePath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]] parameters:comment success:success failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseComment, [comment objectForKey:@"cid"]];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"DELETE"
+ params:comment
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] deletePath:path
+ parameters:comment
+ success:success
+ failure:failure];
+ }
}
#pragma mark commentIndex
@@ -94,15 +141,26 @@ + (void)commentIndexWithPage:(NSString *)page
[commentIndexDict setValue:parameteres forKey:@"parameters"];
[commentIndexDict setValue:pageSize forKey:@"pagesize"];
[self commentIndex:commentIndexDict success:success failure:failure];
+ [commentIndexDict release];
}
+ (void)commentIndex:(NSDictionary *)params
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure; {
-
- [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseComment]
- parameters:params
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseComment];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"GET"
+ params:params
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] getPath:path
+ parameters:params
+ success:success
+ failure:failure];
+ }
}
@end
2  DIOSFile.h
View
@@ -40,7 +40,7 @@
+ (void) fileGet:(NSDictionary *)params
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
-+ (void) fileSave:(NSDictionary *)params
++ (void)fileSave:(NSDictionary *)file
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
+ (UIImageView *) getImageViewForFileImage:(NSDictionary *) file;
37 DIOSFile.m
View
@@ -44,25 +44,44 @@ + (void) fileGet:(NSDictionary *)params
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseFile, [params objectForKey:@"fid"]]
- parameters:params
- success:success
- failure:failure];
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseFile, [params objectForKey:@"fid"]];
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"GET"
+ params:params
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] getPath:path
+ parameters:params
+ success:success
+ failure:failure];
+ }
+
}
+ (void)fileSave:(NSDictionary *)file
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
-
- [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseFile]
- parameters:file
- success:success
- failure:failure];
+ NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseFile];
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"POST"
+ params:file
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] postPath:path
+ parameters:file
+ success:success
+ failure:failure];
+ }
}
+ (UIImageView *) getImageViewForFileImage:(NSDictionary *) file; {
NSURL *url = [NSURL URLWithString:[file objectForKey:@"uri_full"]];
UIImageView *remoteImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[remoteImage setImageWithURL:url];
+ [remoteImage release];
return remoteImage;
}
@end
10 DIOSNode.h
View
@@ -55,15 +55,15 @@
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
-+ (void)nodeIndexWithPage:(NSString *)page fields:(NSString *)fields parameters:(NSArray *)parameteres pageSize:(NSString *)pageSize
++ (void)nodeIndexWithPage:(NSString *)page
+ fields:(NSString *)fields
+ parameters:(NSArray *)parameteres
+ pageSize:(NSString *)pageSize
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure ;
+ (void)nodeIndex:(NSDictionary *)params
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
-+ (void)nodeAttachFile:(NSDictionary *)params
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
@end
112 DIOSNode.m
View
@@ -43,11 +43,21 @@ @implementation DIOSNode
+ (void)nodeGet:(NSDictionary *)node
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]];
- [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]]
- parameters:nil
- success:success
- failure:failure];
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"GET"
+ params:node
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] getPath:path
+ parameters:nil
+ success:success
+ failure:failure];
+ }
}
#pragma mark nodeSave
@@ -55,10 +65,21 @@ + (void)nodeSave:(NSDictionary *)node
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseNode]
- parameters:node
- success:success
- failure:failure];
+ NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseNode];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"POST"
+ params:node
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] postPath:path
+ parameters:node
+ success:success
+ failure:failure];
+ }
}
#pragma mark nodeUpdate
@@ -66,10 +87,22 @@ + (void)nodeUpdate:(NSDictionary *)node
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] putPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]]
- parameters:node
- success:success
- failure:failure];
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"PUT"
+ params:node
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] putPath:path
+ parameters:node
+ success:success
+ failure:failure];
+ }
+
}
#pragma mark nodeDelete
@@ -77,14 +110,27 @@ + (void)nodeDelete:(NSDictionary *)node
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] deletePath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]]
- parameters:node
- success:success
- failure:failure];
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseNode, [node objectForKey:@"nid"]];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"DELETE"
+ params:node
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] deletePath:path
+ parameters:node
+ success:success
+ failure:failure];
+ }
}
#pragma mark nodeIndex
//Simpler method if you didnt want to build the params :)
-+ (void)nodeIndexWithPage:(NSString *)page fields:(NSString *)fields parameters:(NSArray *)parameteres pageSize:(NSString *)pageSize
++ (void)nodeIndexWithPage:(NSString *)page
+ fields:(NSString *)fields
+ parameters:(NSArray *)parameteres
+ pageSize:(NSString *)pageSize
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
NSMutableDictionary *nodeIndexDict = [NSMutableDictionary new];
@@ -93,29 +139,27 @@ + (void)nodeIndexWithPage:(NSString *)page fields:(NSString *)fields parameters:
[nodeIndexDict setValue:parameteres forKey:@"parameters"];
[nodeIndexDict setValue:pageSize forKey:@"pagesize"];
[self nodeIndex:nodeIndexDict success:success failure:failure];
+ [nodeIndexDict release];
}
+ (void)nodeIndex:(NSDictionary *)params
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
-
- [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseNode] parameters:params success:success failure:failure];
-}
-#pragma mark nodeAttachFile
-+ (void)nodeAttachFile:(NSDictionary *)params
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- 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) {
- [formData appendPartWithFileData:[params objectForKey:@"fileData"] name:[params objectForKey:@"name"] fileName:[params objectForKey:@"fileName"] mimeType:[params objectForKey:@"mimetype"]];
- }];
-
- AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
- [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
- NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
- }];
+ NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseNode];
- [operation setCompletionBlockWithSuccess:success failure:failure];
- [operation start];
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"DELETE"
+ params:params
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] getPath:path
+ parameters:params
+ success:success
+ failure:failure];
+ }
}
+
@end
34 DIOSSession.h
View
@@ -41,9 +41,43 @@
@interface DIOSSession : AFHTTPClient {
NSDictionary *user;
+ NSMutableDictionary *accessTokens;
+ NSString *consumerKey;
+ NSString *consumerSecret;
+ NSString *tokenIdentifier;
+ NSString *tokenSecret;
}
@property (strong, nonatomic) NSDictionary *user;
+@property (nonatomic, retain) NSMutableDictionary *accessTokens;
+@property (nonatomic) BOOL signRequests;
+@property (nonatomic) BOOL threeLegged;
+@property (nonatomic, copy) NSString *realm;
+@property (nonatomic, retain) NSString *consumerKey;
+@property (nonatomic, retain) NSString *consumerSecret;
+@property (nonatomic, retain) NSString *tokenIdentifier;
+@property (nonatomic, retain) NSString *tokenSecret;
+ (DIOSSession *)sharedSession;
++ (DIOSSession *)sharedSessionWithURL:(NSString*)url;
++ (DIOSSession *)sharedOauthSessionWithURL:(NSString*)url consumerKey:(NSString *)aConsumerKey secret:(NSString *)aConsumerSecret;
++ (void) getRequestTokensWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
++ (void) getAccessTokensWithRequestTokens:(NSDictionary *)requesTokens
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
+- (void) sendSignedRequestWithPath:(NSString*)path
+ method:(NSString*)method
+ params:(NSDictionary*)params
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
+
+- (void) addHeaderValue:(NSString*)value forKey:(NSString*)key;
+- (NSURLRequest *) signedRequestWithMethod:(NSString *)method
+ path:(NSString *)path
+ parameters:(NSDictionary *)parameters;
+- (id) initWithBaseURL:(NSURL *)url consumerKey:(NSString *)consumerKey secret:(NSString *)consumerSecret;
+
+- (void) setAccessToken:(NSString *)accessToken secret:(NSString *)secret;
+- (void) setConsumerKey:(NSString *)consumerKey secret:(NSString *)secret;
@end
327 DIOSSession.m
View
@@ -38,19 +38,94 @@
#import "DIOSSession.h"
#import "AFJSONRequestOperation.h"
#import "AFPropertyListRequestOperation.h"
+#include <sys/time.h>
+#import <CommonCrypto/CommonHMAC.h>
+static NSString* Base64EncodedStringFromData(NSData *data);
+static NSString* URLEncodeString(NSString *string);
+static const NSString *kOAuthSignatureMethodKey = @"oauth_signature_method";
+static const NSString *kOAuthVersionKey = @"oauth_version";
+static const NSString *kOAuthConsumerKey = @"oauth_consumer_key";
+static const NSString *kOAuthTokenIdentifier = @"oauth_token";
+static const NSString *kOAuthSignatureKey = @"oauth_signature";
+
+static const NSString *kOAuthSignatureTypeHMAC_SHA1 = @"HMAC-SHA1";
+static const NSString *kOAuthVersion1_0 = @"1.0";
+static dispatch_once_t once;
+static DIOSSession *sharedSession;
+@interface DIOSSession()
+
+- (id) initWithBaseURL:(NSURL *)url;
+- (void) addGeneratedTimestampAndNonceInto:(NSMutableDictionary *)dictionary;
+
+- (NSString *) authorizationHeaderValueForRequest:(NSURLRequest *)request;
+@end
@implementation DIOSSession
-@synthesize user;
+@synthesize user, accessTokens, consumerKey, consumerSecret, tokenIdentifier, tokenSecret,
+realm, signRequests, threeLegged;
+
+ (DIOSSession *)sharedSession {
- static dispatch_once_t once;
- static DIOSSession *sharedSession;
- dispatch_once(&once, ^ {
+ dispatch_once(&once, ^ {
sharedSession = [[self alloc] initWithBaseURL:[NSURL URLWithString:kDiosBaseUrl]];
[sharedSession setParameterEncoding:AFJSONParameterEncoding];
});
return sharedSession;
}
++ (DIOSSession *)sharedSessionWithURL:(NSString*)url {
+ dispatch_once(&once, ^ {
+ sharedSession = [[self alloc] initWithBaseURL:[NSURL URLWithString:url]];
+ [sharedSession setParameterEncoding:AFJSONParameterEncoding];
+ });
+ [sharedSession setBaseURL:[NSURL URLWithString:url]];
+ return sharedSession;
+}
+
++ (DIOSSession *)sharedOauthSessionWithURL:(NSString*)url consumerKey:(NSString *)aConsumerKey secret:(NSString *)aConsumerSecret {
+ dispatch_once(&once, ^ {
+ sharedSession = [[self alloc] initWithBaseURL:[NSURL URLWithString:url] consumerKey:aConsumerKey secret:aConsumerSecret];
+ [sharedSession setParameterEncoding:AFJSONParameterEncoding];
+ });
+ [sharedSession setBaseURL:[NSURL URLWithString:url]];
+ return sharedSession;
+}
+
++ (void) getRequestTokensWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
+
+ DIOSSession *client = [[DIOSSession alloc] initWithBaseURL:[[DIOSSession sharedSession] baseURL]];
+ [client setConsumerKey:[[DIOSSession sharedSession] consumerKey] secret:[[DIOSSession sharedSession] consumerSecret]];
+ [client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
+ [client setDefaultHeader:@"Accept" value:@"text/html"];
+ [client setDefaultHeader:@"Content-Type" value:@"text/html"];
+ NSMutableDictionary *params = [NSMutableDictionary new];
+ [params setObject:[[DIOSSession sharedSession] consumerKey] forKey:kOAuthConsumerKey];
+ [params setObject:[[DIOSSession sharedSession] consumerSecret] forKey:kOAuthTokenIdentifier];
+ [client sendSignedRequestWithPath:@"/oauth/request_token" method:@"GET" params:params success:success failure:failure];
+}
++ (void) getAccessTokensWithRequestTokens:(NSDictionary *)requestTokens
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
+
+ DIOSSession *client = [[DIOSSession alloc] initWithBaseURL:[[DIOSSession sharedSession] baseURL]];
+ [client setConsumerKey:[[DIOSSession sharedSession] consumerKey] secret:[[DIOSSession sharedSession] consumerSecret]];
+ [client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
+ [client setDefaultHeader:@"Accept" value:@"text/html"];
+ [client setDefaultHeader:@"Content-Type" value:@"text/html"];
+ [client setAccessToken:[requestTokens objectForKey:@"oauth_token"] secret:[requestTokens objectForKey:@"oauth_token_secret"]];
+ [client sendSignedRequestWithPath:@"/oauth/access_token" method:@"GET" params:requestTokens success:success failure:failure];
+}
+- (void) sendSignedRequestWithPath:(NSString*)path
+ method:(NSString*)method
+ params:(NSDictionary*)params
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
+ NSURLRequest *request = [self signedRequestWithMethod:method path:path parameters:params];
+
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
+ [self enqueueHTTPRequestOperation:operation];
+}
+
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
@@ -64,4 +139,248 @@ - (id)initWithBaseURL:(NSURL *)url {
return self;
}
+
+- (id) initWithBaseURL:(NSURL *)url consumerKey:(NSString *)aConsumerKey secret:(NSString *)aConsumerSecret {
+ self = [super initWithBaseURL:url];
+
+ if (self) {
+ self.signRequests = YES;
+ self.consumerKey = aConsumerKey;
+ self.consumerSecret = aConsumerSecret;
+
+ [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
+ // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
+ [self setDefaultHeader:@"Accept" value:@"application/json"];
+ [self setDefaultHeader:@"Content-Type" value:@"application/json"];
+ }
+
+ return self;
+}
+- (NSURLRequest *) signedRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
+ NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
+
+ NSString *authorizationHeader = [self authorizationHeaderValueForRequest:request];
+ [request setValue:authorizationHeader forHTTPHeaderField:@"Authorization"];
+
+ return request;
+}
+- (NSMutableURLRequest *) requestWithMethod:(NSString *)method
+ path:(NSString *)path
+ parameters:(NSDictionary *)parameters {
+
+ NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
+
+ if (self.signRequests) {
+ NSString *authorizationHeader = [self authorizationHeaderValueForRequest:request];
+ [request setValue:authorizationHeader forHTTPHeaderField:@"Authorization"];
+ }
+
+ return request;
+}
+
+- (NSURLRequest *) unsignedRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
+ NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
+
+ return request;
+}
+
+- (void) setAccessToken:(NSString *)accessToken secret:(NSString *)secret {
+ self.tokenIdentifier = accessToken;
+ self.tokenSecret = secret;
+ self.threeLegged = YES;
+}
+
+- (void) setConsumerKey:(NSString *)aConsumerKey secret:(NSString *)secret {
+ self.consumerKey = aConsumerKey;
+ self.consumerSecret = secret;
+}
+- (void) addHeaderValue:(NSString*)value forKey:(NSString*)key {
+ [self setDefaultHeader:key value:value];
+}
+
+- (NSMutableDictionary *) mutableDictionaryWithOAuthInitialData {
+ NSMutableDictionary *result = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ kOAuthSignatureTypeHMAC_SHA1, kOAuthSignatureMethodKey,
+ kOAuthVersion1_0, kOAuthVersionKey,
+ nil];
+
+ if (self.consumerKey) [result setObject:self.consumerKey forKey:kOAuthConsumerKey];
+ if (self.tokenIdentifier) [result setObject:self.tokenIdentifier forKey:kOAuthTokenIdentifier];
+
+ [self addGeneratedTimestampAndNonceInto:result];
+
+ return result;
+}
+
+- (NSString *) stringWithOAuthParameters:(NSMutableDictionary *)oauthParams requestParameters:(NSDictionary *)parameters {
+ NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:oauthParams];
+ [params addEntriesFromDictionary:parameters];
+
+ // sorting parameters
+ NSArray *sortedKeys = [[params allKeys] sortedArrayUsingComparator:^NSComparisonResult(NSString *key1, NSString *key2) {
+ NSComparisonResult result = [key1 compare:key2 options:NSLiteralSearch];
+ if (result == NSOrderedSame)
+ result = [[params objectForKey:key1] compare:[params objectForKey:key2] options:NSLiteralSearch];
+
+ return result;
+ }];
+
+ // join keys and values with =
+ NSMutableArray *longListOfParameters = [NSMutableArray arrayWithCapacity:[sortedKeys count]];
+ [sortedKeys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) {
+ [longListOfParameters addObject:[NSString stringWithFormat:@"%@=%@", key, [params objectForKey:key]]];
+ }];
+
+ // join components with &
+ return [longListOfParameters componentsJoinedByString:@"&"];
+}
+
+- (NSString *) authorizationHeaderValueForRequest:(NSURLRequest *)request {
+ NSURL *url = request.URL;
+ NSString *fixedURL = [self baseURLforAddress:url];
+ NSMutableDictionary *oauthParams = [self mutableDictionaryWithOAuthInitialData];
+ // adding oauth_ extra params to the header
+ NSArray *parameterComponents = [[request.URL query] componentsSeparatedByString:@"&"];
+ NSMutableDictionary *parameters = [NSMutableDictionary new];
+ for(NSString *component in parameterComponents) {
+ NSArray *subComponents = [component componentsSeparatedByString:@"="];
+ if ([subComponents count] == 2) {
+ [parameters setObject:[subComponents objectAtIndex:1] forKey:[subComponents objectAtIndex:0]];
+ }
+ }
+ NSData *body = [request HTTPBody];
+ NSString *htttpBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
+ NSArray *subComponents = [htttpBody componentsSeparatedByString:@"="];
+ if ([subComponents count] == 2) {
+ [parameters setObject:[subComponents objectAtIndex:1] forKey:[subComponents objectAtIndex:0]];
+ }
+
+ NSString *allParameters = [self stringWithOAuthParameters:oauthParams requestParameters:parameters];
+ // adding HTTP method and URL
+ NSString *signatureBaseString = [NSString stringWithFormat:@"%@&%@&%@", [request.HTTPMethod uppercaseString], URLEncodeString(fixedURL), URLEncodeString(allParameters)];
+
+ NSString *signature = [self signatureForBaseString:signatureBaseString];
+
+ // add to OAuth params
+ [oauthParams setObject:signature forKey:kOAuthSignatureKey];
+
+ // build OAuth Authorization Header
+ NSMutableArray *headerParams = [NSMutableArray arrayWithCapacity:[oauthParams count]];
+ [oauthParams enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
+ [headerParams addObject:[NSString stringWithFormat:@"%@=\"%@\"", key, URLEncodeString(obj)]];
+ }];
+
+ // let's use the base URL if a realm was not set
+ NSString *oauthRealm = self.realm;
+ if (!oauthRealm) oauthRealm = [self baseURLforAddress:[self baseURL]];
+
+ NSString *result = [NSString stringWithFormat:@"OAuth realm=\"%@\",%@", oauthRealm, [headerParams componentsJoinedByString:@","]];
+
+ return result;
+}
+
+- (void)addGeneratedTimestampAndNonceInto:(NSMutableDictionary *)dictionary {
+ NSUInteger epochTime = (NSUInteger)[[NSDate date] timeIntervalSince1970];
+ NSString *timestamp = [NSString stringWithFormat:@"%d", epochTime];
+ CFUUIDRef theUUID = CFUUIDCreate(NULL);
+ CFStringRef string = CFUUIDCreateString(NULL, theUUID);
+ NSString *nonce = (NSString *)string;
+ CFRelease(theUUID);
+
+ [dictionary setObject:nonce forKey:@"oauth_nonce"];
+ [dictionary setObject:timestamp forKey:@"oauth_timestamp"];
+}
+
+- (NSString *) signatureForBaseString:(NSString *)baseString {
+ NSString *key = [NSString stringWithFormat:@"%@&%@", self.consumerSecret != nil ? URLEncodeString(self.consumerSecret) : @"", self.tokenSecret != nil ? URLEncodeString(self.tokenSecret) : @""];
+ const char *keyBytes = [key cStringUsingEncoding:NSUTF8StringEncoding];
+ const char *baseStringBytes = [baseString cStringUsingEncoding:NSUTF8StringEncoding];
+ unsigned char digestBytes[CC_SHA1_DIGEST_LENGTH];
+
+ CCHmac(kCCHmacAlgSHA1, keyBytes, strlen(keyBytes), baseStringBytes, strlen(baseStringBytes), digestBytes);
+
+ NSData *digestData = [NSData dataWithBytes:digestBytes length:CC_SHA1_DIGEST_LENGTH];
+ return Base64EncodedStringFromData(digestData);
+}
+
+- (NSString *) baseURLforAddress:(NSURL *)url {
+ NSAssert1([url host] != nil, @"URL host missing: %@", [url absoluteString]);
+
+ // Port need only be present if it's not the default
+ NSString *hostString;
+ if (([url port] == nil)
+ || ([[[url scheme] lowercaseString] isEqualToString:@"http"] && ([[url port] integerValue] == 80))
+ || ([[[url scheme] lowercaseString] isEqualToString:@"https"] && ([[url port] integerValue] == 443))) {
+ hostString = [[url host] lowercaseString];
+ } else {
+ hostString = [NSString stringWithFormat:@"%@:%@", [[url host] lowercaseString], [url port]];
+ }
+
+ return [NSString stringWithFormat:@"%@://%@%@", [[url scheme] lowercaseString], hostString, [[url absoluteURL] path]];
+}
+
+//
+// The function below is based on
+//
+// NSString+URLEncode.h
+//
+// Created by Scott James Remnant on 6/1/11.
+// Copyright 2011 Scott James Remnant <scott@netsplit.com>. All rights reserved.
+//
+static NSString *URLEncodeString(NSString *string) {
+ // See http://en.wikipedia.org/wiki/Percent-encoding and RFC3986
+ // Hyphen, Period, Understore & Tilde are expressly legal
+ const CFStringRef legalURLCharactersToBeEscaped = CFSTR(":/=,!$&'()*+;[]@#?");
+
+ return ( NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, ( CFStringRef)string, NULL, legalURLCharactersToBeEscaped, kCFStringEncodingUTF8);
+}
@end
+// The function below was inspired on
+//
+// AFOAuth2Client.m
+//
+// Copyright (c) 2011 Mattt Thompson (http://mattt.me/)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+static NSString * Base64EncodedStringFromData(NSData *data) {
+ NSUInteger length = [data length];
+ NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
+
+ uint8_t *input = (uint8_t *)[data bytes];
+ uint8_t *output = (uint8_t *)[mutableData mutableBytes];
+
+ for (NSUInteger i = 0; i < length; i += 3) {
+ NSUInteger value = 0;
+ for (NSUInteger j = i; j < (i + 3); j++) {
+ value <<= 8;
+ if (j < length) value |= (0xFF & input[j]);
+ }
+
+ static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+ NSUInteger idx = (i / 3) * 4;
+ output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
+ output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
+ output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '=';
+ output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '=';
+ }
+
+ return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
+}
60 DIOSTaxonomy.m
View
@@ -50,15 +50,27 @@ + (void)getTreeWithVid:(NSString *)vid
[params setValue:parent forKey:@"parent"];
[params setValue:maxDepth forKey:@"max_depth"];
[self getTreeWithParams:params success:success failure:failure];
+ [params release];
}
+ (void)getTreeWithParams:(NSDictionary *)params
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@/getTree", kDiosEndpoint, kDiosBaseTaxonmyVocabulary];
- [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@/getTree", kDiosEndpoint, kDiosBaseTaxonmyVocabulary]
- parameters:params
- success:success
- failure:failure];
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"POST"
+ params:params
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] postPath:path
+ parameters:params
+ success:success
+ failure:failure];
+ }
+
}
+ (void)selectNodesWithTid:(NSString *)tid
andLimit:(NSString *)limit
@@ -73,22 +85,44 @@ + (void)selectNodesWithTid:(NSString *)tid
[params setValue:pager forKey:@"pager"];
[params setValue:order forKey:@"prder"];
[self selectNodesWithParams:params success:success failure:failure];
+ [params release];
}
+ (void)selectNodesWithParams:(NSDictionary *)params
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
-
- [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@/selectNodes", kDiosEndpoint, kDiosBaseTaxonmyTerm]
- parameters:params
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@/selectNodes", kDiosEndpoint, kDiosBaseTaxonmyTerm];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"POST"
+ params:params
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] postPath:path
+ parameters:params
+ success:success
+ failure:failure];
+ }
}
+ (void)getTermWithTid:(NSString *)tid
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseTaxonmyTerm, tid]
- parameters:nil
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseTaxonmyTerm, tid];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"GET"
+ params:nil
+ success:success
+ failure:failure];
+ } else {
+ [[DIOSSession sharedSession] getPath:path
+ parameters:nil
+ success:success
+ failure:failure];
+ }
}
@end
167 DIOSUser.m
View
@@ -44,11 +44,22 @@ @implementation DIOSUser
+ (void)userGet:(NSDictionary *)user
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
-
- [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]]
- parameters:nil
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"GET"
+ params:user
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] getPath:path
+ parameters:user
+ success:success
+ failure:failure];
+ }
}
@@ -56,11 +67,22 @@ + (void)userGet:(NSDictionary *)user
+ (void)userSave:(NSDictionary *)user
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
-
- [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseUser]
- parameters:user
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseUser];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"POST"
+ params:user
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] postPath:path
+ parameters:user
+ success:success
+ failure:failure];
+ }
}
#pragma mark userRegister
@@ -68,21 +90,43 @@ + (void)userRegister:(NSDictionary *)user
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@/register", kDiosEndpoint, kDiosBaseUser]
- parameters:user
- success:success
- failure:failure];
+ NSString *path = [NSString stringWithFormat:@"%@/%@/register", kDiosEndpoint, kDiosBaseUser];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"POST"
+ params:user
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] postPath:path
+ parameters:user
+ success:success
+ failure:failure];
+ }
}
#pragma mark userUpdate
+ (void)userUpdate:(NSDictionary *)user
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
-
- [[DIOSSession sharedSession] putPath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]]
- parameters:user
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"PUT"
+ params:user
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] putPath:path
+ parameters:user
+ success:success
+ failure:failure];
+ }
}
#pragma mark UserDelete
@@ -90,10 +134,21 @@ + (void)userDelete:(NSDictionary *)user
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] deletePath:[NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]]
- parameters:user
- success:success
- failure:failure];
+ NSString *path = [NSString stringWithFormat:@"%@/%@/%@", kDiosEndpoint, kDiosBaseUser, [user objectForKey:@"uid"]];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"DELETE"
+ params:user
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] deletePath:path
+ parameters:user
+ success:success
+ failure:failure];
+ }
}
@@ -111,15 +166,28 @@ + (void)userIndexWithPage:(NSString *)page
[userIndexDict setValue:parameteres forKey:@"parameters"];
[userIndexDict setValue:pageSize forKey:@"pagesize"];
[self userIndex:userIndexDict success:success failure:failure];
+ [userIndexDict release];
}
+ (void)userIndex:(NSDictionary *)params
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] getPath:[NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseUser]
- parameters:params
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@", kDiosEndpoint, kDiosBaseUser];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"GET"
+ params:params
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] getPath:path
+ parameters:params
+ success:success
+ failure:failure];
+ }
}
#pragma mark userLogin
@@ -128,23 +196,50 @@ + (void)userLoginWithUsername:(NSString *)username andPassword:(NSString *)passw
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
NSDictionary *params = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:username, password, nil] forKeys:[NSArray arrayWithObjects:@"username", @"password", nil]];
- [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@/login", kDiosEndpoint, kDiosBaseUser]
- parameters:params
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@/login", kDiosEndpoint, kDiosBaseUser];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"POST"
+ params:params
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] postPath:path
+ parameters:params
+ success:success
+ failure:failure];
+ }
}
+ (void)userLogin:(NSDictionary *)user
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [self userLoginWithUsername:[user objectForKey:@"name"] andPassword:[user objectForKey:@"pass"] success:success failure:failure];
+ [self userLoginWithUsername:[user objectForKey:@"name"]
+ andPassword:[user objectForKey:@"pass"]
+ success:success
+ failure:failure];
}
#pragma mark userLogout
+ (void)userLogoutWithSuccessBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
- [[DIOSSession sharedSession] postPath:[NSString stringWithFormat:@"%@/%@/logout", kDiosEndpoint, kDiosBaseUser]
- parameters:nil
- success:success
- failure:failure];
+
+ NSString *path = [NSString stringWithFormat:@"%@/%@/logout", kDiosEndpoint, kDiosBaseUser];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"POST"
+ params:nil
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] postPath:path
+ parameters:nil
+ success:success
+ failure:failure];
+ }
}
@end
3  DIOSView.h
View
@@ -44,6 +44,7 @@
@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.
*/
-+ (void)viewGet:(NSDictionary *)params success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
++ (void)viewGet:(NSDictionary *)params
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure;
@end
19 DIOSView.m
View
@@ -36,8 +36,10 @@
#import "DIOSView.h"
#import "DIOSSession.h"
@implementation DIOSView
-+ (void)viewGet:(NSDictionary *)params success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
++ (void)viewGet:(NSDictionary *)params
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {
+
NSMutableString *path = [NSMutableString stringWithFormat:@"%@/%@/%@?", kDiosEndpoint, kDiosBaseView, [params objectForKey:@"view_name"]];
for (NSString *key in params) {
id value = [params objectForKey:key];
@@ -45,6 +47,19 @@ + (void)viewGet:(NSDictionary *)params success:(void (^)(AFHTTPRequestOperation
[path appendFormat:@"%@=%@&", key, value];
}
}
- [[DIOSSession sharedSession] getPath:path parameters:nil success:success failure:failure];
+
+ if ([[DIOSSession sharedSession] signRequests]) {
+ [[DIOSSession sharedSession] sendSignedRequestWithPath:path
+ method:@"GET"
+ params:nil
+ success:success
+ failure:failure];
+ }
+ else {
+ [[DIOSSession sharedSession] getPath:path
+ parameters:nil
+ success:success
+ failure:failure];
+ }
}
@end
95 README.md
View
@@ -3,23 +3,22 @@ Drupal iOS SDK - Connect your iOS/OS X app to Drupal
What you need to know
================================
The Drupal iOS SDK is a standard set of libraries for communicating to Drupal from any iOS device. Its extremely simple.
-If you wanted to get some nodes in a view you can do so by calling class methods on the DIOSView Object.
-Heres an example:
+If you wanted to get a node you can do so calling some class methods on DIOSNode, creating an
+NSDictionary and running the nodeGet method. Heres an example:
```obj-c
- NSDictionary *params = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"articles", nid, nil] forKeys:[NSArray arrayWithObjects:@"view_name", @"args", nil]];
- [DIOSView viewGet:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
- [self set_sites:responseObject];
- [self.tableView reloadData];
- [self stopLoading];
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- #warning We should let the user know rather than dumping a log
- [self stopLoading];
- NSLog(@"Something went wrong: %@", [error localizedDescription]);
- }];
+ NSMutableDictionary *nodeData = [NSMutableDictionary new];
+ [nodeData setValue:@"12" forKey:@"nid"];
+ [DIOSNode nodeGet:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
+ //Do Something with the responseObject
+ NSLog(@"%@", responseObject);
+ } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
+ //we failed, uh-oh lets error log this.
+ NSLog(@"%@, %@", [error localizedDescription], [operation responseString]);
+ }];
```
-For every DIOS Object you make, any method calls that are available to you use blocks.
+For every DIOS call you make, any method calls that are available to you use blocks.
This allows us to define what happens when we have a request that fails or succeeds.
If the request was successful the result would be something like this:
@@ -27,7 +26,7 @@ If the request was successful the result would be something like this:
However if it failed, the error might look like this:
- Expected status code in (200-299), got 404, "View does not exist"
+ Expected status code in (200-299), got 404, "Node 5 could not be found"
What you need to get started
================================
@@ -55,9 +54,75 @@ Branches are in this format
OAuth
--------------------
-its coming.
+If you want to use oAuth theres only one thing you need to do for 2-legged
+```obj-c
+ [DIOSSession sharedOauthSessionWithURL:@"http://d7.workhabit.com" consumerKey:@"yTkyapFEPAdjkW7G2euvJHhmmsURaYJP" secret:@"ZzJymFtvgCbXwFeEhivtF67M5Pcj4NwJ"];
+```
+This will create your shared session with the baseURL and attach your consumer key and secret.
+
+3-legged requires that you get some request tokens, and convert them into access tokens.
+DIOS provides methods to do this, as an Example, this code will grab some request tokens and load a webview to be displayed
+
+```obj-c
+ [DIOSSession getRequestTokensWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
+ requestTokens = [NSMutableDictionary new];
+ NSArray *arr = [operation.responseString componentsSeparatedByCharactersInSet:
+ [NSCharacterSet characterSetWithCharactersInString:@"=&"]];
+ if([arr count] == 4) {
+ [requestTokens setObject:[arr objectAtIndex:1] forKey:[arr objectAtIndex:0]];
+ [requestTokens setObject:[arr objectAtIndex:3] forKey:[arr objectAtIndex:2]];
+ } else {
+ NSLog(@"failed ahh");
+ }
+ [_window addSubview:oauthWebView];
+ NSString *urlToLoad = [NSString stringWithFormat:@"%@/oauth/authorize?%@", [[DIOSSession sharedSession] baseURL], operation.responseString];
+ NSURL *url = [NSURL URLWithString:urlToLoad];
+ NSLog(@"loading url :%@", urlToLoad);
+ //URL Requst Object
+ NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
+ //Load the request in the UIWebView.
+ [oauthWebView loadRequest:requestObj];
+ } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
+ NSLog(@"failed");
+ }];
+```
+
+If you want to get back a notificaiton when the request tokens have been authorized youll need to register a URL
+for your application and make sure it is defined in your oAuth consumer which you created on your Drupal website
+Again, another example here, we registered our app url and this method gets called when it does.
+
+```obj-c
+- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
+{
+ //If our request tokens were validated, this will get called.
+ if ([[url absoluteString] rangeOfString:[requestTokens objectForKey:@"oauth_token"]].location != NSNotFound) {
+ [DIOSSession getAccessTokensWithRequestTokens:requestTokens success:^(AFHTTPRequestOperation *operation, id responseObject) {
+ NSArray *arr = [operation.responseString componentsSeparatedByCharactersInSet:
+ [NSCharacterSet characterSetWithCharactersInString:@"=&"]];
+ if([arr count] == 4) {
+ //Lets set our access tokens now
+ [[DIOSSession sharedSession] setAccessToken:[arr objectAtIndex:1] secret:[arr objectAtIndex:3]];
+ NSDictionary *node = [NSDictionary dictionaryWithObject:@"1" forKey:@"nid"];
+ [DIOSNode nodeGet:node success:^(AFHTTPRequestOperation *operation, id responseObject) {
+ NSLog(@"%@", responseObject);
+ } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
+ NSLog(@"%@", [error localizedDescription]);
+ }];
+ } else {
+ NSLog(@"failed ahh");
+ }
+ NSLog(@"successfully added accessTokens");
+ [oauthWebView removeFromSuperview];
+ } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
+ NSLog(@"getting access tokens failed");
+ [oauthWebView removeFromSuperview];
+ }];
+ }
+ return YES;
+}
+```
Documentation
-----------
[Can be found here](https://github.com/workhabitinc/drupal-ios-sdk/wiki/drupal-ios-sdk-2.0)
2  Settings.h
View
@@ -1,5 +1,5 @@
#define kDiosBaseUrl @"http://d7.workhabit.com"
-#define kDiosEndpoint @"api"
+#define kDiosEndpoint @"test"
#define kDiosBaseNode @"node"
#define kDiosBaseComment @"comment"
#define kDiosBaseUser @"user"

No commit comments for this range

Something went wrong with that request. Please try again.