From 251ce062f2159de90f78926c33cecd87e85d0342 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Fri, 16 Sep 2011 10:36:36 -0500 Subject: [PATCH] Stashing XML Request Operation --- AFNetworking/AFJSONRequestOperation.m | 2 +- .../project.pbxproj | 6 + Example/AFXMLRequestOperation.h | 23 ++++ Example/AFXMLRequestOperation.m | 111 ++++++++++++++++++ 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 Example/AFXMLRequestOperation.h create mode 100644 Example/AFXMLRequestOperation.m diff --git a/AFNetworking/AFJSONRequestOperation.m b/AFNetworking/AFJSONRequestOperation.m index 0baabd8..5987a0c 100644 --- a/AFNetworking/AFJSONRequestOperation.m +++ b/AFNetworking/AFJSONRequestOperation.m @@ -28,7 +28,7 @@ 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) { - af_json_request_operation_processing_queue = dispatch_queue_create("com.alamofire.json-request.processing", 0); + af_json_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.json-request.processing", 0); } return af_json_request_operation_processing_queue; diff --git a/Example/AFNetworking Example.xcodeproj/project.pbxproj b/Example/AFNetworking Example.xcodeproj/project.pbxproj index b61798d..26a50bb 100644 --- a/Example/AFNetworking Example.xcodeproj/project.pbxproj +++ b/Example/AFNetworking Example.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ F874B5DE13E0AA6500B28E3E /* AFRestClient.m in Sources */ = {isa = PBXBuildFile; fileRef = F874B5CE13E0AA6500B28E3E /* AFRestClient.m */; }; F874B5DF13E0AA6500B28E3E /* UIImage+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = F874B5CF13E0AA6500B28E3E /* UIImage+AFNetworking.m */; }; F874B5E013E0AA6500B28E3E /* UIImageView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = F874B5D013E0AA6500B28E3E /* UIImageView+AFNetworking.m */; }; + F8870C0F1423A15900BCD863 /* AFXMLRequestOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = F8870C0E1423A15800BCD863 /* AFXMLRequestOperation.m */; }; F8D25D191396A9D300CF3BD6 /* placeholder-stamp.png in Resources */ = {isa = PBXBuildFile; fileRef = F8D25D171396A9D300CF3BD6 /* placeholder-stamp.png */; }; F8D25D1A1396A9D300CF3BD6 /* placeholder-stamp@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = F8D25D181396A9D300CF3BD6 /* placeholder-stamp@2x.png */; }; F8DA09D21396ABED0057D0CC /* AFGowallaAPIClient.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25D1D1396A9DE00CF3BD6 /* AFGowallaAPIClient.m */; }; @@ -58,6 +59,8 @@ F874B5D613E0AA6500B28E3E /* AFRestClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = AFRestClient.h; path = ../AFNetworking/AFRestClient.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; F874B5D713E0AA6500B28E3E /* UIImage+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImage+AFNetworking.h"; path = "../AFNetworking/UIImage+AFNetworking.h"; sourceTree = ""; }; F874B5D813E0AA6500B28E3E /* UIImageView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImageView+AFNetworking.h"; path = "../AFNetworking/UIImageView+AFNetworking.h"; sourceTree = ""; }; + F8870C0D1423A15800BCD863 /* AFXMLRequestOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFXMLRequestOperation.h; sourceTree = ""; }; + F8870C0E1423A15800BCD863 /* AFXMLRequestOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFXMLRequestOperation.m; sourceTree = ""; }; F8D25D101396A9C400CF3BD6 /* JSONKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONKit.h; sourceTree = ""; }; F8D25D111396A9C400CF3BD6 /* JSONKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONKit.m; sourceTree = ""; }; F8D25D131396A9C400CF3BD6 /* TTTLocationFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TTTLocationFormatter.h; sourceTree = ""; }; @@ -247,6 +250,8 @@ F874B5C913E0AA6500B28E3E /* AFHTTPRequestOperation.m */, F874B5D413E0AA6500B28E3E /* AFJSONRequestOperation.h */, F874B5CC13E0AA6500B28E3E /* AFJSONRequestOperation.m */, + F8870C0D1423A15800BCD863 /* AFXMLRequestOperation.h */, + F8870C0E1423A15800BCD863 /* AFXMLRequestOperation.m */, F874B5D613E0AA6500B28E3E /* AFRestClient.h */, F874B5CE13E0AA6500B28E3E /* AFRestClient.m */, F874B5D313E0AA6500B28E3E /* AFImageRequestOperation.h */, @@ -361,6 +366,7 @@ F85CE2D413EC478F00BFAE01 /* NSString+AFNetworking.m in Sources */, F85CE2DC13EC4A4200BFAE01 /* NSMutableURLRequest+AFNetworking.m in Sources */, F85CE55513EC759200BFAE01 /* NSData+AFNetworking.m in Sources */, + F8870C0F1423A15900BCD863 /* AFXMLRequestOperation.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Example/AFXMLRequestOperation.h b/Example/AFXMLRequestOperation.h new file mode 100644 index 0000000..72b5221 --- /dev/null +++ b/Example/AFXMLRequestOperation.h @@ -0,0 +1,23 @@ +// AFXMLRequestOperation.h + +#import "AFHTTPRequestOperation.h" + +@interface AFXMLRequestOperation : AFHTTPRequestOperation + ++ (id)operationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(id XML))success; + ++ (id)operationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(id XML))success + failure:(void (^)(NSError *error))failure; + ++ (id)operationWithRequest:(NSURLRequest *)urlRequest + acceptableStatusCodes:(NSIndexSet *)acceptableStatusCodes + acceptableContentTypes:(NSSet *)acceptableContentTypes + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id XML))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; + ++ (NSIndexSet *)defaultAcceptableStatusCodes; ++ (NSSet *)defaultAcceptableContentTypes; + +@end diff --git a/Example/AFXMLRequestOperation.m b/Example/AFXMLRequestOperation.m new file mode 100644 index 0000000..a1c03ae --- /dev/null +++ b/Example/AFXMLRequestOperation.m @@ -0,0 +1,111 @@ +// AFXMLRequestOperation.m +// +// Copyright (c) 2011 Gowalla (http://gowalla.com/) +// +// 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: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// 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 "AFXMLRequestOperation.h" + +#include + +static dispatch_queue_t af_xml_request_operation_processing_queue; +static dispatch_queue_t xml_request_operation_processing_queue() { + if (af_xml_request_operation_processing_queue == NULL) { + af_xml_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.xml-request.processing", 0); + } + + return af_xml_request_operation_processing_queue; +} + +@implementation AFXMLRequestOperation + ++ (id)operationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(id XML))success +{ + return [self operationWithRequest:urlRequest success:success failure:nil]; +} + ++ (id)operationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(id XML))success + failure:(void (^)(NSError *error))failure +{ + return [self operationWithRequest:urlRequest acceptableStatusCodes:[self defaultAcceptableStatusCodes] acceptableContentTypes:[self defaultAcceptableContentTypes] success:^(NSURLRequest *request, NSHTTPURLResponse *response, id XML) { + if (success) { + success(XML); + } + } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { + if (failure) { + failure(error); + } + }]; +} + ++ (id)operationWithRequest:(NSURLRequest *)urlRequest + acceptableStatusCodes:(NSIndexSet *)acceptableStatusCodes + acceptableContentTypes:(NSSet *)acceptableContentTypes + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure +{ + return [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) { + if (!error) { + if (![acceptableStatusCodes containsIndex:[response statusCode]]) { + NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; + [userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected status code %@, got %d", nil), acceptableStatusCodes, [response statusCode]] forKey:NSLocalizedDescriptionKey]; + [userInfo setValue:[request URL] forKey:NSURLErrorFailingURLErrorKey]; + + error = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo] autorelease]; + } + + if (![acceptableContentTypes containsObject:[response MIMEType]]) { + NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; + [userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected content type %@, got %@", nil), acceptableContentTypes, [response MIMEType]] forKey:NSLocalizedDescriptionKey]; + [userInfo setValue:[request URL] forKey:NSURLErrorFailingURLErrorKey]; + + error = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo] autorelease]; + } + } + + if (error) { + if (failure) { + failure(request, response, error); + } + } else if ([data length] == 0) { + if (success) { + success(request, response, nil); + } + } else { + dispatch_async(xml_request_operation_processing_queue(), ^(void) { + id XML = nil; + NSError *XMLError = nil; + + + }); + } + }]; +} + ++ (NSIndexSet *)defaultAcceptableStatusCodes { + return [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)]; +} + ++ (NSSet *)defaultAcceptableContentTypes { + return [NSSet setWithObjects:@"application/xml", @"text/xml", nil]; +} + +@end