Overcoat - Mapping multiple types of server responses

465 views Asked by At

I cannot figure out how to map the response array when the response JSON has different envelope keys:

EXAMPLE Responses:

 "response": {
    "currencies": [
      {
        "id": 5,
        "name": "British Pound",
        "shortName": "GBP",
        "symbol": "£",
        "symbolPreceedsValue": true
      },
      {
        "id": 6,
        "name": "U.S. Dollar",
        "shortName": "USD",
        "symbol": "$",
        "symbolPreceedsValue": true
      },

{
  "response": {
    "countries": [
      {
        "id": 9,
        "name": "United Kingdom",
        "shortName": "GB",
        "isMajorMarket": true
      },
      {
        "id": 10,
        "name": "USA",
        "shortName": "US",
        "isMajorMarket": true
      },

I have set up a ServerResponse object:

@implementation ServerResponse

+(NSString *)resultKeyPathForJSONDictionary:(NSDictionary *)JSONDictionary
{
    return @"response";
}

@end

// And setup what I believe to map the response for the Country
+ (NSDictionary *)responseClassesByResourcePath {
    return @{
             @"countries": [SPCCountry class]
             };
}

It results in ONE SPCCountry object being created with null data. I want a LIST of SPCCountry / SCCurrency objects.

ANSWER: For each unique response path, you need to create an OCVResponse subclass and return the class and path in the class method:

+ (NSDictionary *)responseClassesByResourcePath {
    return @{
             @"/install": [ServerResponse class],
             @"/countries": [SPCCountryResponse class],
             @"/currencies": [SPCCurrencyResponse class]
             };

}

The implementation of the response classes need only implement:

@implementation SPCCountryResponse

    +(NSString *)resultKeyPathForJSONDictionary:(NSDictionary *)JSONDictionary
    {
        return @"response.countries";
    }

@end
0

There are 0 answers