Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 114 lines (99 sloc) 3.705 kb
ee0461e7 »
2009-11-06 converted request structure into system of subclasses of LFRequest
1 //
2 // LFGetTokenRequest.m
3 // Last.fm
4 //
5 // Created by Matt Patenaude on 11/6/09.
383b0a50 »
2009-11-08 updated copyright statements to be more correct
6 // Copyright (C) 2009 {13bold}.
ee0461e7 »
2009-11-06 converted request structure into system of subclasses of LFRequest
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 // THE SOFTWARE.
25 //
26
27 #import "LFGetTokenRequest.h"
28
29
30 @implementation LFGetTokenRequest
31
4b4b301c »
2009-11-06 got very basics of authorization working properly
32 #pragma mark Properties
33 @synthesize token;
34
ee0461e7 »
2009-11-06 converted request structure into system of subclasses of LFRequest
35 #pragma mark Overridden methods
4b4b301c »
2009-11-06 got very basics of authorization working properly
36 - (id)initWithTrack:(LFTrack *)theTrack
37 {
38 if (self = [super initWithTrack:theTrack])
39 {
40 requestType = LFRequestGetToken;
41 }
42 return self;
43 }
44 - (void)dealloc
45 {
46 [token release];
47 [super dealloc];
48 }
ee0461e7 »
2009-11-06 converted request structure into system of subclasses of LFRequest
49 - (void)dispatch
50 {
4b4b301c »
2009-11-06 got very basics of authorization working properly
51 NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:
52 @"auth.getToken", @"method",
53 [delegate APIKey], @"api_key",
54 nil];
55
56 NSURL *theURL = [self URLWithParameters:params sign:YES];
57 [params release];
58
59 NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL];
60
61 if (connection)
62 {
63 [connection release];
64 connection = nil;
65 }
66 connection = [[NSURLConnection connectionWithRequest:theRequest delegate:self] retain];
ee0461e7 »
2009-11-06 converted request structure into system of subclasses of LFRequest
67 }
68 - (void)connectionDidFinishLoading:(NSURLConnection *)theConnection
69 {
4b4b301c »
2009-11-06 got very basics of authorization working properly
70 NSError *err;
71 NSXMLDocument *theResponse = [[NSXMLDocument alloc] initWithData:responseData options:0 error:&err];
72
73 if (err)
74 {
75 if (delegate && [delegate respondsToSelector:@selector(request:failedWithError:)])
76 [delegate request:self failedWithError:err];
77 return;
78 }
79
80 NSXMLElement *root = [theResponse rootElement];
81 NSString *status = [[[root attributeForName:@"status"] stringValue] lowercaseString];
82
83 if ([status isEqualToString:@"ok"])
84 {
85 if (token)
86 {
87 [token release];
88 token = nil;
89 }
90 token = [[[[root elementsForName:@"token"] objectAtIndex:0] stringValue] copy];
91
92 if (delegate && [delegate respondsToSelector:@selector(requestSucceeded:)])
93 [delegate requestSucceeded:self];
94 }
95 else if ([status isEqualToString:@"failed"])
96 {
97 NSXMLElement *errorNode = [[root elementsForName:@"error"] objectAtIndex:0];
98 NSError *theError = [NSError errorWithDomain:@"Last.fm" code:[[[errorNode attributeForName:@"code"] objectValue] integerValue] userInfo:[NSDictionary dictionaryWithObject:[errorNode stringValue] forKey:NSLocalizedDescriptionKey]];
99 if (delegate && [delegate respondsToSelector:@selector(request:failedWithError:)])
100 [delegate request:self failedWithError:theError];
101 }
102 else
103 {
104 if (delegate && [delegate respondsToSelector:@selector(request:failedWithError:)])
105 [delegate request:self failedWithError:[NSError errorWithDomain:@"LFMFramework" code:0 userInfo:[NSDictionary dictionaryWithObject:@"An unknown error occurred." forKey:NSLocalizedDescriptionKey]]];
106 }
107
108 [theResponse release];
109 [connection release];
110 connection = nil;
ee0461e7 »
2009-11-06 converted request structure into system of subclasses of LFRequest
111 }
112
113 @end
Something went wrong with that request. Please try again.