-
Notifications
You must be signed in to change notification settings - Fork 4
/
LFWebService.m
303 lines (245 loc) · 8.93 KB
/
LFWebService.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//
// LFWebService.m
// LastHistory
//
// Created by Frederik Seiffert on 28.11.08.
// Copyright 2008 Frederik Seiffert. All rights reserved.
//
#import "LFWebService.h"
#import "NSString+MD5.h"
#define LF_DEBUG 0
#define LF_ROOT_URL @"http://ws.audioscrobbler.com/2.0/"
#define LF_AUTHENTICATE_URL @"http://www.last.fm/api/auth/?api_key=%@&token=%@"
NSString *LFWebServiceErrorDomain = @"LFWebServiceErrorDomain";
// private read-write properties
@interface LFWebService ()
@property (readwrite, copy) NSString *apiKey;
@property (readwrite, copy) NSString *secret;
@property (readwrite, copy) NSString *userName;
@property (readwrite, copy) NSString *sessionKey;
@end
@interface LFWebService (LFPrivateAPI)
- (NSXMLDocument *)callMethod:(NSString *)methodName withParameters:(NSDictionary *)params authenticated:(BOOL)auth error:(NSError **)outError;
- (NSString *)apiSignatureForParameters:(NSDictionary *)params;
- (NSString *)stringFromParameters:(NSDictionary *)params;
- (NSString *)urlEncodeValue:(NSString *)value;
@end
@implementation LFWebService
@synthesize apiKey=_apiKey;
@synthesize secret=_secret;
@synthesize userName=_userName;
@synthesize sessionKey=_sessionKey;
- (id)initWithApiKey:(NSString *)apiKey secret:(NSString *)secret
{
return [self initWithApiKey:apiKey secret:secret userName:nil sessionKey:nil];
}
- (id)initWithApiKey:(NSString *)apiKey
secret:(NSString *)secret
userName:(NSString *)userName
sessionKey:(NSString *)sessionKey
{
self = [super init];
if (self != nil) {
self.apiKey = apiKey;
self.secret = secret;
self.userName = userName;
self.sessionKey = sessionKey;
}
return self;
}
- (void)dealloc
{
[_apiKey release];
[_secret release];
[_userName release];
[_sessionKey release];
[_token release];
[super dealloc];
}
- (BOOL)isAuthenticated
{
return self.userName.length != 0 && self.sessionKey.length != 0;
}
- (NSString *)authenticateGetToken
{
[_token release], _token = nil;
NSXMLDocument *xml = [self callMethod:@"auth.getToken" withParameters:nil error:nil];
if (!xml)
return nil;
NSXMLElement *token = [[[xml rootElement] elementsForName:@"token"] lastObject];
if (!token) {
NSLog(@"Error: unable to get token");
return nil;
}
_token = [[token stringValue] copy];
return _token;
}
- (NSURL *)authenticateGetAuthorizationURL
{
NSString *token = [self authenticateGetToken];
if (!token)
return nil;
NSString *urlStr = [NSString stringWithFormat:LF_AUTHENTICATE_URL, self.apiKey, token];
return [NSURL URLWithString:urlStr];
}
- (BOOL)authenticateFinish
{
// we need a token for auth.getSession
if (!_token)
return NO;
self.userName = nil;
self.sessionKey = nil;
NSXMLDocument *xml = [self callMethod:@"auth.getSession"
withParameters:[NSDictionary dictionaryWithObject:_token forKey:@"token"]
error:nil];
if (!xml)
return NO;
NSXMLElement *session = [[[xml rootElement] elementsForName:@"session"] lastObject];
if (!session) {
NSLog(@"Error: unable to get session");
return NO;
}
NSXMLElement *nameElement = [[session elementsForName:@"name"] lastObject];
NSXMLElement *keyElement = [[session elementsForName:@"key"] lastObject];
if (nameElement && keyElement) {
self.userName = [nameElement stringValue];
self.sessionKey = [keyElement stringValue];
// no need for token any more
[_token release], _token = nil;
return YES;
}
return NO;
}
- (NSXMLDocument *)callMethod:(NSString *)methodName withParameters:(NSDictionary *)params error:(NSError **)outError
{
return [self callMethod:methodName withParameters:params authenticated:NO error:outError];
}
- (NSXMLDocument *)callAuthenticatedMethod:(NSString *)methodName withParameters:(NSDictionary *)params error:(NSError **)outError
{
return [self callMethod:methodName withParameters:params authenticated:YES error:outError];
}
@end
@implementation LFWebService (LFPrivateAPI)
- (NSXMLDocument *)callMethod:(NSString *)methodName withParameters:(NSDictionary *)params authenticated:(BOOL)auth error:(NSError **)outError
{
if (auth && !self.sessionKey) {
NSLog(@"Error: no session key for authenticated call");
NSDictionary *errorUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"No session key available for authenticated call", nil), NSLocalizedDescriptionKey,
NSLocalizedString(@"Please authenticate the application with Last.fm.", nil), NSLocalizedRecoverySuggestionErrorKey,
nil];
NSError *error = [NSError errorWithDomain:LFWebServiceErrorDomain code:0 userInfo:errorUserInfo];
if (outError)
*outError = error;
return nil;
}
NSMutableDictionary *paramsDict = params ?
[NSMutableDictionary dictionaryWithDictionary:params] :
[NSMutableDictionary dictionaryWithCapacity:3];
if (auth || self.isAuthenticated)
[paramsDict setValue:self.sessionKey forKey:@"sk"];
[paramsDict setValue:self.apiKey forKey:@"api_key"];
[paramsDict setValue:methodName forKey:@"method"];
[paramsDict setValue:[self apiSignatureForParameters:paramsDict] forKey:@"api_sig"];
NSString *paramsStr = [self stringFromParameters:paramsDict];
#if LF_DEBUG
NSLog(@"params: %@", paramsStr);
#endif
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:LF_ROOT_URL]];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[paramsStr dataUsingEncoding:NSUTF8StringEncoding]];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:req
returningResponse:&response
error:&error];
if (![responseData length] || error) {
if (!error) {
NSDictionary *errorUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSHTTPURLResponse localizedStringForStatusCode:[response statusCode]], NSLocalizedDescriptionKey, nil];
error = [NSError errorWithDomain:LFWebServiceErrorDomain code:[response statusCode] userInfo:errorUserInfo];
}
NSLog(@"Error retrieving data: %@ (%d)", [error localizedDescription], [error code]);
if (outError)
*outError = error;
return nil;
}
#if LF_DEBUG
// NSLog(@"Response Code: %d", [response statusCode]);
// NSLog(@"Content-Type: %@", [[response allHeaderFields] objectForKey:@"Content-Type"]);
#endif
NSString *responseStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
#if LF_DEBUG
NSLog(@"Response: %@", responseStr);
#endif
NSXMLDocument *result = [[[NSXMLDocument alloc] initWithXMLString:responseStr
options:0
error:&error] autorelease];
[responseStr release];
if (!result) {
NSLog(@"Error in XML: %@ (%d)", [error localizedDescription], [error code]);
NSLog(@"Response:\n%@", responseStr);
if (outError)
*outError = error;
return nil;
}
NSXMLElement *rootElement = [result rootElement];
if ([[rootElement name] isEqualToString:@"lfm"])
{
if (![[[rootElement attributeForName:@"status"] stringValue] isEqualToString:@"ok"])
{
NSXMLElement *errorElement = [[rootElement elementsForName:@"error"] lastObject];
NSInteger errorCode = [[[errorElement attributeForName:@"code"] stringValue] integerValue];
NSString *errorDesc = [errorElement stringValue];
NSLog(@"Error from Last.fm: %@ (%d)", errorDesc, errorCode);
NSDictionary *errorUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:errorDesc, NSLocalizedDescriptionKey, nil];
NSError *error = [NSError errorWithDomain:LFWebServiceErrorDomain code:errorCode userInfo:errorUserInfo];
if (outError)
*outError = error;
return nil;
}
}
else
{
NSLog(@"Error: invalid response (%@)", responseStr);
NSDictionary *errorUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Invalid response from Last.fm", nil), NSLocalizedDescriptionKey, nil];
NSError *error = [NSError errorWithDomain:LFWebServiceErrorDomain code:0 userInfo:errorUserInfo];
if (outError)
*outError = error;
return nil;
}
return result;
}
- (NSString *)apiSignatureForParameters:(NSDictionary *)params
{
NSMutableString *signatureStr = [NSMutableString string];
for (NSString *key in [[params allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
NSString *value = [params valueForKey:key];
[signatureStr appendString:key];
[signatureStr appendString:value];
}
[signatureStr appendString:self.secret];
return [signatureStr md5];
}
- (NSString *)stringFromParameters:(NSDictionary *)params
{
NSMutableString *paramsStr = [NSMutableString string];
for (NSString *key in [params allKeys]) {
NSString *value = [params valueForKey:key];
if ([paramsStr length] > 0)
[paramsStr appendString:@"&"];
[paramsStr appendFormat:@"%@=%@", key, [self urlEncodeValue:value]];
}
return paramsStr;
}
- (NSString *)urlEncodeValue:(NSString *)value
{
CFStringRef result = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)value,
NULL,
CFSTR("?=&+"),
kCFStringEncodingUTF8);
if (result)
CFMakeCollectable(result);
return [(NSString *)result autorelease];
}
@end