Skip to content
Newer
Older
100644 132 lines (114 sloc) 4.33 KB
ee0461e converted request structure into system of subclasses of LFRequest
Matt Patenaude authored Nov 6, 2009
1 //
2 // LFBanRequest.m
2283d20 renamed Last.fm.framework to Scribbler
Matt Patenaude authored Nov 16, 2009
3 // Scribbler
ee0461e converted request structure into system of subclasses of LFRequest
Matt Patenaude authored Nov 7, 2009
4 //
5 // Created by Matt Patenaude on 11/6/09.
383b0a5 updated copyright statements to be more correct
Matt Patenaude authored Nov 8, 2009
6 // Copyright (C) 2009 {13bold}.
ee0461e converted request structure into system of subclasses of LFRequest
Matt Patenaude authored Nov 7, 2009
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 "LFBanRequest.h"
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
28 #import "LFTrack.h"
ee0461e converted request structure into system of subclasses of LFRequest
Matt Patenaude authored Nov 7, 2009
29
9c50778 Scribbler can now be combined with TouchXML for use on iPhone OS
Matt Patenaude authored Apr 21, 2010
30 #if LFUseTouchXML
31 #import "TouchXML.h"
32 #endif
33
ee0461e converted request structure into system of subclasses of LFRequest
Matt Patenaude authored Nov 7, 2009
34
35 @implementation LFBanRequest
36
37 #pragma mark Overridden methods
4b4b301 got very basics of authorization working properly
Matt Patenaude authored Nov 6, 2009
38 - (id)initWithTrack:(LFTrack *)theTrack
39 {
40 if (self = [super initWithTrack:theTrack])
41 {
42 requestType = LFRequestBan;
43 }
44 return self;
45 }
ee0461e converted request structure into system of subclasses of LFRequest
Matt Patenaude authored Nov 7, 2009
46 - (void)dispatch
47 {
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
48 // get the URL root
49 static NSString *__LFWebServiceURL = nil;
50 if (!__LFWebServiceURL)
256c3ba @mattpat Fixed a potential crash that never would have actually happened unles…
mattpat authored Nov 19, 2011
51 __LFWebServiceURL = [[[NSBundle bundleForClass:[self class]] objectForInfoDictionaryKey:@"LFWebServiceURL"] retain];
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
52
53 NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:
54 @"track.ban", @"method",
55 ([track title] != nil) ? [track title] : @"", @"track",
56 ([track artist] != nil) ? [track artist] : @"", @"artist",
57 [delegate APIKey], @"api_key",
58 [delegate sessionKey], @"sk",
59 nil];
60
61 NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:__LFWebServiceURL]];
62 [theRequest setHTTPMethod:@"POST"];
63 [theRequest setHTTPBody:[[self queryStringWithParameters:params sign:YES] dataUsingEncoding:NSUTF8StringEncoding]];
64 [params release];
65
66 if (connection)
67 {
68 [connection release];
69 connection = nil;
70 }
71 connection = [[NSURLConnection connectionWithRequest:theRequest delegate:self] retain];
ee0461e converted request structure into system of subclasses of LFRequest
Matt Patenaude authored Nov 7, 2009
72 }
73 - (void)connectionDidFinishLoading:(NSURLConnection *)theConnection
74 {
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
75 NSError *err;
9c50778 Scribbler can now be combined with TouchXML for use on iPhone OS
Matt Patenaude authored Apr 21, 2010
76 #if LFUseTouchXML
77 CXMLDocument *theResponse = [[CXMLDocument alloc] initWithData:responseData options:0 error:&err];
78 #else
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
79 NSXMLDocument *theResponse = [[NSXMLDocument alloc] initWithData:responseData options:0 error:&err];
9c50778 Scribbler can now be combined with TouchXML for use on iPhone OS
Matt Patenaude authored Apr 21, 2010
80 #endif
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
81
82 if (err)
83 {
b19b78e added failure counter to requests, will remove after 5 failures
Matt Patenaude authored Nov 14, 2009
84 failureCount++;
85
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
86 if (delegate && [delegate respondsToSelector:@selector(request:failedWithError:)])
87 [delegate request:self failedWithError:err];
88 return;
89 }
90
9c50778 Scribbler can now be combined with TouchXML for use on iPhone OS
Matt Patenaude authored Apr 21, 2010
91 #if LFUseTouchXML
92 CXMLElement *root = [theResponse rootElement];
93 #else
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
94 NSXMLElement *root = [theResponse rootElement];
9c50778 Scribbler can now be combined with TouchXML for use on iPhone OS
Matt Patenaude authored Apr 21, 2010
95 #endif
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
96 NSString *status = [[[root attributeForName:@"status"] stringValue] lowercaseString];
97
98 if ([status isEqualToString:@"ok"])
99 {
b19b78e added failure counter to requests, will remove after 5 failures
Matt Patenaude authored Nov 14, 2009
100 failureCount = 0;
101
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
102 if (delegate && [delegate respondsToSelector:@selector(requestSucceeded:)])
103 [delegate requestSucceeded:self];
104 }
105 else if ([status isEqualToString:@"failed"])
106 {
b19b78e added failure counter to requests, will remove after 5 failures
Matt Patenaude authored Nov 14, 2009
107 failureCount++;
108
9c50778 Scribbler can now be combined with TouchXML for use on iPhone OS
Matt Patenaude authored Apr 21, 2010
109 #if LFUseTouchXML
110 CXMLElement *errorNode = [[root elementsForName:@"error"] objectAtIndex:0];
111 #else
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
112 NSXMLElement *errorNode = [[root elementsForName:@"error"] objectAtIndex:0];
9c50778 Scribbler can now be combined with TouchXML for use on iPhone OS
Matt Patenaude authored Apr 21, 2010
113 #endif
114 NSError *theError = [NSError errorWithDomain:@"Last.fm" code:[[[errorNode attributeForName:@"code"] stringValue] integerValue] userInfo:[NSDictionary dictionaryWithObject:[errorNode stringValue] forKey:NSLocalizedDescriptionKey]];
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
115 if (delegate && [delegate respondsToSelector:@selector(request:failedWithError:)])
116 [delegate request:self failedWithError:theError];
117 }
118 else
119 {
b19b78e added failure counter to requests, will remove after 5 failures
Matt Patenaude authored Nov 14, 2009
120 failureCount++;
121
45d717f finished all remaining functionns; still rough, but fully working Las…
Matt Patenaude authored Nov 9, 2009
122 if (delegate && [delegate respondsToSelector:@selector(request:failedWithError:)])
123 [delegate request:self failedWithError:[NSError errorWithDomain:@"LFMFramework" code:0 userInfo:[NSDictionary dictionaryWithObject:@"An unknown error occurred." forKey:NSLocalizedDescriptionKey]]];
124 }
125
126 [theResponse release];
127 [connection release];
128 connection = nil;
ee0461e converted request structure into system of subclasses of LFRequest
Matt Patenaude authored Nov 7, 2009
129 }
130
131 @end
Something went wrong with that request. Please try again.