2011-07-27 15:14:15 -05:00
|
|
|
// AFJSONRequestOperation.m
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2011 Gowalla (http://gowalla.com/)
|
2013-01-27 12:15:07 -05:00
|
|
|
//
|
2011-07-27 15:14:15 -05:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
2013-01-27 12:15:07 -05:00
|
|
|
//
|
2011-07-27 15:14:15 -05:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2013-01-27 12:15:07 -05:00
|
|
|
//
|
2011-07-27 15:14:15 -05:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
#import "AFJSONRequestOperation.h"
|
2011-10-11 12:26:49 -05:00
|
|
|
|
2011-08-14 19:46:33 -05:00
|
|
|
static dispatch_queue_t af_json_request_operation_processing_queue;
|
|
|
|
|
static dispatch_queue_t json_request_operation_processing_queue() {
|
|
|
|
|
if (af_json_request_operation_processing_queue == NULL) {
|
2011-10-04 00:13:12 -05:00
|
|
|
af_json_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.json-request.processing", 0);
|
2011-08-14 19:46:33 -05:00
|
|
|
}
|
2013-01-27 12:15:07 -05:00
|
|
|
|
2011-08-14 19:46:33 -05:00
|
|
|
return af_json_request_operation_processing_queue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-30 12:25:22 -08:00
|
|
|
@interface AFURLConnectionOperation ()
|
|
|
|
|
@property (readwrite, nonatomic, strong) NSRecursiveLock *lock;
|
|
|
|
|
@end
|
|
|
|
|
|
2011-10-04 00:13:12 -05:00
|
|
|
@interface AFJSONRequestOperation ()
|
2012-10-09 09:20:18 -07:00
|
|
|
@property (readwrite, nonatomic, strong) id responseJSON;
|
|
|
|
|
@property (readwrite, nonatomic, strong) NSError *JSONError;
|
2011-10-04 00:13:12 -05:00
|
|
|
@end
|
2011-07-27 15:14:15 -05:00
|
|
|
|
2011-10-04 00:13:12 -05:00
|
|
|
@implementation AFJSONRequestOperation
|
|
|
|
|
@synthesize responseJSON = _responseJSON;
|
2012-10-31 08:01:22 -07:00
|
|
|
@synthesize JSONReadingOptions = _JSONReadingOptions;
|
2011-10-19 22:33:09 -07:00
|
|
|
@synthesize JSONError = _JSONError;
|
2011-10-04 00:13:12 -05:00
|
|
|
|
2012-12-23 10:59:56 +02:00
|
|
|
+ (instancetype)JSONRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
|
|
|
|
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
|
|
|
|
|
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure
|
2011-07-27 15:14:15 -05:00
|
|
|
{
|
2012-12-17 22:59:43 +11:00
|
|
|
AFJSONRequestOperation *requestOperation = [(AFJSONRequestOperation *)[self alloc] initWithRequest:urlRequest];
|
2011-10-24 13:08:58 -05:00
|
|
|
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
|
|
|
|
if (success) {
|
|
|
|
|
success(operation.request, operation.response, responseObject);
|
2011-10-05 12:36:45 -05:00
|
|
|
}
|
2011-10-24 13:08:58 -05:00
|
|
|
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
|
|
|
if (failure) {
|
|
|
|
|
failure(operation.request, operation.response, error, [(AFJSONRequestOperation *)operation responseJSON]);
|
2011-07-27 15:14:15 -05:00
|
|
|
}
|
2011-10-24 13:08:58 -05:00
|
|
|
}];
|
2013-01-27 12:15:07 -05:00
|
|
|
|
2011-10-24 13:08:58 -05:00
|
|
|
return requestOperation;
|
2011-10-04 00:13:12 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-03 11:31:00 -05:00
|
|
|
|
2011-10-04 00:13:12 -05:00
|
|
|
- (id)responseJSON {
|
2013-01-30 12:25:22 -08:00
|
|
|
[self.lock lock];
|
2012-04-25 16:02:29 +08:00
|
|
|
if (!_responseJSON && [self.responseData length] > 0 && [self isFinished] && !self.JSONError) {
|
2011-10-05 12:36:45 -05:00
|
|
|
NSError *error = nil;
|
2011-10-05 14:14:52 -05:00
|
|
|
|
2012-11-30 14:40:53 -08:00
|
|
|
// Workaround for behavior of Rails to return a single space for `head :ok` (a workaround for a bug in Safari), which is not interpreted as valid input by NSJSONSerialization.
|
|
|
|
|
// See https://github.com/rails/rails/issues/1742
|
|
|
|
|
if ([self.responseData length] == 0 || [self.responseString isEqualToString:@" "]) {
|
2011-10-12 16:42:31 -07:00
|
|
|
self.responseJSON = nil;
|
2011-10-05 14:14:52 -05:00
|
|
|
} else {
|
2012-11-30 14:12:59 -08:00
|
|
|
// Workaround for a bug in NSJSONSerialization when Unicode character escape codes are used instead of the actual character
|
|
|
|
|
// See http://stackoverflow.com/a/12843465/157142
|
|
|
|
|
NSData *JSONData = [self.responseString dataUsingEncoding:self.responseStringEncoding];
|
|
|
|
|
self.responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:self.JSONReadingOptions error:&error];
|
2011-10-12 16:42:31 -07:00
|
|
|
}
|
2013-01-27 12:15:07 -05:00
|
|
|
|
2011-10-19 22:33:09 -07:00
|
|
|
self.JSONError = error;
|
2011-10-04 00:13:12 -05:00
|
|
|
}
|
2013-01-30 12:25:22 -08:00
|
|
|
[self.lock unlock];
|
2013-01-27 12:15:07 -05:00
|
|
|
|
2011-10-04 00:13:12 -05:00
|
|
|
return _responseJSON;
|
2011-08-03 11:31:00 -05:00
|
|
|
}
|
|
|
|
|
|
2011-10-19 22:33:09 -07:00
|
|
|
- (NSError *)error {
|
|
|
|
|
if (_JSONError) {
|
|
|
|
|
return _JSONError;
|
|
|
|
|
} else {
|
|
|
|
|
return [super error];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-03 13:19:53 -08:00
|
|
|
#pragma mark - AFHTTPRequestOperation
|
|
|
|
|
|
|
|
|
|
+ (NSSet *)acceptableContentTypes {
|
|
|
|
|
return [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
|
|
|
|
|
return [[[request URL] pathExtension] isEqualToString:@"json"] || [super canProcessRequest:request];
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-24 13:08:58 -05:00
|
|
|
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
|
|
|
|
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
2011-10-05 15:44:51 -05:00
|
|
|
{
|
2012-07-16 19:21:30 +02:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
|
#pragma clang diagnostic ignored "-Warc-retain-cycles"
|
2013-01-27 12:15:07 -05:00
|
|
|
self.completionBlock = ^ {
|
2012-07-16 19:21:30 +02:00
|
|
|
if (self.error) {
|
2012-02-20 20:22:46 -08:00
|
|
|
if (failure) {
|
2012-07-16 19:21:30 +02:00
|
|
|
dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
|
|
|
|
|
failure(self, self.error);
|
2012-02-20 20:22:46 -08:00
|
|
|
});
|
|
|
|
|
}
|
2011-10-24 13:08:58 -05:00
|
|
|
} else {
|
2012-04-23 20:58:14 -07:00
|
|
|
dispatch_async(json_request_operation_processing_queue(), ^{
|
2012-07-16 19:21:30 +02:00
|
|
|
id JSON = self.responseJSON;
|
2013-01-27 12:15:07 -05:00
|
|
|
|
2012-02-06 13:29:00 -08:00
|
|
|
if (self.JSONError) {
|
2012-02-20 20:22:46 -08:00
|
|
|
if (failure) {
|
2012-07-16 19:21:30 +02:00
|
|
|
dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
|
|
|
|
|
failure(self, self.error);
|
2012-02-20 20:22:46 -08:00
|
|
|
});
|
|
|
|
|
}
|
2012-02-06 13:29:00 -08:00
|
|
|
} else {
|
2012-02-20 20:22:46 -08:00
|
|
|
if (success) {
|
2012-07-16 19:21:30 +02:00
|
|
|
dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
|
|
|
|
|
success(self, JSON);
|
2012-02-20 20:22:46 -08:00
|
|
|
});
|
2013-01-27 12:15:07 -05:00
|
|
|
}
|
2012-02-06 13:29:00 -08:00
|
|
|
}
|
2011-10-24 13:08:58 -05:00
|
|
|
});
|
|
|
|
|
}
|
2012-07-16 19:21:30 +02:00
|
|
|
};
|
|
|
|
|
#pragma clang diagnostic pop
|
2011-10-05 15:44:51 -05:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 15:14:15 -05:00
|
|
|
@end
|