Update RestKit method to 0.2x

33 Views Asked by At

I had to upgrade the RestKit framework in a customer's app to the 0.2x version. In the old version the following code was used to login the user:

MySingleton *singleton = [MySingleton sharedInstance];
NSString *token = singleton.oauth_token;
NSString *secret = singleton.oauth_token_secret;

EnvironmentSingleton *environmentSingleton = [EnvironmentSingleton sharedInstance];
NSString *resourcePath = [NSString stringWithFormat:@"%@/api/user", environmentSingleton.base_url];

RKParams* params = [RKParams params];
[params setValue:token forParam:@"token"];
[params setValue:secret forParam:@"secret"];
[params setValue:txtEmail.text forParam:@"email"];

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[UserInfo class]];
[objectMapping mapKeyPath:@"_appId" toAttribute:@"appId"];
[objectMapping mapKeyPath:@"_fullName" toAttribute:@"fullName"];
[objectMapping mapKeyPath:@"_domainUrn" toAttribute:@"domainUrn"];

RKObjectLoader *objectLoader = [[RKObjectManager sharedManager] loaderWithResourcePath:resourcePath];
objectLoader.method = RKRequestMethodPOST;
objectLoader.objectMapping = objectMapping;
objectLoader.params = params;
objectLoader.delegate = self;

@try
{
    [objectLoader send];
}
@catch (NSException *exception)
{
    NSLog(@"NSException - Name: %@, Reason: %@", exception.name, exception.reason);
}

Now I'm trying to translate this to the following code, but it doesn't work:

MySingleton *singleton = [MySingleton sharedInstance];
NSString *token = singleton.oauth_token;
NSString *secret = singleton.oauth_token_secret;

EnvironmentSingleton *environmentSingleton = [EnvironmentSingleton sharedInstance];
NSString *envURL = [NSString stringWithFormat:@"%@%@", environmentSingleton.environment, environmentSingleton.base_url];
NSString *resourcePath = [NSString stringWithFormat:@"%@/api/user", envURL];

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:token forKey:@"token"];
[params setObject:secret forKey:@"secret"];
[params setObject:txtEmail.text forKey:@"email"];

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[UserInfo class]];
[objectMapping addAttributeMappingsFromDictionary:@{@"_appId": @"appId",
                                                    @"_fullName": @"fullName",
                                                    @"_domainUrn": @"domainUrn"}];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping method:RKRequestMethodPOST pathPattern:nil keyPath:nil statusCodes:statusCodes];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:resourcePath]];

RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    NSArray *objects = [[NSArray alloc] initWithArray:[result array]];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error(%li): %@", (long)error.code,error.localizedDescription);
}];
[operation start];

I've just noticed that I'm not sending the parameters anywhere, so maybe that's why I'm getting a 406 error. but how to send them?

0

There are 0 best solutions below