Reputation: 10532
I am doing a multi-part posting of image data and some values using RestKit's RKClient like so:
RKParams* params = [RKParams params];
[params setValue:foo.accountId forParam:@"accountId"];
[params setValue:foo.identifier forParam:@"fooId"];
[params setValue:_photoId forParam:@"photoId"];
[params setData:data MIMEType:@"image/png" forParam:@"image"];
[[RKClient sharedClient] post:@"/foo/uploadPhoto" params:params delegate:self];
This works great, and my backend server responds with JSON representation of the server side model object, it look like this:
{"id":"4ee2b4670364720c089e75b9","accountId":"4ebee3469ae2d8adf983c561","fooId":"4ec0983d036463d900841f0b","photoId":"E5B20AF1-9F10-4175-8262-852BDA3DEDE9","filename":"4ebee3469ae2d8adf983c561_4ec0983d036463d900841f0b_E5B20AF1-9F10-4175-8262-852BDA3DEDE9","contentType":"image/png"}
What I need to do now is map this to my client side (iOS) model object. The client side model object is almost the same, but not identical (so using RKJSONParser's objectFromString method is not an option), therefore I have a custom RKObjectMapping defined that handles the mapping. RKClient's delegate only gets a RKResponse, so how can I use the response along with the mapper to get an instance of my client side model object?
Note: To be clear, I am very familiar how this works when using RKObjectManager to post an object and map a response. The unique part of my situation is that I am using RKClient to achieve the multi-part post. Unfortunately RKClient doesn't seem to have simple methods available to handle response mapping like RKObjectManager does... unless I am missing something (which I hope and am and you all will point out for me ;).
Upvotes: 1
Views: 2364
Reputation: 10532
Well, this post was similar (but non-functional) and it gave me some ideas of a new technique of using this method on RKObjectLoader
- (RKObjectLoader *)postObject:(id<NSObject>)object delegate:(id<RKObjectLoaderDelegate>)delegate block:(void ( ^ ) ( RKObjectLoader *))block
So now I could get the benefit of mapping that wasn't obvious how to get using RKClient.
Router setup:
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:kApiUrlBase];
[objectManager.router routeClass:[PAPetPhoto class] toResourcePath:@"/pet/uploadPhoto" forMethod:RKRequestMethodPOST];
Mapping setup:
RKObjectMapping *papetPhotoMapping = [RKObjectMapping mappingForClass:[PAPetPhoto class]];
[papetPhotoMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[papetPhotoMapping mapAttributes:@"accountId", @"petId", @"photoId", @"filename", @"contentType", nil];
[objectManager.mappingProvider addObjectMapping:papetPhotoMapping];
[objectManager.mappingProvider setSerializationMapping:[papetPhotoMapping inverseMapping] forClass:[PAPetPhoto class]];
[objectManager.mappingProvider setMapping:papetPhotoMapping forKeyPath:@"petPhoto"];
The post: (notice since I built up all my params in the block my object is just a dummy instance to trigger the proper routing and mapper).
PAPetPhoto *photo = [[PAPetPhoto alloc] init];
[[RKObjectManager sharedManager] postObject:photo delegate:self block:^(RKObjectLoader *loader){
RKParams* params = [RKParams params];
[params setValue:pet.accountId forParam:@"accountId"];
[params setValue:pet.identifier forParam:@"petId"];
[params setValue:_photoId forParam:@"photoId"];
[params setValue:_isThumb ? @"THUMB" : @"FULL" forParam:@"photoSize"];
[params setData:data MIMEType:@"image/png" forParam:@"image"];
loader.params = params;
}];
Server endpoint (Java, Spring MVC)
@RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> handleFormUpload(@RequestParam("accountId") String accountId,
@RequestParam("petId") String petId,
@RequestParam("photoId") String photoId,
@RequestParam("photoSize") PhotoSizeEnum photoSize,
@RequestParam("image") Part image) throws IOException {
if (log.isTraceEnabled())
log.trace("uploadPhoto. accountId=" + accountId + " petId=" + petId + " photoId=" + photoId + " photoSize=" + photoSize);
PetPhoto petPhoto = petDao.savePetPhoto(accountId, petId, photoId, photoSize, image);
Map<String, Object> map = GsonUtils.wrapWithKeypath(petPhoto, "petPhoto");
return map;
}
Server response JSON (note the keyPath of "petPhoto" that corresponds to the mapping setup):
{
petPhoto = {
accountId = 4ebee3469ae2d8adf983c561;
contentType = "image/png";
filename = "4ebee3469ae2d8adf983c561_4ec0983d036463d900841f09_3FED4959-1042-4D8B-91A8-76AA873851A3";
id = 4ee2e80203646ecd096d5201;
petId = 4ec0983d036463d900841f09;
photoId = "3FED4959-1042-4D8B-91A8-76AA873851A3";
};
}
Delegate:
- (void) objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object {
if ([objectLoader wasSentToResourcePath:@"/pet/uploadPhoto"]) {
PAPetPhoto *photo = (PAPetPhoto*)object;
}
}
Upvotes: 1