Refactoring success and failure callbacks for HTTP request operations and client, to add more information and make everything more consistent
Removing AFHTTPClient protocol
This commit is contained in:
parent
8007f0aade
commit
30ea7353d4
12 changed files with 217 additions and 215 deletions
|
|
@ -125,20 +125,20 @@ typedef enum {
|
|||
///----------------------------------
|
||||
|
||||
/**
|
||||
Attempts to register a class conforming to the `AFHTTPClientOperation` protocol, adding it to a chain to automatically generate request operations from a URL request.
|
||||
Attempts to register a subclass of `AFHTTPRequestOperation`, adding it to a chain to automatically generate request operations from a URL request.
|
||||
|
||||
@param The class conforming to the `AFHTTPClientOperation` protocol to register
|
||||
@param The subclass of `AFHTTPRequestOperation` to register
|
||||
|
||||
@return `YES` if the registration is successful, `NO` otherwise. The only failure condition is if `operationClass` does not conform to the `AFHTTPCLientOperation` protocol.
|
||||
@return `YES` if the registration is successful, `NO` otherwise. The only failure condition is if `operationClass` does is not a subclass of `AFHTTPRequestOperation`.
|
||||
|
||||
@discussion When `enqueueHTTPRequestOperationWithRequest:success:failure` is invoked, each registered class is consulted in turn to see if it can handle the specific request. The first class to return `YES` when sent a `canProcessRequest:` message is used to generate an operation using `HTTPRequestOperationWithRequest:success:failure:`. There is no guarantee that all registered classes will be consulted. Classes are consulted in the reverse order of their registration. Attempting to register an already-registered class will move it to the top of the chain.
|
||||
@discussion When `enqueueHTTPRequestOperationWithRequest:success:failure` is invoked, each registered class is consulted in turn to see if it can handle the specific request. The first class to return `YES` when sent a `canProcessRequest:` message is used to create an operation using `initWithURLRequest:` and do `setCompletionBlockWithSuccess:failure:`. There is no guarantee that all registered classes will be consulted. Classes are consulted in the reverse order of their registration. Attempting to register an already-registered class will move it to the top of the list.
|
||||
|
||||
@see `AFHTTPClientOperation`
|
||||
*/
|
||||
- (BOOL)registerHTTPOperationClass:(Class)operationClass;
|
||||
|
||||
/**
|
||||
Unregisteres the specified class conforming to the `AFHTTPClientOperation` protocol.
|
||||
Unregisters the specified subclass of `AFHTTPRequestOperation`.
|
||||
|
||||
@param The class conforming to the `AFHTTPClientOperation` protocol to unregister
|
||||
|
||||
|
|
@ -244,8 +244,8 @@ typedef enum {
|
|||
@see `AFHTTPClientOperation`
|
||||
*/
|
||||
- (void)enqueueHTTPRequestOperationWithRequest:(NSURLRequest *)request
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
|
||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
|
||||
/**
|
||||
Enqueues an `AFHTTPRequestOperation` to the HTTP client's operation queue.
|
||||
|
||||
|
|
@ -281,8 +281,8 @@ typedef enum {
|
|||
*/
|
||||
- (void)getPath:(NSString *)path
|
||||
parameters:(NSDictionary *)parameters
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
|
||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
|
||||
|
||||
/**
|
||||
Creates an `AFHTTPRequestOperation` with a `POST` request, and enqueues it to the HTTP client's operation queue.
|
||||
|
|
@ -296,8 +296,8 @@ typedef enum {
|
|||
*/
|
||||
- (void)postPath:(NSString *)path
|
||||
parameters:(NSDictionary *)parameters
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
|
||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
|
||||
|
||||
/**
|
||||
Creates an `AFHTTPRequestOperation` with a `PUT` request, and enqueues it to the HTTP client's operation queue.
|
||||
|
|
@ -311,8 +311,8 @@ typedef enum {
|
|||
*/
|
||||
- (void)putPath:(NSString *)path
|
||||
parameters:(NSDictionary *)parameters
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
|
||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
|
||||
|
||||
/**
|
||||
Creates an `AFHTTPRequestOperation` with a `DELETE` request, and enqueues it to the HTTP client's operation queue.
|
||||
|
|
@ -326,36 +326,8 @@ typedef enum {
|
|||
*/
|
||||
- (void)deletePath:(NSString *)path
|
||||
parameters:(NSDictionary *)parameters
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
/**
|
||||
The `AFHTTPClientOperation` protocol defines the methods used for the automatic content parsing functionality of `AFHTTPClient`.
|
||||
|
||||
@see `AFHTTPClient -registerHTTPOperationClass:`
|
||||
*/
|
||||
@protocol AFHTTPClientOperation
|
||||
|
||||
/**
|
||||
A Boolean value determining whether or not the class can process the specified request. For example, `AFJSONRequestOperation` may check to make sure the content type was `application/json` or the URL path extension was `.json`.
|
||||
|
||||
@param urlRequest The request that is determined to be supported or not supported for this class.
|
||||
*/
|
||||
+ (BOOL)canProcessRequest:(NSURLRequest *)urlRequest;
|
||||
|
||||
/**
|
||||
Constructs and initializes an operation with success and failure callbacks.
|
||||
|
||||
@param urlRequest The request used by the operation connection.
|
||||
@param success A block object to be executed when the operation finishes successfully. The block has no return value and takes a single argument, the response object from the request.
|
||||
@param failure A block object to be executed when the operation finishes unsuccessfully. The block has no return value and takes two arguments: the response received from the server, and the error describing the network or parsing error that occurred.
|
||||
*/
|
||||
+ (id)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
|
||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
|
|
|||
|
|
@ -204,7 +204,7 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
|||
#pragma mark -
|
||||
|
||||
- (BOOL)registerHTTPOperationClass:(Class)operationClass {
|
||||
if (![operationClass conformsToProtocol:@protocol(AFHTTPClientOperation)]) {
|
||||
if (![operationClass isSubclassOfClass:[AFHTTPRequestOperation class]]) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
|
|
@ -321,8 +321,8 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
|||
}
|
||||
|
||||
- (void)enqueueHTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
|
||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||
{
|
||||
AFHTTPRequestOperation *operation = nil;
|
||||
NSString *className = nil;
|
||||
|
|
@ -330,13 +330,15 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
|||
while (!operation && (className = [enumerator nextObject])) {
|
||||
Class class = NSClassFromString(className);
|
||||
if (class && [class canProcessRequest:urlRequest]) {
|
||||
operation = [class HTTPRequestOperationWithRequest:urlRequest success:success failure:failure];
|
||||
operation = [[(AFHTTPRequestOperation *)[class alloc] initWithRequest:urlRequest] autorelease];
|
||||
}
|
||||
}
|
||||
|
||||
if (!operation) {
|
||||
operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:urlRequest success:success failure:failure];
|
||||
operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
|
||||
}
|
||||
|
||||
[operation setCompletionBlockWithSuccess:success failure:failure];
|
||||
|
||||
[self enqueueHTTPRequestOperation:operation];
|
||||
}
|
||||
|
|
@ -357,8 +359,8 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
|||
|
||||
- (void)getPath:(NSString *)path
|
||||
parameters:(NSDictionary *)parameters
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
|
||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||
{
|
||||
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
|
||||
[self enqueueHTTPRequestOperationWithRequest:request success:success failure:failure];
|
||||
|
|
@ -366,8 +368,8 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
|||
|
||||
- (void)postPath:(NSString *)path
|
||||
parameters:(NSDictionary *)parameters
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
|
||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||
{
|
||||
NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
|
||||
[self enqueueHTTPRequestOperationWithRequest:request success:success failure:failure];
|
||||
|
|
@ -375,8 +377,8 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
|||
|
||||
- (void)putPath:(NSString *)path
|
||||
parameters:(NSDictionary *)parameters
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
|
||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||
{
|
||||
NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
|
||||
[self enqueueHTTPRequestOperationWithRequest:request success:success failure:failure];
|
||||
|
|
@ -384,8 +386,8 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
|||
|
||||
- (void)deletePath:(NSString *)path
|
||||
parameters:(NSDictionary *)parameters
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
|
||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||
{
|
||||
NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
|
||||
[self enqueueHTTPRequestOperationWithRequest:request success:success failure:failure];
|
||||
|
|
|
|||
|
|
@ -22,12 +22,11 @@
|
|||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "AFURLConnectionOperation.h"
|
||||
#import "AFHTTPClient.h"
|
||||
|
||||
/**
|
||||
`AFHTTPRequestOperation` is a subclass of `AFURLConnectionOperation` for requests using the HTTP or HTTPS protocols. It encapsulates the concept of acceptable status codes and content types, which determine the success or failure of a request.
|
||||
*/
|
||||
@interface AFHTTPRequestOperation : AFURLConnectionOperation <AFHTTPClientOperation> {
|
||||
@interface AFHTTPRequestOperation : AFURLConnectionOperation {
|
||||
@private
|
||||
NSIndexSet *_acceptableStatusCodes;
|
||||
NSSet *_acceptableContentTypes;
|
||||
|
|
@ -72,4 +71,21 @@
|
|||
*/
|
||||
@property (readonly) BOOL hasAcceptableContentType;
|
||||
|
||||
/**
|
||||
A Boolean value determining whether or not the class can process the specified request. For example, `AFJSONRequestOperation` may check to make sure the content type was `application/json` or the URL path extension was `.json`.
|
||||
|
||||
@param urlRequest The request that is determined to be supported or not supported for this class.
|
||||
*/
|
||||
+ (BOOL)canProcessRequest:(NSURLRequest *)urlRequest;
|
||||
|
||||
///-----------------------------------------------------------
|
||||
/// @name Setting Completion Block Success / Failure Callbacks
|
||||
///-----------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
*/
|
||||
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -86,38 +86,34 @@
|
|||
return !self.acceptableContentTypes || [self.acceptableContentTypes containsObject:[self.response MIMEType]];
|
||||
}
|
||||
|
||||
#pragma mark - AFHTTPClientOperation
|
||||
|
||||
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
|
||||
return NO;
|
||||
}
|
||||
|
||||
+ (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
|
||||
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||
{
|
||||
AFHTTPRequestOperation *operation = [[[self alloc] initWithRequest:urlRequest] autorelease];
|
||||
operation.completionBlock = ^ {
|
||||
if ([operation isCancelled]) {
|
||||
self.completionBlock = ^ {
|
||||
if ([self isCancelled]) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (operation.error) {
|
||||
if (self.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(operation.response, operation.error);
|
||||
failure(self, self.error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (success) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
success(operation.responseData);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
success(self, self.responseData);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return operation;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - AFHTTPClientOperation
|
||||
|
||||
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -62,6 +62,6 @@
|
|||
@return A new JSON request operation
|
||||
*/
|
||||
+ (AFJSONRequestOperation *)JSONRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
|
||||
success:(void (^)(NSURLRequest *request, NSURLResponse *response, id JSON))success
|
||||
failure:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError *error, id JSON))failure;
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -48,41 +48,21 @@ static dispatch_queue_t json_request_operation_processing_queue() {
|
|||
@synthesize JSONError = _JSONError;
|
||||
|
||||
+ (AFJSONRequestOperation *)JSONRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
|
||||
success:(void (^)(NSURLRequest *request, NSURLResponse *response, id JSON))success
|
||||
failure:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError *error, id JSON))failure
|
||||
{
|
||||
AFJSONRequestOperation *operation = [[[self alloc] initWithRequest:urlRequest] autorelease];
|
||||
operation.completionBlock = ^ {
|
||||
if ([operation isCancelled]) {
|
||||
return;
|
||||
AFJSONRequestOperation *requestOperation = [[[self alloc] initWithRequest:urlRequest] autorelease];
|
||||
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
||||
if (success) {
|
||||
success(operation.request, operation.response, responseObject);
|
||||
}
|
||||
|
||||
if (operation.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(operation.request, operation.response, operation.error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
dispatch_async(json_request_operation_processing_queue(), ^(void) {
|
||||
id JSON = operation.responseJSON;
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
if (operation.error) {
|
||||
if (failure) {
|
||||
failure(operation.request, operation.response, operation.error);
|
||||
}
|
||||
} else {
|
||||
if (success) {
|
||||
success(operation.request, operation.response, JSON);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
||||
if (failure) {
|
||||
failure(operation.request, operation.response, error, [(AFJSONRequestOperation *)operation responseJSON]);
|
||||
}
|
||||
};
|
||||
}];
|
||||
|
||||
return operation;
|
||||
return requestOperation;
|
||||
}
|
||||
|
||||
+ (NSSet *)defaultAcceptableContentTypes {
|
||||
|
|
@ -93,6 +73,10 @@ static dispatch_queue_t json_request_operation_processing_queue() {
|
|||
return [NSSet setWithObjects:@"json", nil];
|
||||
}
|
||||
|
||||
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
|
||||
return [[self defaultAcceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]];
|
||||
}
|
||||
|
||||
- (id)initWithRequest:(NSURLRequest *)urlRequest {
|
||||
self = [super initWithRequest:urlRequest];
|
||||
if (!self) {
|
||||
|
|
@ -143,21 +127,38 @@ static dispatch_queue_t json_request_operation_processing_queue() {
|
|||
}
|
||||
}
|
||||
|
||||
#pragma mark - AFHTTPClientOperation
|
||||
|
||||
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
|
||||
return [[self defaultAcceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]];
|
||||
}
|
||||
|
||||
+ (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
|
||||
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||
{
|
||||
return [self JSONRequestOperationWithRequest:urlRequest success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, id JSON) {
|
||||
success(JSON);
|
||||
} failure:^(NSURLRequest __unused *request, NSHTTPURLResponse *response, NSError *error) {
|
||||
failure(response, error);
|
||||
}];
|
||||
self.completionBlock = ^ {
|
||||
if ([self isCancelled]) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (self.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(self, self.error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
dispatch_async(json_request_operation_processing_queue(), ^(void) {
|
||||
id JSON = self.responseJSON;
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
if (self.JSONError) {
|
||||
if (failure) {
|
||||
failure(self, self.JSONError);
|
||||
}
|
||||
} else {
|
||||
if (success) {
|
||||
success(self, JSON);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -69,6 +69,6 @@
|
|||
*/
|
||||
+ (AFPropertyListRequestOperation *)propertyListRequestOperationWithRequest:(NSURLRequest *)request
|
||||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList))failure;
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ static dispatch_queue_t property_list_request_operation_processing_queue() {
|
|||
@interface AFPropertyListRequestOperation ()
|
||||
@property (readwrite, nonatomic, retain) id responsePropertyList;
|
||||
@property (readwrite, nonatomic, assign) NSPropertyListFormat propertyListFormat;
|
||||
@property (readwrite, nonatomic, retain) NSError *error;
|
||||
@property (readwrite, nonatomic, retain) NSError *propertyListError;
|
||||
|
||||
+ (NSSet *)defaultAcceptableContentTypes;
|
||||
+ (NSSet *)defaultAcceptablePathExtensions;
|
||||
|
|
@ -44,44 +44,24 @@ static dispatch_queue_t property_list_request_operation_processing_queue() {
|
|||
@synthesize responsePropertyList = _responsePropertyList;
|
||||
@synthesize propertyListReadOptions = _propertyListReadOptions;
|
||||
@synthesize propertyListFormat = _propertyListFormat;
|
||||
@synthesize error = _propertyListError;
|
||||
@synthesize propertyListError = _propertyListError;
|
||||
|
||||
+ (AFPropertyListRequestOperation *)propertyListRequestOperationWithRequest:(NSURLRequest *)request
|
||||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList))failure
|
||||
{
|
||||
AFPropertyListRequestOperation *operation = [[[self alloc] initWithRequest:request] autorelease];
|
||||
operation.completionBlock = ^ {
|
||||
if ([operation isCancelled]) {
|
||||
return;
|
||||
AFPropertyListRequestOperation *requestOperation = [[[self alloc] initWithRequest:request] autorelease];
|
||||
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
||||
if (success) {
|
||||
success(operation.request, operation.response, responseObject);
|
||||
}
|
||||
|
||||
if (operation.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(operation.request, operation.response, operation.error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
dispatch_async(property_list_request_operation_processing_queue(), ^(void) {
|
||||
id propertyList = operation.responsePropertyList;
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
if (operation.error) {
|
||||
if (failure) {
|
||||
failure(operation.request, operation.response, operation.error);
|
||||
}
|
||||
} else {
|
||||
if (success) {
|
||||
success(operation.request, operation.response, propertyList);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
||||
if (failure) {
|
||||
failure(operation.request, operation.response, error, [(AFPropertyListRequestOperation *)operation responsePropertyList]);
|
||||
}
|
||||
};
|
||||
}];
|
||||
|
||||
return operation;
|
||||
return requestOperation;
|
||||
}
|
||||
|
||||
+ (NSSet *)defaultAcceptableContentTypes {
|
||||
|
|
@ -117,27 +97,56 @@ static dispatch_queue_t property_list_request_operation_processing_queue() {
|
|||
NSError *error = nil;
|
||||
self.responsePropertyList = [NSPropertyListSerialization propertyListWithData:self.responseData options:self.propertyListReadOptions format:&format error:&error];
|
||||
self.propertyListFormat = format;
|
||||
self.error = error;
|
||||
self.propertyListError = error;
|
||||
}
|
||||
|
||||
return _responsePropertyList;
|
||||
}
|
||||
|
||||
#pragma mark - AFHTTPClientOperation
|
||||
- (NSError *)error {
|
||||
if (_propertyListError) {
|
||||
return _propertyListError;
|
||||
} else {
|
||||
return [super error];
|
||||
}
|
||||
}
|
||||
|
||||
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
|
||||
return [[self defaultAcceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]];
|
||||
}
|
||||
|
||||
+ (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
|
||||
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||
{
|
||||
return [self propertyListRequestOperationWithRequest:urlRequest success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, id propertyList) {
|
||||
success(propertyList);
|
||||
} failure:^(NSURLRequest __unused *request, NSHTTPURLResponse *response, NSError *error) {
|
||||
failure(response, error);
|
||||
}];
|
||||
self.completionBlock = ^ {
|
||||
if ([self isCancelled]) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (self.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(self, self.error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
dispatch_async(property_list_request_operation_processing_queue(), ^(void) {
|
||||
id propertyList = self.responsePropertyList;
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
if (self.propertyListError) {
|
||||
if (failure) {
|
||||
failure(self, self.propertyListError);
|
||||
}
|
||||
} else {
|
||||
if (success) {
|
||||
success(self, propertyList);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@
|
|||
*/
|
||||
+ (AFXMLRequestOperation *)XMLParserRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParse))failure;
|
||||
|
||||
|
||||
#if __MAC_OS_X_VERSION_MIN_REQUIRED
|
||||
|
|
@ -86,7 +86,7 @@
|
|||
*/
|
||||
+ (AFXMLRequestOperation *)XMLDocumentRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLDocument *document))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLDocument *document))failure;
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -53,37 +53,26 @@ static dispatch_queue_t xml_request_operation_processing_queue() {
|
|||
|
||||
+ (AFXMLRequestOperation *)XMLParserRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser))failure
|
||||
{
|
||||
AFXMLRequestOperation *operation = [[[self alloc] initWithRequest:urlRequest] autorelease];
|
||||
operation.completionBlock = ^ {
|
||||
if ([operation isCancelled]) {
|
||||
return;
|
||||
AFXMLRequestOperation *requestOperation = [[[self alloc] initWithRequest:urlRequest] autorelease];
|
||||
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
||||
if (success) {
|
||||
success(operation.request, operation.response, responseObject);
|
||||
}
|
||||
|
||||
if (operation.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(operation.request, operation.response, operation.error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
NSXMLParser *XMLParser = operation.responseXMLParser;
|
||||
if (success) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
success(operation.request, operation.response, XMLParser);
|
||||
});
|
||||
}
|
||||
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
||||
if (failure) {
|
||||
failure(operation.request, operation.response, error, [(AFXMLRequestOperation *)operation responseXMLParser]);
|
||||
}
|
||||
};
|
||||
}];
|
||||
|
||||
return operation;
|
||||
return requestOperation;
|
||||
}
|
||||
|
||||
#if __MAC_OS_X_VERSION_MIN_REQUIRED
|
||||
+ (AFXMLRequestOperation *)XMLDocumentRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLDocument *document))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLDocument *document))failure
|
||||
{
|
||||
AFXMLRequestOperation *operation = [[[self alloc] initWithRequest:urlRequest] autorelease];
|
||||
operation.completionBlock = ^ {
|
||||
|
|
@ -94,7 +83,7 @@ static dispatch_queue_t xml_request_operation_processing_queue() {
|
|||
if (operation.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(operation.request, operation.response, operation.error);
|
||||
failure(operation.request, operation.response, operation.error, [(AFXMLRequestOperation *)operation responseXMLDocument]);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
|
|
@ -165,6 +154,14 @@ static dispatch_queue_t xml_request_operation_processing_queue() {
|
|||
}
|
||||
#endif
|
||||
|
||||
- (NSError *)error {
|
||||
if (_XMLError) {
|
||||
return _XMLError;
|
||||
} else {
|
||||
return [super error];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - NSOperation
|
||||
|
||||
- (void)cancel {
|
||||
|
|
@ -173,29 +170,36 @@ static dispatch_queue_t xml_request_operation_processing_queue() {
|
|||
self.responseXMLParser.delegate = nil;
|
||||
}
|
||||
|
||||
#pragma mark - AFHTTPClientOperation
|
||||
|
||||
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
|
||||
return [[self defaultAcceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]];
|
||||
}
|
||||
|
||||
+ (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
|
||||
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||
{
|
||||
self.completionBlock = ^ {
|
||||
if ([self isCancelled]) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (self.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(self, self.error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
#if __MAC_OS_X_VERSION_MIN_REQUIRED
|
||||
return [self XMLDocumentRequestOperationWithRequest:urlRequest success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, NSXMLDocument *XMLDocument) {
|
||||
success(XMLDocument);
|
||||
} failure:^(NSURLRequest __unused *request, NSHTTPURLResponse *response, NSError *error) {
|
||||
failure(response, error);
|
||||
}];
|
||||
if (success) {
|
||||
success(self, self.responseXMLDocument);
|
||||
}
|
||||
#else
|
||||
return [self XMLParserRequestOperationWithRequest:urlRequest success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, NSXMLParser *XMLParser) {
|
||||
success(XMLParser);
|
||||
} failure:^(NSURLRequest __unused *request, NSHTTPURLResponse *response, NSError *error) {
|
||||
failure(response, error);
|
||||
}];
|
||||
if (success) {
|
||||
success(self, self.responseXMLParser);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -56,17 +56,19 @@
|
|||
[mutableParameters setValue:[NSString stringWithFormat:@"%1.7f", location.coordinate.longitude] forKey:@"lng"];
|
||||
}
|
||||
|
||||
[[AFGowallaAPIClient sharedClient] getPath:urlString parameters:mutableParameters success:^(id object) {
|
||||
[[AFGowallaAPIClient sharedClient] getPath:urlString parameters:mutableParameters success:^(__unused AFHTTPRequestOperation *operation, id JSON) {
|
||||
NSMutableArray *mutableRecords = [NSMutableArray array];
|
||||
for (NSDictionary *attributes in [object valueForKeyPath:@"spots"]) {
|
||||
Spot *spot = [[Spot alloc] initWithAttributes:attributes];
|
||||
[mutableRecords addObject:spot];
|
||||
for (NSDictionary *attributes in [JSON valueForKeyPath:@"spots"]) {
|
||||
@autoreleasepool {
|
||||
Spot *spot = [[Spot alloc] initWithAttributes:attributes];
|
||||
[mutableRecords addObject:spot];
|
||||
}
|
||||
}
|
||||
|
||||
if (block) {
|
||||
block([NSArray arrayWithArray:mutableRecords]);
|
||||
}
|
||||
} failure:^(NSHTTPURLResponse *response, NSError *error) {
|
||||
} failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) {
|
||||
if (block) {
|
||||
block([NSArray array]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,9 +64,9 @@
|
|||
[mutableParameters setValue:[NSString stringWithFormat:@"%1.7f", location.coordinate.longitude] forKey:@"lng"];
|
||||
}
|
||||
|
||||
[[AFGowallaAPIClient sharedClient] getPath:urlString parameters:mutableParameters success:^(id object) {
|
||||
[[AFGowallaAPIClient sharedClient] getPath:urlString parameters:mutableParameters success:^(__unused AFHTTPRequestOperation *operation, id JSON) {
|
||||
NSMutableArray *mutableRecords = [NSMutableArray array];
|
||||
for (NSDictionary *attributes in [object valueForKeyPath:@"spots"]) {
|
||||
for (NSDictionary *attributes in [JSON valueForKeyPath:@"spots"]) {
|
||||
Spot *spot = [[[Spot alloc] initWithAttributes:attributes] autorelease];
|
||||
[mutableRecords addObject:spot];
|
||||
}
|
||||
|
|
@ -74,7 +74,7 @@
|
|||
if (block) {
|
||||
block([NSArray arrayWithArray:mutableRecords]);
|
||||
}
|
||||
} failure:^(NSHTTPURLResponse *response, NSError *error) {
|
||||
} failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) {
|
||||
if (block) {
|
||||
block([NSArray array]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue