-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCCall.m
286 lines (224 loc) · 9.5 KB
/
RCCall.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// RCCall.m
// RestClient
//
// Created by Alberto García Hierro on 16/04/09.
// Copyright 2009 Alberto García Hierro. All rights reserved.
//
#import "RCManager.h"
#import "RCParameter.h"
#import "RCCall.h"
@interface NSObject (RCCall)
- (NSURLRequest *)call:(RCCall *)call willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response;
@end
static NSString *Methods[] = {
@"HEAD",
@"GET",
@"POST",
@"PUT",
@"DELETE",
};
@implementation RCCall
@synthesize manager = manager_;
@synthesize callURL = callURL_;
@synthesize callMethod = callMethod_;
@synthesize parameters = parameters_;
@synthesize delegate = delegate_;
@synthesize didFinishSelector = didFinishSelector_;
@synthesize didFailSelector = didFailSelector_;
@synthesize request = request_;
@synthesize bodyEncoding = bodyEncoding_;
@synthesize response = response_;
@synthesize responseBody = responseBody_;
@synthesize responseError = responseError_;
@synthesize context = context_;
- (id)init {
return [self initWithCallURL:nil method:kRCCallMethodGET parameters:nil
delegate:nil didFinishSelector:0 didFailSelector:0];
}
- (id)initWithCallURL:(NSString *)theCallURL parameters:(NSArray *)parameters {
return [self initWithCallURL:theCallURL parameters:parameters didFinishSelector:NULL didFailSelector:NULL];
}
- (id)initWithCallURL:(NSString *)theCallURL method:(RCCallMethod)theMethod
parameters:(NSArray *)theParameters didFinishSelector:(SEL)didFinishSelector
didFailSelector:(SEL)didFailSelector {
return [self initWithCallURL:theCallURL method:theMethod parameters:theParameters
delegate:nil didFinishSelector:didFinishSelector didFailSelector:didFailSelector];
}
- (id)initWithCallURL:(NSString *)theCallURL parameters:(NSArray *)theParameters
didFinishSelector:(SEL)didFinishSelector didFailSelector:(SEL)didFailSelector {
return [self initWithCallURL:theCallURL method:kRCCallMethodGET parameters:theParameters
didFinishSelector:didFinishSelector didFailSelector:didFailSelector];
}
- (id)initWithCallURL:(NSString *)theCallURL method:(RCCallMethod)theMethod
didFinishSelector:(SEL)didFinishSelector didFailSelector:(SEL)didFailSelector {
return [self initWithCallURL:theCallURL method:theMethod parameters:nil
didFinishSelector:didFinishSelector didFailSelector:didFailSelector];
}
- (id)initWithCallURL:(NSString *)theCallURL didFinishSelector:(SEL)didFinishSelector
didFailSelector:(SEL)didFailSelector {
return [self initWithCallURL:theCallURL method:kRCCallMethodGET parameters:nil
didFinishSelector:didFinishSelector didFailSelector:didFailSelector];
}
- (id)initWithCallURL:(NSString *)theCallURL method:(RCCallMethod)theMethod
parameters:(NSArray *)theParameters delegate:(id <NSObject>)delegate
didFinishSelector:(SEL)didFinishSelector didFailSelector:(SEL)didFailSelector {
if (self = [super init]) {
self.callURL = theCallURL;
self.callMethod = theMethod;
self.parameters = theParameters;
self.delegate = delegate;
self.didFinishSelector = didFinishSelector;
self.didFailSelector = didFailSelector;
self.bodyEncoding = NSUTF8StringEncoding;
request_ = [NSMutableURLRequest new];
}
return self;
}
- (void)dealloc {
[callURL_ release];
[parameters_ release];
[request_ release];
[response_ release];
[responseBody_ release];
[connection_ cancel];
[connection_ release];
[responseError_ release];
[super dealloc];
}
- (NSInteger)responseCode {
return [response_ statusCode];
}
- (NSString *)responseBodyString {
NSString *value = [[NSString alloc] initWithData:responseBody_ encoding:self.bodyEncoding];
return [value autorelease];
}
- (BOOL)didSucceed {
return (!responseError_ && self.responseCode >= 200 && self.responseCode < 300);
}
- (void)reset {
[request_ release];
request_ = [NSMutableURLRequest new];
}
- (void)prepareRequest {
[self.request setURL:[NSURL URLWithString:callURL_]];
[self.request setHTTPMethod:Methods[self.callMethod]];
for (RCParameter *parameter in parameters_) {
[parameter attachToCall:self];
}
}
- (void)perform {
[self prepareRequest];
connection_ = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:YES];
#ifdef RESTCLIENT_DEBUG
NSString *body = [[NSString alloc] initWithData:self.request.HTTPBody encoding:NSUTF8StringEncoding];
NSLog(@"Calling %@ (%@) %@", self.request.URL, self.request.HTTPMethod, body);
[body release];
#endif
}
- (void)cancel {
[connection_ cancel];
}
#pragma mark -
#pragma mark NSURLConnectionDelegate
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse {
NSURLRequest *returnedRequest = request;
if ([self.delegate respondsToSelector:@selector(call:willSendRequest:redirectResponse:)]) {
returnedRequest = [(id)self.delegate call:self willSendRequest:request redirectResponse:redirectResponse];
}
if (returnedRequest) {
NSMutableURLRequest *mutableRequest = [returnedRequest mutableCopy];
[request_ release];
request_ = mutableRequest;
if (redirectResponse) {
self.callURL = mutableRequest.URL.absoluteString;
}
}
return returnedRequest;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[response_ release];
response_ = [response retain];
[responseBody_ release];
responseBody_ = [NSMutableData new];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseBody_ appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
/* Disable the cache */
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
#ifdef RESTCLIENT_DEBUG
NSLog(@"Received from %@ (%d) '%@'", self.callURL, self.responseCode, self.responseBodyString);
#endif
[self.delegate performSelector:self.didFinishSelector withObject:self];
[self.manager callDidComplete:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
#ifdef RESTCLIENT_DEBUG
NSLog(@"Call to %@ failed with error '%@'", self.callURL, [error localizedDescription]);
#endif
responseError_ = [error retain];
[self.delegate performSelector:self.didFailSelector ? self.didFailSelector : self.didFinishSelector withObject:self];
[self.manager callDidComplete:self];
}
#pragma mark Class methods
+ (id)callWithCallURL:(NSString *)theCallURL parameters:(NSArray *)parameters {
return [[self class] callWithCallURL:theCallURL parameters:parameters
didFinishSelector:NULL didFailSelector:NULL];
}
+ (id)callWithCallURL:(NSString *)theCallURL method:(RCCallMethod)theMethod
parameters:(NSArray *)theParameters didFinishSelector:(SEL)didFinishSelector
didFailSelector:(SEL)didFailSelector {
return [[self class] callWithCallURL:theCallURL method:theMethod parameters:theParameters
delegate:nil didFinishSelector:didFinishSelector didFailSelector:didFailSelector];
}
+ (id)callWithCallURL:(NSString *)theCallURL parameters:(NSArray *)theParameters
didFinishSelector:(SEL)didFinishSelector didFailSelector:(SEL)didFailSelector {
return [[self class] callWithCallURL:theCallURL method:kRCCallMethodGET parameters:theParameters
didFinishSelector:didFinishSelector didFailSelector:didFailSelector];
}
+ (id)callWithCallURL:(NSString *)theCallURL parameters:(NSArray *)theParameters
didFinishSelector:(SEL)didFinishSelector {
return [[self class] callWithCallURL:theCallURL method:kRCCallMethodGET parameters:theParameters
didFinishSelector:didFinishSelector didFailSelector:NULL];
}
+ (id)callWithCallURL:(NSString *)theCallURL didFinishSelector:(SEL)didFinishSelector {
return [[self class] callWithCallURL:theCallURL method:kRCCallMethodGET parameters:nil
didFinishSelector:didFinishSelector didFailSelector:NULL];
}
+ (id)callWithCallURL:(NSString *)theCallURL method:(RCCallMethod)theMethod
didFinishSelector:(SEL)didFinishSelector didFailSelector:(SEL)didFailSelector {
return [[self class] callWithCallURL:theCallURL method:theMethod parameters:nil
didFinishSelector:didFinishSelector didFailSelector:didFailSelector];
}
+ (id)callWithCallURL:(NSString *)theCallURL didFinishSelector:(SEL)didFinishSelector
didFailSelector:(SEL)didFailSelector {
return [[self class] callWithCallURL:theCallURL method:kRCCallMethodGET parameters:nil
didFinishSelector:didFinishSelector didFailSelector:didFailSelector];
}
+ (id)callWithCallURL:(NSString *)theCallURL method:(RCCallMethod)theMethod
parameters:(NSArray *)theParameters delegate:(id <NSObject>)delegate
didFinishSelector:(SEL)didFinishSelector didFailSelector:(SEL)didFailSelector {
return [[[[self class] alloc] initWithCallURL:theCallURL method:theMethod parameters:theParameters
delegate:delegate didFinishSelector:didFinishSelector
didFailSelector:didFailSelector] autorelease];
}
+ (id)callWithCallURL:(NSString *)theCallURL delegate:(id <NSObject>)delegate
didFinishSelector:(SEL)didFinishSelector {
return [[[[self class] alloc] initWithCallURL:theCallURL method:kRCCallMethodGET
parameters:nil delegate:delegate
didFinishSelector:didFinishSelector
didFailSelector:NULL] autorelease];
}
+ (id)callWithCallURL:(NSString *)theCallURL parameters:(NSArray *)parameters
delegate:(id <NSObject>)delegate didFinishSelector:(SEL)didFinishSelector {
return [[[[self class] alloc] initWithCallURL:theCallURL method:kRCCallMethodGET
parameters:parameters delegate:delegate
didFinishSelector:didFinishSelector
didFailSelector:NULL] autorelease];
}
@end