From eda2d1a3f578d12d4033d09038e3c35bf7e2a84a Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Mon, 26 Sep 2011 14:46:46 -0500 Subject: [PATCH] Expanding AFJSONRequestOperation and AFHTTPClient failure blocks to include the NSHTTPURLResponse object --- AFNetworking/AFHTTPClient.h | 30 ++++++++++++------- AFNetworking/AFHTTPClient.m | 42 +++++++++++++++++++++------ AFNetworking/AFJSONRequestOperation.h | 4 +-- AFNetworking/AFJSONRequestOperation.m | 6 ++-- Example/Classes/Models/Spot.m | 6 ++-- 5 files changed, 60 insertions(+), 28 deletions(-) diff --git a/AFNetworking/AFHTTPClient.h b/AFNetworking/AFHTTPClient.h index f3a29b7..482de9e 100644 --- a/AFNetworking/AFHTTPClient.h +++ b/AFNetworking/AFHTTPClient.h @@ -180,12 +180,12 @@ Creates and enqueues an `AFHTTPRequestOperation` to the HTTP client's operation queue. @param request The request object to be loaded asynchronously during execution of the operation. - @param success A block object to be executed when the request operation finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `application/json`). This block has no return value and takes a single argument, which is the response object created from the response data of request. + @param success A block object to be executed when the request operation finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `application/json`). This block has no return value and takes a single argument, which is an object created from the response data of request. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data as JSON. This block has no return value and takes a single argument, which is the `NSError` object describing the network or parsing error that occurred. */ - (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request - success:(void (^)(id response))success - failure:(void (^)(NSError *error))failure; + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure; ///--------------------------------- /// @name Cancelling HTTP Operations @@ -210,11 +210,13 @@ @param parameters The parameters to be encoded and appended as the query string for the request URL. @param success A block object to be executed when the request operation finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `application/json`). This block has no return value and takes a single argument, which is the response object created from the response data of request. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data as JSON. This block has no return value and takes a single argument, which is the `NSError` object describing the network or parsing error that occurred. + + @see enqueueHTTPOperationWithRequest:success:failure */ - (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters - success:(void (^)(id response))success - failure:(void (^)(NSError *error))failure; + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure; /** Creates an `AFHTTPRequestOperation` with a `POST` request, and enqueues it to the HTTP client's operation queue. @@ -223,11 +225,13 @@ @param parameters The parameters to be encoded and set in the request HTTP body. @param success A block object to be executed when the request operation finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `application/json`). This block has no return value and takes a single argument, which is the response object created from the response data of request. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data as JSON. This block has no return value and takes a single argument, which is the `NSError` object describing the network or parsing error that occurred. + + @see enqueueHTTPOperationWithRequest:success:failure */ - (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters - success:(void (^)(id response))success - failure:(void (^)(NSError *error))failure; + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure; /** Creates an `AFHTTPRequestOperation` with a `PUT` request, and enqueues it to the HTTP client's operation queue. @@ -236,11 +240,13 @@ @param parameters The parameters to be encoded and set in the request HTTP body. @param success A block object to be executed when the request operation finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `application/json`). This block has no return value and takes a single argument, which is the response object created from the response data of request. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data as JSON. This block has no return value and takes a single argument, which is the `NSError` object describing the network or parsing error that occurred. + + @see enqueueHTTPOperationWithRequest:success:failure */ - (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters - success:(void (^)(id response))success - failure:(void (^)(NSError *error))failure; + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure; /** Creates an `AFHTTPRequestOperation` with a `DELETE` request, and enqueues it to the HTTP client's operation queue. @@ -249,11 +255,13 @@ @param parameters The parameters to be encoded and set in the request HTTP body. @param success A block object to be executed when the request operation finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `application/json`). This block has no return value and takes a single argument, which is the response object created from the response data of request. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data as JSON. This block has no return value and takes a single argument, which is the `NSError` object describing the network or parsing error that occurred. + + @see enqueueHTTPOperationWithRequest:success:failure */ - (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters - success:(void (^)(id response))success - failure:(void (^)(NSError *error))failure; + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure; @end #pragma mark - diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m index 46bfd7e..0c828c6 100644 --- a/AFNetworking/AFHTTPClient.m +++ b/AFNetworking/AFHTTPClient.m @@ -224,12 +224,20 @@ static NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSS return request; } -- (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request success:(void (^)(id response))success failure:(void (^)(NSError *error))failure { - if ([request URL] == nil || [[request URL] isEqual:[NSNull null]]) { - return; - } +- (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure +{ + AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:urlRequest success:^(id JSON) { + if (success) { + success(JSON); + } + } failure:^(NSHTTPURLResponse *response, NSError *error) { + if (failure) { + failure(response, error); + } + }]; - AFHTTPRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:success failure:failure]; [self.operationQueue addOperation:operation]; } @@ -243,22 +251,38 @@ static NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSS #pragma mark - -- (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success failure:(void (^)(NSError *error))failure { +- (void)getPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure +{ NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters]; [self enqueueHTTPOperationWithRequest:request success:success failure:failure]; } -- (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success failure:(void (^)(NSError *error))failure { +- (void)postPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure +{ NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters]; [self enqueueHTTPOperationWithRequest:request success:success failure:failure]; } -- (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success failure:(void (^)(NSError *error))failure { +- (void)putPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure +{ NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters]; [self enqueueHTTPOperationWithRequest:request success:success failure:failure]; } -- (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success failure:(void (^)(NSError *error))failure { +- (void)deletePath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure +{ NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters]; [self enqueueHTTPOperationWithRequest:request success:success failure:failure]; } diff --git a/AFNetworking/AFJSONRequestOperation.h b/AFNetworking/AFJSONRequestOperation.h index f7b44b7..91e47dc 100644 --- a/AFNetworking/AFJSONRequestOperation.h +++ b/AFNetworking/AFJSONRequestOperation.h @@ -55,7 +55,7 @@ @param urlRequest The request object to be loaded asynchronously during execution of the operation @param success A block object to be executed when the JSON request operation finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `application/json`). This block has no return value and takes a single argument, which is the JSON object created from the response data of request. - @param failure A block object to be executed when the JSON request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data as JSON. This block has no return value and takes a single argument, which is the error describing the network or parsing error that occurred. + @param failure A block object to be executed when the JSON request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data as JSON. This block has no return value and takes a two arguments: the response from the server, and the error describing the network or parsing error that occurred. @see defaultAcceptableStatusCodes @see defaultAcceptableContentTypes @@ -65,7 +65,7 @@ */ + (AFJSONRequestOperation *)operationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(id JSON))success - failure:(void (^)(NSError *error))failure; + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure; /** Creates and returns an `AFJSONRequestOperation` object and sets the specified success and failure callbacks, as well as the status codes and content types that are acceptable for a successful request. diff --git a/AFNetworking/AFJSONRequestOperation.m b/AFNetworking/AFJSONRequestOperation.m index e681cf8..2698a08 100644 --- a/AFNetworking/AFJSONRequestOperation.m +++ b/AFNetworking/AFJSONRequestOperation.m @@ -44,15 +44,15 @@ static dispatch_queue_t json_request_operation_processing_queue() { + (AFJSONRequestOperation *)operationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(id JSON))success - failure:(void (^)(NSError *error))failure + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure { return [self operationWithRequest:urlRequest acceptableStatusCodes:[self defaultAcceptableStatusCodes] acceptableContentTypes:[self defaultAcceptableContentTypes] success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, id JSON) { if (success) { success(JSON); } - } failure:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, NSError *error) { + } failure:^(NSURLRequest __unused *request, NSHTTPURLResponse *response, NSError *error) { if (failure) { - failure(error); + failure(response, error); } }]; } diff --git a/Example/Classes/Models/Spot.m b/Example/Classes/Models/Spot.m index 58e1cd7..5d9675a 100644 --- a/Example/Classes/Models/Spot.m +++ b/Example/Classes/Models/Spot.m @@ -64,9 +64,9 @@ [mutableParameters setValue:[NSString stringWithFormat:@"%1.7f", location.coordinate.longitude] forKey:@"lng"]; } - [[AFGowallaAPIClient sharedClient] getPath:urlString parameters:mutableParameters success:^(id response) { + [[AFGowallaAPIClient sharedClient] getPath:urlString parameters:mutableParameters success:^(id object) { NSMutableArray *mutableRecords = [NSMutableArray array]; - for (NSDictionary *attributes in [response valueForKeyPath:@"spots"]) { + for (NSDictionary *attributes in [object valueForKeyPath:@"spots"]) { Spot *spot = [[[Spot alloc] initWithAttributes:attributes] autorelease]; [mutableRecords addObject:spot]; } @@ -74,7 +74,7 @@ if (block) { block([NSArray arrayWithArray:mutableRecords]); } - } failure:^(NSError *error) { + } failure:^(NSHTTPURLResponse *response, NSError *error) { if (block) { block([NSArray array]); }