From b7564e9f5aa6b14bec0861bb7720b93d0b650353 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Mon, 10 Oct 2011 10:41:29 -0500 Subject: [PATCH] Refactoring implementation of AFHTTPRequest subclasses to fulfill AFHTTPClientOperation protocol --- AFNetworking/AFHTTPRequestOperation.m | 23 +------------- AFNetworking/AFImageRequestOperation.m | 21 ++++++++----- AFNetworking/AFJSONRequestOperation.m | 20 ++++++++----- AFNetworking/AFPropertyListRequestOperation.m | 18 +++++++---- AFNetworking/AFXMLRequestOperation.m | 30 ++++++++++++++++++- 5 files changed, 68 insertions(+), 44 deletions(-) diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index 7ed0025..a842c95 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -94,28 +94,7 @@ success:(void (^)(id object))success failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure { - AFHTTPRequestOperation *operation = [[[self alloc] initWithRequest:urlRequest] autorelease]; - operation.completionBlock = ^ { - if ([operation isCancelled]) { - return; - } - - if (operation.error) { - if (failure) { - dispatch_async(dispatch_get_main_queue(), ^(void) { - failure(operation.response, operation.error); - }); - } - } else { - if (success) { - dispatch_async(dispatch_get_main_queue(), ^(void) { - success(operation.responseData); - }); - } - } - }; - - return operation; + return nil; } @end diff --git a/AFNetworking/AFImageRequestOperation.m b/AFNetworking/AFImageRequestOperation.m index ba68b23..9e7e956 100644 --- a/AFNetworking/AFImageRequestOperation.m +++ b/AFNetworking/AFImageRequestOperation.m @@ -34,6 +34,9 @@ static dispatch_queue_t image_request_operation_processing_queue() { @interface AFImageRequestOperation () @property (readwrite, nonatomic, retain) UIImage *responseImage; + ++ (NSSet *)defaultAcceptableContentTypes; ++ (NSSet *)defaultAcceptablePathExtensions; @end @implementation AFImageRequestOperation @@ -93,13 +96,21 @@ static dispatch_queue_t image_request_operation_processing_queue() { return operation; } ++ (NSSet *)defaultAcceptableContentTypes { + return [NSSet setWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon" @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", nil]; +} + ++ (NSSet *)defaultAcceptablePathExtensions { + return [NSSet setWithObjects:@"tif", @"tiff", @"jpg", @"jpeg", @"gif", @"png", @"ico", @"bmp", @"cur", nil]; +} + - (id)initWithRequest:(NSURLRequest *)urlRequest { self = [super initWithRequest:urlRequest]; if (!self) { return nil; } - self.acceptableContentTypes = [NSSet setWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon" @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", nil]; + self.acceptableContentTypes = [[self class] defaultAcceptableContentTypes]; return self; } @@ -125,13 +136,7 @@ static dispatch_queue_t image_request_operation_processing_queue() { #pragma mark - AFHTTPClientOperation + (BOOL)canProcessRequest:(NSURLRequest *)request { - NSSet *acceptableContentTypes = [NSSet setWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon" @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", nil]; - NSSet *acceptablePathExtensions = [NSSet setWithObjects:@"tif", @"tiff", @"jpg", @"jpeg", @"gif", @"png", @"ico", @"bmp", @"cur", nil]; - return [acceptableContentTypes containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [acceptablePathExtensions containsObject:[[request URL] pathExtension]]; -} - -+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { - return request; + return [[self defaultAcceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]]; } + (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest diff --git a/AFNetworking/AFJSONRequestOperation.m b/AFNetworking/AFJSONRequestOperation.m index c3373e3..61a4cbf 100644 --- a/AFNetworking/AFJSONRequestOperation.m +++ b/AFNetworking/AFJSONRequestOperation.m @@ -37,6 +37,9 @@ static dispatch_queue_t json_request_operation_processing_queue() { @interface AFJSONRequestOperation () @property (readwrite, nonatomic, retain) id responseJSON; @property (readwrite, nonatomic, retain) NSError *error; + ++ (NSSet *)defaultAcceptableContentTypes; ++ (NSSet *)defaultAcceptablePathExtensions; @end @implementation AFJSONRequestOperation @@ -83,13 +86,21 @@ static dispatch_queue_t json_request_operation_processing_queue() { return operation; } ++ (NSSet *)defaultAcceptableContentTypes { + return [NSSet setWithObjects:@"application/json", @"application/x-javascript", @"text/javascript", @"text/x-javascript", @"text/x-json", @"text/json", @"text/plain", nil]; +} + ++ (NSSet *)defaultAcceptablePathExtensions { + return [NSSet setWithObjects:@"json", nil]; +} + - (id)initWithRequest:(NSURLRequest *)urlRequest { self = [super initWithRequest:urlRequest]; if (!self) { return nil; } - self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"application/x-javascript", @"text/javascript", @"text/x-javascript", @"text/x-json", @"text/json", @"text/plain", nil]; + self.acceptableContentTypes = [[self class] defaultAcceptableContentTypes]; return self; } @@ -123,12 +134,7 @@ static dispatch_queue_t json_request_operation_processing_queue() { #pragma mark - AFHTTPClientOperation + (BOOL)canProcessRequest:(NSURLRequest *)request { - NSSet *acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"application/x-javascript", @"text/javascript", @"text/x-javascript", @"text/x-json", @"text/json", @"text/plain", nil]; - return [acceptableContentTypes containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[[request URL] pathExtension] isEqualToString:@"json"]; -} - -+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { - return request; + return [[self defaultAcceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]]; } + (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest diff --git a/AFNetworking/AFPropertyListRequestOperation.m b/AFNetworking/AFPropertyListRequestOperation.m index 6fabbf5..eb2f7c4 100644 --- a/AFNetworking/AFPropertyListRequestOperation.m +++ b/AFNetworking/AFPropertyListRequestOperation.m @@ -35,6 +35,9 @@ static dispatch_queue_t property_list_request_operation_processing_queue() { @property (readwrite, nonatomic, retain) id responsePropertyList; @property (readwrite, nonatomic, assign) NSPropertyListFormat propertyListFormat; @property (readwrite, nonatomic, retain) NSError *error; + ++ (NSSet *)defaultAcceptableContentTypes; ++ (NSSet *)defaultAcceptablePathExtensions; @end @implementation AFPropertyListRequestOperation @@ -81,6 +84,14 @@ static dispatch_queue_t property_list_request_operation_processing_queue() { return operation; } ++ (NSSet *)defaultAcceptableContentTypes { + return [NSSet setWithObjects:@"application/x-plist", @"application/xml", nil]; +} + ++ (NSSet *)defaultAcceptablePathExtensions { + return [NSSet setWithObjects:@"plist", nil]; +} + - (id)initWithRequest:(NSURLRequest *)urlRequest { self = [super initWithRequest:urlRequest]; if (!self) { @@ -116,12 +127,7 @@ static dispatch_queue_t property_list_request_operation_processing_queue() { #pragma mark - AFHTTPClientOperation + (BOOL)canProcessRequest:(NSURLRequest *)request { - NSSet *acceptableContentTypes = [NSSet setWithObjects:@"application/x-plist", @"application/xml", nil]; - return [acceptableContentTypes containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[[request URL] pathExtension] isEqualToString:@"plist"]; -} - -+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { - return request; + return [[self defaultAcceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]]; } + (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest diff --git a/AFNetworking/AFXMLRequestOperation.m b/AFNetworking/AFXMLRequestOperation.m index 00dd1fb..057b2e5 100644 --- a/AFNetworking/AFXMLRequestOperation.m +++ b/AFNetworking/AFXMLRequestOperation.m @@ -26,6 +26,9 @@ @interface AFXMLRequestOperation () @property (readwrite, nonatomic, retain) NSXMLParser *responseXMLParser; + ++ (NSSet *)defaultAcceptableContentTypes; ++ (NSSet *)defaultAcceptablePathExtensions; @end @implementation AFXMLRequestOperation @@ -58,13 +61,21 @@ return operation; } ++ (NSSet *)defaultAcceptableContentTypes { + return [NSSet setWithObjects:@"application/xml", @"text/xml", nil]; +} + ++ (NSSet *)defaultAcceptablePathExtensions { + return [NSSet setWithObjects:@"xml", nil]; +} + - (id)initWithRequest:(NSURLRequest *)urlRequest { self = [super initWithRequest:urlRequest]; if (!self) { return nil; } - self.acceptableContentTypes = [NSSet setWithObjects:@"application/xml", @"text/xml", nil]; + self.acceptableContentTypes = [[self class] defaultAcceptableContentTypes]; return self; } @@ -82,4 +93,21 @@ return _responseXMLParser; } +#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 +{ + 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); + }]; +} + @end