/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. #import "NSRTMInvoker.h"
  22. #import "DebugLog.h"
  23. #import "NSRTMParam.h"
  24. #import "NSDataMd5.h"
  25. #import "DigestMd5.h"
  26. #define SERVICE_REST @"http://api.rememberthemilk.com/services/rest/?"
  27. #define SERVICE_AUTH @"http://www.rememberthemilk.com/services/auth/?"
  28. #define API_STRING @"api_key"
  29. #define SIGN_STRING @"api_sig"
  30. @implementation NSRTMInvoker
  31. -(id) init
  32. {
  33. self = [super init];
  34. if (self != nil) {
  35. crypto = [[DigestMd5 alloc] init] ;
  36. }
  37. return self;
  38. }
  39. -(id)initWithApiKey:(NSString *)apikey andSicret:(NSString *)sicretvalue {
  40. self = [super init];
  41. if (self != nil) {
  42. crypto = [[DigestMd5 alloc] init] ;
  43. api_key = apikey;
  44. sicret = sicretvalue;
  45. }
  46. return self;
  47. }
  48. -(NSString *)generateSign:(NSMutableArray *)params {
  49. if(sicret !=nil) {
  50. NSString *digest = sicret;
  51. [params addObject:[[NSRTMParam alloc] initWithKey:API_STRING value:api_key]];
  52. NSArray *sortedParams = [params sortedArrayUsingSelector:@selector(compare:)];
  53. int count = [sortedParams count];
  54. int i = 0;
  55. for (i = 0; i < count; i++) {
  56. digest = [digest stringByAppendingString:[[sortedParams objectAtIndex:i] description]];
  57. }
  58. DLog(@"TEST %@", digest);
  59. [crypto setClearTextWithString:digest];
  60. DLog(@"MD5 %@",[[crypto digest] hexval]);
  61. return [[crypto digest] hexval];
  62. } else {
  63. return nil;
  64. }
  65. }
  66. -(NSURL *)generateURL:(NSMutableArray *)params {
  67. NSString * urlString = [NSString stringWithFormat:@"%@%@=%@",SERVICE_REST,API_STRING,api_key];
  68. int count = [params count];
  69. int i = 0;
  70. for (i = 0; i < count; i++) {
  71. urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"&%@",[[params objectAtIndex:i] keyValueURLformat]]];
  72. }
  73. NSString *sign = [self generateSign:params];
  74. urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"&%@=%@",SIGN_STRING,sign]];
  75. NSString *stringWithoutSpaces = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"\%20"];
  76. DLog(@"URL: %@",stringWithoutSpaces);
  77. return [NSURL URLWithString:stringWithoutSpaces];
  78. }
  79. -(NSURL *)generateAuthURL:(NSMutableArray *)params {
  80. NSString * urlStringAuth = [NSString stringWithFormat:@"%@%@=%@",SERVICE_AUTH,API_STRING,api_key];
  81. int count = [params count];
  82. int i = 0;
  83. for (i = 0; i < count; i++) {
  84. urlStringAuth = [urlStringAuth stringByAppendingString:[NSString stringWithFormat:@"&%@",[[params objectAtIndex:i] keyValueURLformat]]];
  85. }
  86. NSString *sign = [self generateSign:params];
  87. urlStringAuth = [urlStringAuth stringByAppendingString:[NSString stringWithFormat:@"&%@=%@",SIGN_STRING,sign]];
  88. DLog(@"URL: %@",urlStringAuth);
  89. DLog(@"URL: %@",[NSURL URLWithString:urlStringAuth]);
  90. return [NSURL URLWithString:urlStringAuth];
  91. }
  92. -(void)setSicret:(NSString *)value {
  93. sicret = value;
  94. }
  95. @end