Created
July 10, 2013 21:46
-
-
Save onelittlefish/5970616 to your computer and use it in GitHub Desktop.
Category for RestKit's RKObjectManager that allows PUTs and POSTs with query parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // RKObjectManager+PutQueryParams.h | |
| // | |
| // | |
| #import "RKObjectManager.h" | |
| @interface RKObjectManager (PutQueryParams) | |
| /** | |
| By default, RestKit 0.20 (as if this writing 0.20.2) will send parameters | |
| in the HTTP body of the request for PUTs. | |
| This method is a wrapper around putObject:path:parameters:success:failure: | |
| that allows query parameters to be sent. | |
| ( See https://groups.google.com/forum/#!topic/restkit/LzzplJKYowQ ) | |
| @param path If nil, defaults to the path specified by the router for PUTting the object. Can also be a path pattern. | |
| @param queryParameters Dictionary of values to append as query parameters | |
| */ | |
| - (void)putObject:(id)object path:(NSString *)path queryParameters:(NSDictionary *)queryParameters success:(void (^)(RKObjectRequestOperation *, RKMappingResult *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure; | |
| /** | |
| By default, RestKit 0.20 (as if this writing 0.20.2) will send parameters | |
| in the HTTP body of the request for POSTs. | |
| This method is a wrapper around postObject:path:parameters:success:failure: | |
| that allows query parameters to be sent. | |
| ( See https://groups.google.com/forum/#!topic/restkit/LzzplJKYowQ ) | |
| @param path If nil, defaults to the path specified by the router for POSTing the object. Can also be a path pattern. | |
| @param queryParameters Dictionary of values to append as query parameters | |
| */ | |
| - (void)postObject:(id)object path:(NSString *)path queryParameters:(NSDictionary *)queryParameters success:(void (^)(RKObjectRequestOperation *, RKMappingResult *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // RKObjectManager+PutQueryParams.m | |
| // | |
| // | |
| #import "RKObjectManager+PutQueryParams.h" | |
| #import <AFNetworking/AFNetworking.h> | |
| #import <RestKit/RestKit.h> | |
| @implementation RKObjectManager (PutQueryParams) | |
| - (void)putObject:(id)object path:(NSString *)path queryParameters:(NSDictionary *)queryParameters success:(void (^)(RKObjectRequestOperation *, RKMappingResult *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure { | |
| [self putObject:object | |
| path:[self pathForObject:object path:path method:RKRequestMethodPUT queryParameters:queryParameters] | |
| parameters:nil | |
| success:success | |
| failure:failure]; | |
| } | |
| - (void)postObject:(id)object path:(NSString *)path queryParameters:(NSDictionary *)queryParameters success:(void (^)(RKObjectRequestOperation *, RKMappingResult *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure { | |
| [self postObject:object | |
| path:[self pathForObject:object path:path method:RKRequestMethodPOST queryParameters:queryParameters] | |
| parameters:nil | |
| success:success | |
| failure:failure]; | |
| } | |
| // Returns the correponding path for the given object with the query parameters appended | |
| // If path is nil, defaults to the path specified by the router for the object/method combination | |
| - (NSString *)pathForObject:(id)object path:(NSString *)path method:(RKRequestMethod)method queryParameters:(NSDictionary *)queryParameters { | |
| NSString *actualPath; | |
| if (queryParameters) { | |
| // If necessary, get the path specified by the router for the given object and method | |
| actualPath = path ? path : [[[[self.router URLForObject:object method:method] absoluteString] componentsSeparatedByString:self.baseURL.absoluteString] objectAtIndex:1]; | |
| // Append ?queryParameters or &queryParameters to path | |
| actualPath = RKPathFromPatternWithObject(actualPath, object); | |
| actualPath = [actualPath stringByAppendingFormat:[actualPath rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@",AFQueryStringFromParametersWithEncoding(queryParameters, self.HTTPClient.stringEncoding)]; | |
| } | |
| else { | |
| actualPath = path; | |
| } | |
| return actualPath; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Extremely helpful for my project, still trying to get it to work properly.
The line "actualPath = RKPathFromPatternWithObject(actualPath, object);" Is throwing "Implicit conversion of 'int' to 'NSString *' is disallowed with ARC" Any thoughts?