Skip to content

Commit d9bdaf3

Browse files
committed
Add HTTP Verb to RKRequestDescriptor
1 parent 3744939 commit d9bdaf3

File tree

6 files changed

+45
-25
lines changed

6 files changed

+45
-25
lines changed

Code/Network/RKObjectManager.m

+4-6
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,14 @@
6969
@param object The object to find a matching request descriptor for.
7070
@return An `RKRequestDescriptor` object matching the given object, or `nil` if none could be found.
7171
*/
72-
static RKRequestDescriptor *RKRequestDescriptorFromArrayMatchingObject(NSArray *requestDescriptors, id object)
72+
static RKRequestDescriptor *RKRequestDescriptorFromArrayMatchingObjectAndRequestMethod(NSArray *requestDescriptors, id object, RKRequestMethod requestMethod)
7373
{
7474
Class searchClass = [object class];
7575
do {
7676
for (RKRequestDescriptor *requestDescriptor in requestDescriptors) {
77-
if ([requestDescriptor.objectClass isEqual:searchClass]) return requestDescriptor;
77+
if ([requestDescriptor matchesObject:object requestMethod:requestMethod exactMatch:YES]) return requestDescriptor;
7878
}
79-
searchClass = [searchClass superclass];
80-
} while (searchClass);
81-
79+
} while ((searchClass = [searchClass superclass]));
8280
return nil;
8381
}
8482

