Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding two request descriptors for a given class in Restkit 0.2

Tags:

ios

restkit

I need to make two different types of POST coming from the User class.

//JSON Type A
{
    "password":"12345",
    "email":"[email protected]"
}

//JSON Type B
{
   "user":{
      "Password":"12345",
      "Email":"[email protected]"
   }
}

I've tried to make two request descriptors and adding them to my object manager however I get the error

"Cannot add a request descriptor for the same object class as an existing request descriptor."

My code

@interface User : NSObject

@property (nonatomic, retain) NSString * userID;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSString * firstName;
@property (nonatomic, retain) NSString * lastName;

@end

- (void)setupUserMapping:(RKObjectManager *)objectManager {

    // Setup user response mappings
    RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
    [userMapping addAttributeMappingsFromDictionary:@{
     @"ID" :@"userID",
     @"Email" : @"email",
     @"Password" : @"password",
     @"FirstName" : @"firstName",
     @"LastName" : @"lastName",
     }];


    RKResponseDescriptor *responseDescriptorAuthenticate = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                       pathPattern:@"/Authenticate"
                                                                                           keyPath:nil
                                                                                       statusCodes:[NSIndexSet indexSetWithIndex:200]];


    RKResponseDescriptor *responseDescriptorRegister = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                                   pathPattern:@"/Register"
                                                                                                       keyPath:nil
                                                                                                   statusCodes:[NSIndexSet indexSetWithIndex:200]];
    [objectManager addResponseDescriptor:responseDescriptorRegister];
    [objectManager addResponseDescriptor:responseDescriptorAuthenticate];

    // Setup user request mappings
    RKObjectMapping* userRequestMappingForRegister = [RKObjectMapping requestMapping];
    [userRequestMappingForRegister addAttributeMappingsFromDictionary:@{
     @"email" : @"Email",
     @"password" : @"Password",
     @"firstName" : @"FirstName",
     @"lastName" : @"LastName",
     }];
    RKRequestDescriptor *requestDescriptorForRegister = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMappingForRegister objectClass:[User class] rootKeyPath:@"user"];


    RKObjectMapping* userRequestMappingForAuthenticate = [RKObjectMapping requestMapping];
    [userRequestMappingForAuthenticate addAttributeMappingsFromDictionary:@{
     @"userID" :@"ID",
     @"email" : @"email",
     @"password": @"password"
     }];
    RKRequestDescriptor *requestDescriptorForAuthenticate = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMappingForAuthenticate objectClass:[User class] rootKeyPath:nil];

    [objectManager addRequestDescriptor:requestDescriptorForRegister];
    [objectManager addRequestDescriptor:requestDescriptorForAuthenticate];
}

Does anyone know how I can solve this problem without creating a separate class for these requests?

Any help is appreciated.

Thanks.

like image 807
Frank Fu Avatar asked Jan 06 '13 06:01

Frank Fu


People also ask

What is restkit in Objective C?

RestKit RestKit is a modern Objective-C framework for implementing RESTful web services clients on iOS and Mac OS X. It provides a powerful object mapping engine that seamlessly integrates with Core Data and a simple set of networking primitives for mapping HTTP requests and responses built on top of AFNetworking.

How to get a reference for requestspecification?

RequestSpecification is an interface that allows you to specify how the request will look like. This interface has readymade methods to define base URL, base path, headers, etc. We need to use given () method of RestAssured class to get a reference for RequestSpecification.

What is object mapping in restkit?

Object mapping is a deep topic and is explored in exhaustive detail in the Object Mapping Guide on the wiki. RestKit is broken into several modules that cleanly separate the mapping engine from the HTTP and Core Data integrations to provide maximum flexibility.

How do I use requestspecification in rest assured?

T o club common request specifications together and put as a common entity, we can use RequestSpecification in Rest Assured. RequestSpecification is an interface that allows you to specify how the request will look like. This interface has readymade methods to define base URL, base path, headers, etc.


1 Answers

You can use a dynamic mapping to switch the serialization behaviors. If this is a common enough issue, we could conceivably add path matching to the request descriptor. I just have not had a ton of requests for such a feature.

There is an example of how to use the dynamic mapping with a request in the unit tests: https://github.com/RestKit/RestKit/blob/master/Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m#L495-L534

like image 124
Blake Watters Avatar answered Nov 15 '22 14:11

Blake Watters