Ecwid / ecwid-mailchimp

MailChimp API Wrapper for Java
Apache License 2.0
86 stars 83 forks source link

SubscribeMethod sends incorrect JSON for InterestGrouping (API Error 270) #15

Closed johngirvin closed 10 years ago

johngirvin commented 10 years ago

SubscribeMethod (/lists/subscribe) encodes the member's Interest Groups as an array of Interest Group objects, but the API in 2.0 is expecting a simple array of Interest Group names as strings.

(code edited for brevity)

public class MergeVars extends MailChimpObject {
   @Field public List<InterestGrouping> groupings;
}

MergeVars vars = new MergeVars();
vars.groupings = new ArrayList<InterestGrouping>();

InterestGrouping grouping = new InterestGrouping();
grouping.id = 1234;
grouping.groups = new ArrayList<InterestGroup>();

InterestGroup group1 = new InterestGroup();
group1.setName("group1");
grouping.groups.add(group1);

InterestGroup group2 = new InterestGroup();
group2.setName("group2");
grouping.groups.add(group2);

SubscribeMethod subscribeMethod = new SubscribeMethod();
...
subscribeMethod.merge_vars = vars;
mailChimpClient.execute(subscribeMethod);

Based on the above, the JSON sent to the server contains (array of Interest Group objects):

"merge_vars":{
    "groupings":[
        {
            "id":1234,
            "groups":[
                {"name":"group1"},{"name":"group2"}
            ]
        }
    ]
}

This results in an API error: com.ecwid.mailchimp.MailChimpException: API Error (270): "Array" is not a valid Interest Group in Grouping "Email Preferences" on the list: ...

Modifying the JSON to the following allows the request to complete successfully and modify the member's groups.

"merge_vars":{
    "groupings":[
        {
            "id":1234,
            "groups":[ "group1","group2" ]
        }
    ]
}

As a workaround I am using a custom Interest Grouping mergevar that has a simple List for the groups field.

public class ListSubscribeInterestGrouping extends MailChimpObject {
    @Field public Integer id;
    @Field public List<String> groups;
}
johngirvin commented 10 years ago

Is List<InterestGrouping> the correct type for the field SubscribeMethod.merge_vars.groupings ?

basiliscus commented 10 years ago

Hi John,

Is List the correct type for the field SubscribeMethod.merge_vars.groupings ?

No.

InterestGrouping is supposed to be used in the result of the "listInterestGroupings" method (API v1.3).

Apparently, the "/lists/subscribe" method uses different format for the group names. Perhaps, you should create and use a class like this:

public class MyInterestGrouping extends MailChimpObject {
    @Field
    public Integer id;

    @Field
    public String name;

    @Field
    public List<String> groups;
}
johngirvin commented 10 years ago

Thanks for the clarification. I'm using my own class as above and it's working.