@@ -429,7 +427,7 @@ - (id)mergedParametersWithObject:(id)object method:(RKRequestMethod)method param
429427
NSArray *objectsToParameterize = ([object isKindOfClass:[NSArray class]] || object == nil) ? object : @[ object ];
430428
RKObjectParameters *objectParameters = [RKObjectParameters new];
431429
for (id objectToParameterize in objectsToParameterize) {
432-
RKRequestDescriptor *requestDescriptor = RKRequestDescriptorFromArrayMatchingObject(self.requestDescriptors, objectToParameterize);
430+
RKRequestDescriptor *requestDescriptor = RKRequestDescriptorFromArrayMatchingObjectAndRequestMethod(self.requestDescriptors, objectToParameterize, method);
433431
if ((method != RKRequestMethodGET && method != RKRequestMethodDELETE) && requestDescriptor) {
434432
NSError *error = nil;
435433
NSDictionary *parametersForObject = [RKObjectParameterization parametersWithObject:objectToParameterize requestDescriptor:requestDescriptor error:&error];

Code/Network/RKRequestDescriptor.h

+13-5
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
//
2020

2121
#import <Foundation/Foundation.h>
22+
#import "RKHTTPUtilities.h"
2223

2324
@class RKMapping;
2425

2526
/**
2627
An `RKRequestDescriptor` object describes an object mapping configuration that is used to construct the parameters of an HTTP request for an object. Request descriptors are defined by specifying the `RKMapping` object (whose `objectClass` must be `NSMutableDictionary`) that is to be used when object mapping an object into an `NSDictionary` of parameters, the class of the type of object for which the mapping is to be applied, and an optional root key path under which the paramters are to be nested. Response descriptors are only utilized when construct parameters for an `NSURLRequest` with an HTTP method of `POST`, `PUT`, or `PATCH`.
27-
28+
2829
@see RKObjectParameterization
2930
@see [RKObjectMapping requestMapping]
3031
@see [RKObjectManager requestWithObject:method:path:parameters:]
@@ -37,19 +38,24 @@
3738

3839
/**
3940
Creates and returns a new `RKRequestDescriptor` object.
40-
41+
4142
@param mapping The mapping to be used when parameterizing an object using the request descriptor. Cannot be nil and must have an objectClass equal to `[NSMutableDictionary class]`.
4243
@param objectClass The class of objects for which the request descriptor should be used. Cannot be nil.
4344
@param rootKeyPath The root key path under which paramters constructed using the response descriptor will be nested. If nil, the parameters will not be nested and returned as a flat dictionary object.
4445
@return A new `RKRequestDescriptor` object.
45-
46+
4647
@see [RKObjectMapping requestMapping]
4748
@warning An exception will be raised if the objectClass of the given mapping is not `[NSMutableDictionary class]`.
4849
*/
4950
+ (instancetype)requestDescriptorWithMapping:(RKMapping *)mapping
5051
objectClass:(Class)objectClass
5152
rootKeyPath:(NSString *)rootKeyPath;
5253

54+
+ (instancetype)requestDescriptorWithMapping:(RKMapping *)mapping
55+
objectClass:(Class)objectClass
56+
rootKeyPath:(NSString *)rootKeyPath
57+
requestMethod:(RKRequestMethod)requestMethod;
58+
5359
///-----------------------------------------------------
5460
/// @name Getting Information About a Request Descriptor
5561
///-----------------------------------------------------
@@ -69,16 +75,18 @@
6975
*/
7076
@property (nonatomic, copy, readonly) NSString *rootKeyPath;
7177

78+
@property (nonatomic, assign, readonly) RKRequestMethod requestMethod;
79+
7280
///--------------------------------
7381
/// @name Using Request Descriptors
7482
///--------------------------------
7583

7684
/**
7785
Returns `YES` if the given object is instance of objectClass or any class that inherits from objectClass, else `NO`.
78-
86+
7987
@param object The object to be matched against the receiver.
8088
@return `YES` if the given object matches objectClass, else `NO`.
8189
*/
82-
- (BOOL)matchesObject:(id)object;
90+
- (BOOL)matchesObject:(id)object requestMethod:(RKRequestMethod)requestMethod exactMatch:(BOOL)exact;
8391

8492
@end

Code/Network/RKRequestDescriptor.m

+9-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@ @interface RKRequestDescriptor ()
4747
@property (nonatomic, strong, readwrite) RKMapping *mapping;
4848
@property (nonatomic, strong, readwrite) Class objectClass;
4949
@property (nonatomic, copy, readwrite) NSString *rootKeyPath;
50+
@property (nonatomic, assign, readwrite) RKRequestMethod requestMethod;
5051

5152
@end
5253

5354
@implementation RKRequestDescriptor
5455

5556
+ (instancetype)requestDescriptorWithMapping:(RKMapping *)mapping objectClass:(Class)objectClass rootKeyPath:(NSString *)rootKeyPath
57+
{
58+
return [self requestDescriptorWithMapping:mapping objectClass:objectClass rootKeyPath:rootKeyPath requestMethod:RKRequestMethodAny];
59+
}
60+
61+
+ (instancetype)requestDescriptorWithMapping:(RKMapping *)mapping objectClass:(Class)objectClass rootKeyPath:(NSString *)rootKeyPath requestMethod:(RKRequestMethod)requestMethod
5662
{
5763
NSParameterAssert(mapping);
5864
NSParameterAssert(objectClass);
@@ -62,6 +68,7 @@ + (instancetype)requestDescriptorWithMapping:(RKMapping *)mapping objectClass:(C
6268
requestDescriptor.mapping = mapping;
6369
requestDescriptor.objectClass = objectClass;
6470
requestDescriptor.rootKeyPath = rootKeyPath;
71+
requestDescriptor.requestMethod = requestMethod;
6572
return requestDescriptor;
6673
}
6774

@@ -71,9 +78,9 @@ - (NSString *)description
7178
NSStringFromClass([self class]), self, NSStringFromClass(self.objectClass), self.rootKeyPath, self.mapping];
7279
}
7380

74-
- (BOOL)matchesObject:(id)object
81+
- (BOOL)matchesObject:(id)object requestMethod:(RKRequestMethod)requestMethod exactMatch:(BOOL)exact
7582
{
76-
return [object isKindOfClass:self.objectClass];
83+
return (exact ? [object class] == self.objectClass : [object isKindOfClass:self.objectClass] ) && (requestMethod & self.requestMethod) != 0;
7784
}
7885

7986
@end

Code/Network/RKRouteSet.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#import "RKRoute.h"
2222

2323
// Wildcard matches on objects
24-
extern RKRequestMethod const RKRequestMethodAny;
24+
//extern RKRequestMethod const RKRequestMethodAny;
2525

2626
/**
2727
The `RKRouteSet` class provides for the storage and retrieval of `RKRoute` objects. Route objects are added and removed the route set to manipulate the routing table of the application.

Code/Network/RKRouteSet.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#import "RKRouteSet.h"
2222
#import "RKPathMatcher.h"
2323

24-
RKRequestMethod const RKRequestMethodAny = RKRequestMethodInvalid;
24+
//RKRequestMethod const RKRequestMethodAny = RKRequestMethodInvalid;
2525

2626
@interface RKRouteSet ()
2727

Code/ObjectMapping/RKHTTPUtilities.h

+17-10
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@
2323
/**
2424
HTTP methods for requests
2525
*/
26-
typedef enum RKRequestMethod {
27-
RKRequestMethodInvalid = -1,
28-
RKRequestMethodGET,
29-
RKRequestMethodPOST,
30-
RKRequestMethodPUT,
31-
RKRequestMethodDELETE,
32-
RKRequestMethodHEAD,
33-
RKRequestMethodPATCH,
34-
RKRequestMethodOPTIONS
35-
} RKRequestMethod; // RKHTTPMethod? RKStringFromHTTPMethod... RKHTTPMethodFromString
26+
typedef NS_OPTIONS(NSInteger, RKRequestMethod) {
27+
RKRequestMethodInvalid = -1,
28+
RKRequestMethodGET = 1 << 0,
29+
RKRequestMethodPOST = 1 << 1,
30+
RKRequestMethodPUT = 1 << 2,
31+
RKRequestMethodDELETE = 1 << 3,
32+
RKRequestMethodHEAD = 1 << 4,
33+
RKRequestMethodPATCH = 1 << 5,
34+
RKRequestMethodOPTIONS = 1 << 6,
35+
RKRequestMethodAny = (RKRequestMethodGET |
36+
RKRequestMethodPOST |
37+
RKRequestMethodPUT |
38+
RKRequestMethodDELETE |
39+
RKRequestMethodHEAD |
40+
RKRequestMethodPATCH |
41+
RKRequestMethodOPTIONS)
42+
}; // RKHTTPMethod? RKStringFromHTTPMethod... RKHTTPMethodFromString
3643

3744
/**
3845
Returns the corresponding string for value for a given HTTP request method.

0 commit comments

Comments
 (0)