Skip to content

Instantly share code, notes, and snippets.

@valeriomazzeo
Created May 11, 2015 14:55
Show Gist options
  • Save valeriomazzeo/51f31166657520dad5e5 to your computer and use it in GitHub Desktop.
Save valeriomazzeo/51f31166657520dad5e5 to your computer and use it in GitHub Desktop.
RKObjectManager Extension
#pragma mark - Standard Methods Extensions
- (NSMutableURLRequest *)multipartFormRequestAtPathForRelationship:(NSString *)relationshipName
ofObject:(id)object
method:(RKRequestMethod)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
{
RKRoute *route = [self.router.routeSet routeForRelationship:relationshipName ofClass:[object class] method:method];
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:nil];
NSAssert(URL, @"Failed to generate URL for relationship named '%@' for object: %@", relationshipName, object);
NSString *requestPath = (path) ? path : [URL relativeString];
id requestParameters = [self mergedParametersWithObject:object method:method parameters:parameters];
NSMutableURLRequest *multipartRequest = [self.HTTPClient multipartFormRequestWithMethod:RKStringFromRequestMethod(method)
path:requestPath
parameters:requestParameters
constructingBodyWithBlock:block];
return multipartRequest;
}
- (void)postObjectAtPathForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
[self enqueueObjectRequestOperation:[self postObjectAtPathOperationForRelationship:relationshipName ofObject:object parameters:parameters success:success failure:failure]];
}
- (void)postObjectAtPathForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
[self enqueueObjectRequestOperation:[self postObjectAtPathOperationForRouteNamed:routeName object:object parameters:parameters success:success failure:failure]];
}
- (void)putObjectAtPathForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
[self enqueueObjectRequestOperation:[self putObjectAtPathOperationForRelationship:relationshipName ofObject:object parameters:parameters success:success failure:failure]];
}
- (void)putObjectAtPathForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
[self enqueueObjectRequestOperation:[self putObjectAtPathOperationForRouteNamed:routeName object:object parameters:parameters success:success failure:failure]];
}
- (void)patchObjectAtPathForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
[self enqueueObjectRequestOperation:[self patchObjectAtPathOperationForRelationship:relationshipName ofObject:object parameters:parameters success:success failure:failure]];
}
- (void)patchObjectAtPathForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
[self enqueueObjectRequestOperation:[self patchObjectAtPathOperationForRouteNamed:routeName object:object parameters:parameters success:success failure:failure]];
}
- (void)deleteObjectAtPathForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
[self enqueueObjectRequestOperation:[self deleteObjectAtPathOperationForRelationship:relationshipName ofObject:object parameters:parameters success:success failure:failure]];
}
- (void)deleteObjectAtPathForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
[self enqueueObjectRequestOperation:[self deleteObjectAtPathOperationForRouteNamed:routeName object:object parameters:parameters success:success failure:failure]];
}
#pragma mark - RKObjectRequestOperation Methods
- (id)getObjectsAtPathOperationForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
RKRoute *route = [self.router.routeSet routeForRelationship:relationshipName ofClass:[object class] method:RKRequestMethodGET];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"Failed to generate URL for relationship named '%@' for object: %@", relationshipName, object);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodGET path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)getObjectsAtPathOperationForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(routeName);
RKRoute *route = [self.router.routeSet routeForName:routeName];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"No route found named '%@'", routeName);
NSAssert(route.method & RKRequestMethodGET, @"Expected route named '%@' to specify a GET, but it does not", routeName);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodGET path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)postObjectAtPathOperationForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
RKRoute *route = [self.router.routeSet routeForRelationship:relationshipName ofClass:[object class] method:RKRequestMethodPOST];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"Failed to generate URL for relationship named '%@' for object: %@", relationshipName, object);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodPOST path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)postObjectAtPathOperationForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(routeName);
RKRoute *route = [self.router.routeSet routeForName:routeName];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"No route found named '%@'", routeName);
NSAssert(route.method & RKRequestMethodPOST, @"Expected route named '%@' to specify a POST, but it does not", routeName);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodPOST path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)putObjectAtPathOperationForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
RKRoute *route = [self.router.routeSet routeForRelationship:relationshipName ofClass:[object class] method:RKRequestMethodPUT];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"Failed to generate URL for relationship named '%@' for object: %@", relationshipName, object);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodPUT path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)putObjectAtPathOperationForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(routeName);
RKRoute *route = [self.router.routeSet routeForName:routeName];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"No route found named '%@'", routeName);
NSAssert(route.method & RKRequestMethodPUT, @"Expected route named '%@' to specify a PUT, but it does not", routeName);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodPUT path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)patchObjectAtPathOperationForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
RKRoute *route = [self.router.routeSet routeForRelationship:relationshipName ofClass:[object class] method:RKRequestMethodPATCH];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"Failed to generate URL for relationship named '%@' for object: %@", relationshipName, object);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodPATCH path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
operation.targetObject = nil;
return operation;
}
- (id)patchObjectAtPathOperationForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(routeName);
RKRoute *route = [self.router.routeSet routeForName:routeName];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"No route found named '%@'", routeName);
NSAssert(route.method & RKRequestMethodPATCH, @"Expected route named '%@' to specify a PATCH, but it does not", routeName);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodPATCH path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)deleteObjectAtPathOperationForRelationship:(NSString *)relationshipName
ofObject:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
RKRoute *route = [self.router.routeSet routeForRelationship:relationshipName ofClass:[object class] method:RKRequestMethodDELETE];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"Failed to generate URL for relationship named '%@' for object: %@", relationshipName, object);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodDELETE path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
operation.targetObject = nil;
return operation;
}
- (id)deleteObjectAtPathOperationForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(routeName);
RKRoute *route = [self.router.routeSet routeForName:routeName];
NSDictionary *interpolatedParameters = nil;
NSURL *URL = [self URLWithRoute:route object:object interpolatedParameters:&interpolatedParameters];
NSAssert(URL, @"No route found named '%@'", routeName);
NSAssert(route.method & RKRequestMethodDELETE, @"Expected route named '%@' to specify a DELETE, but it does not", routeName);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodDELETE path:[URL relativeString] parameters:parameters];
operation.mappingMetadata = @{ @"routing": @{ @"parameters": interpolatedParameters, @"route": route } };
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)getObjectsOperationAtPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(path);
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodGET path:path parameters:parameters];
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)getOperationWithObject:(id)object
path:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSAssert(object || path, @"Cannot make a request without an object or a path.");
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodGET path:path parameters:parameters];
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)postOperationWithObject:(id)object
path:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSAssert(object || path, @"Cannot make a request without an object or a path.");
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodPOST path:path parameters:parameters];
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)putOperationWithObject:(id)object
path:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSAssert(object || path, @"Cannot make a request without an object or a path.");
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodPUT path:path parameters:parameters];
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)patchOperationWithObject:(id)object
path:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSAssert(object || path, @"Cannot make a request without an object or a path.");
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodPATCH path:path parameters:parameters];
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
- (id)deleteOperationWithObject:(id)object
path:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
NSAssert(object || path, @"Cannot make a request without an object or a path.");
RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:object method:RKRequestMethodDELETE path:path parameters:parameters];
[operation setCompletionBlockWithSuccess:success failure:failure];
return operation;
}
#pragma mark - Convenience Methods
- (void)addFetchRequestBlocksFromArray:(NSArray *)fetchRequestBlocks
{
for (RKFetchRequestBlock block in fetchRequestBlocks) {
[self addFetchRequestBlock:block];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment