changed git call from https to git readonly
[atutor.git] / mods / atsocial_iphone_app / OAuth / OAAsynchronousDataFetcher.m
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     [connection cancel];
79 }
80
81 - (void)dealloc
82 {
83   if (request) [request release];
84   if (connection) [connection release];
85   if (response) [response release];
86   if (responseData) [responseData release];
87   [super dealloc];
88 }
89
90 #pragma mark -
91 #pragma mark NSURLConnection's delegate methods
92
93 - (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)aResponse
94 {
95   if (response)
96     [response release];
97   response = [aResponse retain];
98   [responseData setLength:0];
99 }
100
101 - (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)data
102 {
103   [responseData appendData:data];
104 }
105
106 - (void)connection:(NSURLConnection *)aConnection didFailWithError:(NSError *)error
107 {
108   OAServiceTicket *ticket= [[OAServiceTicket alloc] initWithRequest:request
109                                                            response:response
110                                                          didSucceed:NO];
111   [delegate performSelector:didFailSelector
112                  withObject:ticket
113                  withObject:error];
114   
115   [ticket release];
116 }
117
118 - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
119 {
120   OAServiceTicket *ticket = [[OAServiceTicket alloc] initWithRequest:request
121                                                             response:response
122                                                           didSucceed:[(NSHTTPURLResponse *)response statusCode] < 400];
123   [delegate performSelector:didFinishSelector
124                  withObject:ticket
125                  withObject:responseData];
126   
127   [ticket release];
128 }
129
130 @end
131