hanzoai / gochimp3

🐒 Golang client for MailChimp API 3.0.

Home Page:https://github.com/zeekay/gochimp3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GetMembers doesn't support Location filter

NorseGaud opened this issue · comments

Forgive me as I'm new to all of this (including go)...
I've been trying to do:

mailchimpConnection := gochimp3.New(mailchimpAPIKey)
mailchimpMembers, err := mailchimpConnection.NewListResponse("XXXXXX").GetMembers(
		&gochimp3.MemberQueryParams{
			ExtendedQueryParams: gochimp3.ExtendedQueryParams{
				BasicQueryParams: gochimp3.BasicQueryParams{
					Fields: []string{"total_items,members.id,members.location"}, // Return less data
				},
				Count: 3,
			},
			Location: &gochimp3.MemberLocation{
				CountryCode: "US",
			},
		},
	)
	if err != nil {
		fmt.Println("Failed to get list...")
		os.Exit(1)
	}

	//// Total US Beta Requestors
	fmt.Printf("Total US Beta Requestors: %d\n", mailchimpMembers.TotalItems)

This kind of works... I get three replies:

{
    "total_items": 53555,
    "_links": null,
    "list_id": "",
    "members": [
        {
            "email_address": "",
            "status": "",
            "language": "",
            "vip": false,
            "location": {
                "latitude": 0,
                "longitude": 0,
                "gmtoff": 0,
                "dstoff": 0,
                "timezone": "",
                "country_code": "US"
            },
            "id": "cbf746b69e309c90552499690c45b6c3",
            "list_id": "",
            "unique_email_id": "",
            "email_type": "",
            "stats": {
                "avg_open_rate": 0,
                "avg_click_rate": 0
            },
            "member_rating": 0,
            "last_changed": "",
            "email_client": "",
            "last_note": {
                "note_id": 0,
                "created_at": "",
                "created_by": "",
                "note": ""
            }
        },
        {
            "email_address": "",
            "status": "",
            "language": "",
            "vip": false,
            "location": {
                "latitude": 0,
                "longitude": 0,
                "gmtoff": 0,
                "dstoff": 0,
                "timezone": "",
                "country_code": "US"
            },
            "id": "e3dc49211a2ed16d3a7e4950dbefd145",
            "list_id": "",
            "unique_email_id": "",
            "email_type": "",
            "stats": {
                "avg_open_rate": 0,
                "avg_click_rate": 0
            },
            "member_rating": 0,
            "last_changed": "",
            "email_client": "",
            "last_note": {
                "note_id": 0,
                "created_at": "",
                "created_by": "",
                "note": ""
            }
        },
        {
            "email_address": "",
            "status": "",
            "language": "",
            "vip": false,
            "location": {
                "latitude": 0,
                "longitude": 0,
                "gmtoff": 0,
                "dstoff": 0,
                "timezone": "",
                "country_code": "AR"
            },
            "id": "9d370d0f280f60805ec59d37f767d7c2",
            "list_id": "",
            "unique_email_id": "",
            "email_type": "",
            "stats": {
                "avg_open_rate": 0,
                "avg_click_rate": 0
            },
            "member_rating": 0,
            "last_changed": "",
            "email_client": "",
            "last_note": {
                "note_id": 0,
                "created_at": "",
                "created_by": "",
                "note": ""
            }
        }
    ]
}
Total US Beta Requestors: 53555

Now, Location params isn't supported currently (as far as I can tell), so I've added them to the members.go:

type MemberQueryParams struct {
	ExtendedQueryParams
	Location *MemberLocation `json:"location,omitempty"`
	Type     string          `json:"type"`
}

// Params MemberQueryParams
func (q MemberQueryParams) Params() map[string]string {
	m := q.ExtendedQueryParams.Params()
	m["location"] = fmt.Sprintf("%v", q.Location)
	m["type"] = q.Type
	return m
}

. . .
func (list ListResponse) GetMembers(params *MemberQueryParams) (*ListOfMembers, error) {
	if err := list.CanMakeRequest(); err != nil {
		return nil, err
	}

	endpoint := fmt.Sprintf(members_path, list.ID)
	response := new(ListOfMembers)

	err := list.api.Request("GET", endpoint, params, nil, response)
	if err != nil {
		return nil, err
	}

	for _, m := range response.Members {
		m.api = list.api
	}

	prettyList1, err := json.MarshalIndent(response, "", "    ")
	if err != nil {
		fmt.Println("Failed to json pretty list...")
		os.Exit(1)
	}
	fmt.Printf("%s\n", string(prettyList1))

	prettyList2, err := json.MarshalIndent(params, "", "    ")
	if err != nil {
		fmt.Println("Failed to json pretty list...")
		os.Exit(1)
	}
	fmt.Printf("%s\n", string(prettyList2))

	return response, nil
}

Results:

{
    "Status": "",
    "SortField": "",
    "SortDirection": "",
    "Fields": [
        "total_items,members.id,members.location.country_code"
    ],
    "ExcludeFields": null,
    "Count": 3,
    "Offset": 0,
    "location": {
        "latitude": 0,
        "longitude": 0,
        "gmtoff": 0,
        "dstoff": 0,
        "timezone": "",
        "country_code": "US"
    },
    "type": ""
}

Shouldn't this return only US country_code entries? Am I misunderstanding how all of this works?

Figured out how to debug the request:

2020/03/11 16:37:00 Requesting GET: https://us15.api.mailchimp.com/3.0/lists/X/members
2020/03/11 16:37:00 Adding query params: map["count":["3"] "fields":["total_items,members.id,members.location.country_code"] "offset":["0"]]
2020/03/11 16:37:00 GET /3.0/lists/X/members?count=3&fields=total_items%2Cmembers.id%2Cmembers.location.country_code&offset=0 HTTP/1.1
Host: us15.api.mailchimp.com
User-Agent: Go-http-client/1.1
Authorization: Basic X
Content-Type: application/json
Accept-Encoding: gzip

2020/03/11 16:37:01 HTTP/2.0 200 OK
Content-Type: application/json; charset=utf-8
Date: Wed, 11 Mar 2020 20:37:00 GMT
Link: <https://us15.api.mailchimp.com/schema/3.0/Lists/Members/Collection.json>; rel="describedBy"
Server: openresty
Set-Cookie: _AVESTA_ENVIRONMENT=prod; path=/
Set-Cookie: _mcid=1X1; expires=Thu, 11-Mar-2021 20:37:00 GMT; Max-Age=31536000; path=/; domain=.mailchimp.com
Vary: Accept-Encoding
X-Request-Id: a78d75d5-8d69-46df-816c-310087ea9a57

{"members":[{"id":"cbf746b69e309c90552499690c45b6c3","location":{"country_code":"US"}},{"id":"e3dc49211a2ed16d3a7e4950dbefd145","location":{"country_code":"US"}},{"id":"9d370d0f280f60805ec59d37f767d7c2","location":{"country_code":"AR"}}],"total_items":53560}

Doesn't seem like things are getting through in the call.

Lol, well, I was reading the wrong API page and turns out you can't GET members and filter by location... Lame