forked from 13bold/Scribbler
/
LFGetSessionRequest.m
155 lines (134 loc) · 4.56 KB
/
LFGetSessionRequest.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
//
// LFGetSessionRequest.m
// Scribbler
//
// Created by Matt Patenaude on 11/6/09.
// Copyright (C) 2009 {13bold}.
//
// 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.
//
#import "LFGetSessionRequest.h"
#if LFUseTouchXML
#import "TouchXML.h"
#endif
@implementation LFGetSessionRequest
#pragma mark Properties
@synthesize token;
@synthesize sessionUser;
@synthesize sessionKey;
#pragma mark Overridden methods
- (id)initWithTrack:(LFTrack *)theTrack
{
if (self = [super initWithTrack:theTrack])
{
requestType = LFRequestGetSession;
}
return self;
}
- (void)dealloc
{
[token release];
[sessionUser release];
[sessionKey release];
[super dealloc];
}
- (void)dispatch
{
NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:
@"auth.getSession", @"method",
token, @"token",
[delegate APIKey], @"api_key",
nil];
NSURL *theURL = [self URLWithParameters:params sign:YES];
[params release];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL];
if (connection)
{
[connection release];
connection = nil;
}
connection = [[NSURLConnection connectionWithRequest:theRequest delegate:self] retain];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection
{
NSError *err;
#if LFUseTouchXML
CXMLDocument *theResponse = [[CXMLDocument alloc] initWithData:responseData options:0 error:&err];
#else
NSXMLDocument *theResponse = [[NSXMLDocument alloc] initWithData:responseData options:0 error:&err];
#endif
if (err)
{
failureCount++;
if (delegate && [delegate respondsToSelector:@selector(request:failedWithError:)])
[delegate request:self failedWithError:err];
return;
}
#if LFUseTouchXML
CXMLElement *root = [theResponse rootElement];
#else
NSXMLElement *root = [theResponse rootElement];
#endif
NSString *status = [[[root attributeForName:@"status"] stringValue] lowercaseString];
if ([status isEqualToString:@"ok"])
{
failureCount = 0;
#if LFUseTouchXML
CXMLElement *session = [[root elementsForName:@"session"] objectAtIndex:0];
#else
NSXMLElement *session = [[root elementsForName:@"session"] objectAtIndex:0];
#endif
if (sessionUser)
{
[sessionUser release];
sessionUser = nil;
}
sessionUser = [[[[session elementsForName:@"name"] objectAtIndex:0] stringValue] copy];
if (sessionKey)
{
[sessionKey release];
sessionKey = nil;
}
sessionKey = [[[[session elementsForName:@"key"] objectAtIndex:0] stringValue] copy];
if (delegate && [delegate respondsToSelector:@selector(requestSucceeded:)])
[delegate requestSucceeded:self];
}
else if ([status isEqualToString:@"failed"])
{
failureCount++;
#if LFUseTouchXML
CXMLElement *errorNode = [[root elementsForName:@"error"] objectAtIndex:0];
#else
NSXMLElement *errorNode = [[root elementsForName:@"error"] objectAtIndex:0];
#endif
NSError *theError = [NSError errorWithDomain:@"Last.fm" code:[[[errorNode attributeForName:@"code"] stringValue] integerValue] userInfo:[NSDictionary dictionaryWithObject:[errorNode stringValue] forKey:NSLocalizedDescriptionKey]];
if (delegate && [delegate respondsToSelector:@selector(request:failedWithError:)])
[delegate request:self failedWithError:theError];
}
else
{
failureCount++;
if (delegate && [delegate respondsToSelector:@selector(request:failedWithError:)])
[delegate request:self failedWithError:[NSError errorWithDomain:@"LFMFramework" code:0 userInfo:[NSDictionary dictionaryWithObject:@"An unknown error occurred." forKey:NSLocalizedDescriptionKey]]];
}
[theResponse release];
[connection release];
connection = nil;
}
@end