forked from exobin/go-mailchimp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_subscription.go
93 lines (82 loc) · 1.91 KB
/
update_subscription.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package gochimp
import (
"encoding/json"
"fmt"
"io/ioutil"
)
func (c *Client) Unsubscribe(listID, email string) (*MemberResponse, error) {
emailMD5 := emailHash(email)
params := map[string]interface{}{
"status": Unsubscribed,
}
resp, err := c.do(
"PATCH",
fmt.Sprintf("/lists/%s/members/%s", listID, emailMD5),
¶ms,
)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Read the response body
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Allow any success status (2xx)
if resp.StatusCode/100 == 2 {
// Unmarshal response into MemberResponse struct
memberResponse := new(MemberResponse)
if err := json.Unmarshal(data, memberResponse); err != nil {
return nil, err
}
return memberResponse, nil
}
// Request failed
errorResponse, err := extractError(data)
if err != nil {
return nil, err
}
return nil, errorResponse
}
// UpdateSubscription ...
func (c *Client) UpdateSubscription(listID, email string, status Status, mergeFields map[string]interface{}) (*MemberResponse, error) {
emailMD5 := emailHash(email)
// Make request
params := map[string]interface{}{
"email_address": email,
"merge_fields": mergeFields,
}
if status != DontChange {
params["status"] = status
}
resp, err := c.do(
"PUT",
fmt.Sprintf("/lists/%s/members/%s", listID, emailMD5),
¶ms,
)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Read the response body
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Allow any success status (2xx)
if resp.StatusCode/100 == 2 {
// Unmarshal response into MemberResponse struct
memberResponse := new(MemberResponse)
if err := json.Unmarshal(data, memberResponse); err != nil {
return nil, err
}
return memberResponse, nil
}
// Request failed
errorResponse, err := extractError(data)
if err != nil {
return nil, err
}
return nil, errorResponse
}