Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 135 lines (115 sloc) 4.06 kb
ac9c63a Arkadiusz Buras Working with pin code
authored
1 //
2 // OAAsynchronousDataFetcher.m
3 // OAuthConsumer
4 //
5 // Created by Zsombor Szabó on 12/3/08.
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 // THE SOFTWARE.
24
25 #import "OAAsynchronousDataFetcher.h"
26
27 #import "OAServiceTicket.h"
28
29 @implementation OAAsynchronousDataFetcher
30
31 + (id)asynchronousFetcherWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector
32 {
33 return [[[OAAsynchronousDataFetcher alloc] initWithRequest:aRequest delegate:aDelegate didFinishSelector:finishSelector didFailSelector:failSelector] autorelease];
34 }
35
36 - (id)initWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector
37 {
38 if (self = [super init])
39 {
40 request = [aRequest retain];
41 delegate = aDelegate;
42 didFinishSelector = finishSelector;
43 didFailSelector = failSelector;
44 }
45 return self;
46 }
47
48 - (void)start
49 {
50 [request prepare];
51
52 if (connection)
53 [connection release];
54
55 connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
56
57 if (connection)
58 {
59 if (responseData)
60 [responseData release];
61 responseData = [[NSMutableData data] retain];
62 }
63 else
64 {
65 OAServiceTicket *ticket= [[OAServiceTicket alloc] initWithRequest:request
66 response:nil
67 didSucceed:NO];
68 [delegate performSelector:didFailSelector
69 withObject:ticket
70 withObject:nil];
71 [ticket release];
72 }
73 }
74
75 - (void)cancel
76 {
77 if (connection)
78 {
79 [connection cancel];
80 [connection release];
81 connection = nil;
82 }
83 }
84
85 - (void)dealloc
86 {
87 if (request) [request release];
88 if (connection) [connection release];
89 if (response) [response release];
90 if (responseData) [responseData release];
91 [super dealloc];
92 }
93
94 #pragma mark -
95 #pragma mark NSURLConnection methods
96
97 - (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)aResponse
98 {
99 if (response)
100 [response release];
101 response = [aResponse retain];
102 [responseData setLength:0];
103 }
104
105 - (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)data
106 {
107 [responseData appendData:data];
108 }
109
110 - (void)connection:(NSURLConnection *)aConnection didFailWithError:(NSError *)error
111 {
112 OAServiceTicket *ticket= [[OAServiceTicket alloc] initWithRequest:request
113 response:response
114 didSucceed:NO];
115 [delegate performSelector:didFailSelector
116 withObject:ticket
117 withObject:error];
118
119 [ticket release];
120 }
121
122 - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
123 {
124 OAServiceTicket *ticket = [[OAServiceTicket alloc] initWithRequest:request
125 response:response
126 didSucceed:[(NSHTTPURLResponse *)response statusCode] < 400];
127 [delegate performSelector:didFinishSelector
128 withObject:ticket
129 withObject:responseData];
130
131 [ticket release];
132 }
133
134 @end
Something went wrong with that request. Please try again.