PageRenderTime 26ms CodeModel.GetById 10ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/NSRTMInvoker.m

http://rtm2cocoa.googlecode.com/
Objective C | 114 lines | 75 code | 19 blank | 20 comment | 9 complexity | 603df6f1eaa109576e7b78dec415e77b MD5 | raw file
  1/*
  2 *  NSRTMInvoker.m
  3 *  RTMApiTest
  4 *
  5 *  Created by kkillian on 17/11/09.
  6 *  Copyright 2009 shufflecodebox. All rights reserved.
  7 *	This program is free software; you can redistribute it and/or modify
  8 *	it under the terms of the GNU General Public License as published by
  9 *	the Free Software Foundation; either version 2 of the License, or
 10 *	(at your option) any later version.
 11 *
 12 *	This program is distributed in the hope that it will be useful,
 13 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 *	GNU General Public License for more details.
 16 *
 17 *	You should have received a copy of the GNU General Public License
 18 *	along with this program; if not, write to the Free Software
 19 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20 */
 21
 22#import "NSRTMInvoker.h"
 23#import "DebugLog.h"
 24#import "NSRTMParam.h"
 25#import "NSDataMd5.h"
 26#import "DigestMd5.h"
 27
 28#define SERVICE_REST @"http://api.rememberthemilk.com/services/rest/?"
 29#define SERVICE_AUTH @"http://www.rememberthemilk.com/services/auth/?"
 30#define API_STRING @"api_key"
 31#define SIGN_STRING @"api_sig"
 32
 33@implementation NSRTMInvoker
 34
 35-(id) init
 36{
 37	self = [super init];
 38	if (self != nil) {
 39		crypto = [[DigestMd5 alloc] init] ;
 40	}
 41	return self;
 42}
 43
 44-(id)initWithApiKey:(NSString *)apikey andSicret:(NSString *)sicretvalue {
 45	self = [super init];
 46	if (self != nil) {
 47		crypto = [[DigestMd5 alloc] init] ;
 48		api_key = apikey;
 49		sicret = sicretvalue;
 50	}
 51	return self;
 52}
 53
 54-(NSString *)generateSign:(NSMutableArray *)params {
 55	if(sicret !=nil) {
 56		NSString *digest = sicret;
 57		[params addObject:[[NSRTMParam alloc] initWithKey:API_STRING value:api_key]];
 58		NSArray *sortedParams = [params sortedArrayUsingSelector:@selector(compare:)];
 59		int count = [sortedParams count];
 60		int i = 0;
 61		for (i = 0; i < count; i++) {
 62			digest = [digest stringByAppendingString:[[sortedParams objectAtIndex:i] description]];
 63		}
 64		
 65		DLog(@"TEST %@", digest);
 66		
 67		[crypto setClearTextWithString:digest];
 68		
 69		DLog(@"MD5 %@",[[crypto digest] hexval]);
 70		return [[crypto digest] hexval];
 71	} else {
 72		return nil;
 73	}
 74}
 75
 76-(NSURL *)generateURL:(NSMutableArray *)params {
 77	NSString * urlString = [NSString stringWithFormat:@"%@%@=%@",SERVICE_REST,API_STRING,api_key]; 
 78	
 79	int count = [params count];
 80	int i = 0;
 81	for (i = 0; i < count; i++) {
 82		urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"&%@",[[params objectAtIndex:i] keyValueURLformat]]];
 83	}
 84	
 85	NSString *sign = [self generateSign:params];
 86	urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"&%@=%@",SIGN_STRING,sign]];
 87	NSString *stringWithoutSpaces = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"\%20"];
 88	DLog(@"URL: %@",stringWithoutSpaces);
 89	return [NSURL URLWithString:stringWithoutSpaces];
 90}
 91
 92-(NSURL *)generateAuthURL:(NSMutableArray *)params {
 93	NSString * urlStringAuth = [NSString stringWithFormat:@"%@%@=%@",SERVICE_AUTH,API_STRING,api_key]; 
 94	
 95	int count = [params count];
 96	int i = 0;
 97	for (i = 0; i < count; i++) {
 98		urlStringAuth = [urlStringAuth stringByAppendingString:[NSString stringWithFormat:@"&%@",[[params objectAtIndex:i] keyValueURLformat]]];
 99	}
100	
101	NSString *sign = [self generateSign:params];
102	urlStringAuth = [urlStringAuth stringByAppendingString:[NSString stringWithFormat:@"&%@=%@",SIGN_STRING,sign]];
103	DLog(@"URL: %@",urlStringAuth);
104	DLog(@"URL: %@",[NSURL URLWithString:urlStringAuth]);
105	return [NSURL URLWithString:urlStringAuth];
106}
107
108
109-(void)setSicret:(NSString *)value {
110	sicret = value;
111}
112
113
114@end