Expanding AFJSONRequestOperation and AFHTTPClient failure blocks to include the NSHTTPURLResponse object

This commit is contained in:
Mattt Thompson 2011-09-26 14:46:46 -05:00
parent 424f6de299
commit eda2d1a3f5
5 changed files with 60 additions and 28 deletions

View file

@ -180,12 +180,12 @@
Creates and enqueues an `AFHTTPRequestOperation` to the HTTP client's operation queue. 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 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. @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 - (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request
success:(void (^)(id response))success success:(void (^)(id object))success
failure:(void (^)(NSError *error))failure; failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
///--------------------------------- ///---------------------------------
/// @name Cancelling HTTP Operations /// @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 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 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. @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 - (void)getPath:(NSString *)path
parameters:(NSDictionary *)parameters parameters:(NSDictionary *)parameters
success:(void (^)(id response))success success:(void (^)(id object))success
failure:(void (^)(NSError *error))failure; failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
/** /**
Creates an `AFHTTPRequestOperation` with a `POST` request, and enqueues it to the HTTP client's operation queue. 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 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 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. @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 - (void)postPath:(NSString *)path
parameters:(NSDictionary *)parameters parameters:(NSDictionary *)parameters
success:(void (^)(id response))success success:(void (^)(id object))success
failure:(void (^)(NSError *error))failure; failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
/** /**
Creates an `AFHTTPRequestOperation` with a `PUT` request, and enqueues it to the HTTP client's operation queue. 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 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 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. @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 - (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters parameters:(NSDictionary *)parameters
success:(void (^)(id response))success success:(void (^)(id object))success
failure:(void (^)(NSError *error))failure; failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
/** /**
Creates an `AFHTTPRequestOperation` with a `DELETE` request, and enqueues it to the HTTP client's operation queue. 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 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 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. @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 - (void)deletePath:(NSString *)path
parameters:(NSDictionary *)parameters parameters:(NSDictionary *)parameters
success:(void (^)(id response))success success:(void (^)(id object))success
failure:(void (^)(NSError *error))failure; failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
@end @end
#pragma mark - #pragma mark -

View file

@ -224,12 +224,20 @@ static NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSS
return request; return request;
} }
- (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request success:(void (^)(id response))success failure:(void (^)(NSError *error))failure { - (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)urlRequest
if ([request URL] == nil || [[request URL] isEqual:[NSNull null]]) { success:(void (^)(id object))success
return; 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]; [self.operationQueue addOperation:operation];
} }
@ -243,22 +251,38 @@ static NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSS
#pragma mark - #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]; NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
[self enqueueHTTPOperationWithRequest:request success:success failure:failure]; [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]; NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
[self enqueueHTTPOperationWithRequest:request success:success failure:failure]; [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]; NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
[self enqueueHTTPOperationWithRequest:request success:success failure:failure]; [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]; NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
[self enqueueHTTPOperationWithRequest:request success:success failure:failure]; [self enqueueHTTPOperationWithRequest:request success:success failure:failure];
} }

View file

@ -55,7 +55,7 @@
@param urlRequest The request object to be loaded asynchronously during execution of the operation @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 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 defaultAcceptableStatusCodes
@see defaultAcceptableContentTypes @see defaultAcceptableContentTypes
@ -65,7 +65,7 @@
*/ */
+ (AFJSONRequestOperation *)operationWithRequest:(NSURLRequest *)urlRequest + (AFJSONRequestOperation *)operationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(id JSON))success 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. 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.

View file

@ -44,15 +44,15 @@ static dispatch_queue_t json_request_operation_processing_queue() {
+ (AFJSONRequestOperation *)operationWithRequest:(NSURLRequest *)urlRequest + (AFJSONRequestOperation *)operationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(id JSON))success 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) { return [self operationWithRequest:urlRequest acceptableStatusCodes:[self defaultAcceptableStatusCodes] acceptableContentTypes:[self defaultAcceptableContentTypes] success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, id JSON) {
if (success) { if (success) {
success(JSON); success(JSON);
} }
} failure:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, NSError *error) { } failure:^(NSURLRequest __unused *request, NSHTTPURLResponse *response, NSError *error) {
if (failure) { if (failure) {
failure(error); failure(response, error);
} }
}]; }];
} }

View file

@ -64,9 +64,9 @@
[mutableParameters setValue:[NSString stringWithFormat:@"%1.7f", location.coordinate.longitude] forKey:@"lng"]; [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]; 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]; Spot *spot = [[[Spot alloc] initWithAttributes:attributes] autorelease];
[mutableRecords addObject:spot]; [mutableRecords addObject:spot];
} }
@ -74,7 +74,7 @@
if (block) { if (block) {
block([NSArray arrayWithArray:mutableRecords]); block([NSArray arrayWithArray:mutableRecords]);
} }
} failure:^(NSError *error) { } failure:^(NSHTTPURLResponse *response, NSError *error) {
if (block) { if (block) {
block([NSArray array]); block([NSArray array]);
} }