-
Notifications
You must be signed in to change notification settings - Fork 3
/
BeesClientBase.m
196 lines (142 loc) · 5.46 KB
/
BeesClientBase.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// BeesClientBase.m
// HudsonMobile
//
// Created by simone on 5/28/11.
// Copyright 2011 LMIT ltd. All rights reserved.
//
#import "BeesClientBase.h"
#include <CommonCrypto/CommonDigest.h>
@implementation BeesClientBase
-(id)initWithApiUrl:(NSString*)_serverApiUrl andApiKey:(NSString*)_apiKey andSecret:(NSString*)_secret andFormat:(NSString*)_format andVersion:(NSString*)_version{
self = [super init];
if (self) {
serverApiUrl = @"http://localhost:8080/api";
version = @"1.0";
sigVersion = @"1";
format = @"xml";
if(_serverApiUrl!=nil){
serverApiUrl = _serverApiUrl;
}
if(_version!=nil){
version = _version;
}
if(_format!=nil){
format = _format;
}
if(_secret!=nil){
secret = _secret;
}
if(_apiKey!=nil){
api_key = _apiKey;
}
}
return self;
}
-(NSString*) getRequestUrl:(NSString*) method andMethodParams:(NSDictionary*)params {
NSDictionary* urlParams = [self defaultParameters];
NSMutableString* requestURL = [[[NSMutableString alloc] initWithString:[self apiUrl:nil]] autorelease];
[requestURL appendString:@"?"];
if(params!=nil){
NSEnumerator* paramsKeyEnum = [params keyEnumerator];
NSString* paramName = [paramsKeyEnum nextObject];
while(paramName!=nil){
[urlParams setValue:[params valueForKey:paramName] forKey:paramName];
paramName = [paramsKeyEnum nextObject];
}
}
//if (asActionParam){ always YES for now
[urlParams setValue:method forKey:@"action"];
//}
NSString* signature = [self calculateSignature:urlParams];
NSEnumerator* entryNameEnumerator = [urlParams keyEnumerator];
NSString* entryName = [entryNameEnumerator nextObject];
int i=0;
while(entryName!=nil) {
NSString* key = entryName;
NSString* value = [urlParams valueForKey:entryName];
if (i > 0){
[requestURL appendString:@"&"];
}
[requestURL appendString:key];
[requestURL appendString:@"="];
[requestURL appendString:value];
i++;
entryName = [entryNameEnumerator nextObject];
}
[requestURL appendString:@"&"];
[requestURL appendString:@"sig"];
[requestURL appendString:@"="];
[requestURL appendString:signature];
return requestURL;
}
-(NSDictionary*) defaultParameters{
NSDictionary* result = [[[NSMutableDictionary alloc] init] autorelease];
NSString* timestamp = [NSString stringWithFormat:@"%d",(int)([[NSDate date] timeIntervalSince1970])];
[result setValue:format forKey:@"format"];
[result setValue:version forKey:@"v"];
[result setValue:api_key forKey:@"api_key"];
[result setValue:timestamp forKey:@"timestamp"];
[result setValue:sigVersion forKey:@"sig_version"];
return result;
}
-(NSString*) apiUrl:(NSString*) method {
NSMutableString* result = [[[NSMutableString alloc] init] autorelease];
[result appendString:serverApiUrl];
if(method!=nil){
[result appendString:@"/"];
[result appendString:method];
}
return result;
}
-(NSString*) getSignature:(NSString*) data :(NSString*) secret{
NSString* s = [NSString stringWithFormat:@"%@%@",data,secret];
NSString* result = [self md5:s];
return result;
}
-(NSString*) md5:(NSString*) message{
// Initialize the hash object
CC_MD5_CTX hashObject;
CC_MD5_Init(&hashObject);
const char* cString = [message cStringUsingEncoding:NSWindowsCP1252StringEncoding];
CC_MD5_Update(&hashObject,
(const void *)cString,
(CC_LONG)[message length]);
// Compute the hash digest
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(digest, &hashObject);
NSMutableString* result = [[NSMutableString alloc] init];
int j;
for(j=0;j<CC_MD5_DIGEST_LENGTH;j++){
[result appendFormat:@"%02x",digest[j]];
}
return result;
}
-(NSString*) calculateSignature:(NSDictionary*) entries{
NSMutableString* sigData = [[NSMutableString alloc] init];
NSMutableArray* unsortedEntriesKeys = [[NSMutableArray alloc] init];
NSEnumerator* keyEnum = [entries keyEnumerator];
id key = [keyEnum nextObject];
while(key!=nil){
[unsortedEntriesKeys addObject:key];
key = [keyEnum nextObject];
}
NSArray* sortedKeys = [unsortedEntriesKeys sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2){
NSString *strobj1 = (NSString*)obj1;
NSString *strobj2 = (NSString*)obj2;
return [strobj1 caseInsensitiveCompare:strobj2]; }];
for (int i=0; i<[sortedKeys count]; i++) {
[sigData appendString:[sortedKeys objectAtIndex:i]];
[sigData appendString:[entries valueForKey:[sortedKeys objectAtIndex:i]]];
}
NSString* signature = [self getSignature:sigData :secret];
[sigData release];
[unsortedEntriesKeys release];
return signature;
}
-(void) executeRequest:(NSString*) url withReceiver:(id<IWSDataReceiver>)dataReceiver{
[Logger debug:[NSString stringWithFormat:@"API call: %@", url]];
SimpleHTTPGetter* httpGetter = [[SimpleHTTPGetter alloc] init];
[httpGetter httpGETNoAuth:url :dataReceiver];
}
@end