How to use sub dictionary in Mantle with Overcoat?

108 views Asked by At

I decided to use Mantle to consume some JSON endpoints, but the structure of the JSON is different depending on whether you're GETing or POSTing an object. Take the users endpoint for instance:

When requesting a user you get a response similar to this:

{
    "random_meta_data": "whatever",
    "etc.": "etc.",
    "payload": {
        "username": "username",
        "email": "[email protected]",
        "etc.": "etc."
    }
}

When creating a user you need to send something like this:

{
    "username": "username",
    "email": "[email protected]",
    "etc.": "etc."
}

It's not a difficult problem to solve, but it seems like a common enough problem that Mantle should be able to solve it for you.

I know I could simply initialize the mantle model with dictionary[@"payload"], but Overcoat is doing the mapping for me automatically and if I'm going to do that manually I'm not taking advantage of Overcoat anymore.

So I'm wondering if there is a standard way of solving this with Mantle and/or Overcoat?

1

There are 1 answers

0
Erik B On

What you are describing is called an envelop response and you can read about how to deal with those in the Overcoat README.md:

https://github.com/Overcoat/Overcoat#envelop-and-error-responses

Other services like App.net use an envelop response, which is a top level JSON response containing the data requested and additional metadata. For these kind of services, you must create your own OVCResponse subclass and specify the data key path.

@interface AppDotNetResponse : OVCResponse
...
@end

@implementation AppDotNetResponse
+ (NSString *)resultKeyPathForJSONDictionary:(NSDictionary *)JSONDictionary 
{
    return @"data";
}
@end

You can then specify which response class to use in your client by overriding +responseClass.

+ (Class)responseClass {
    return [AppDotNetResponse class];
}