forked from RichardKnop/go-mailchimp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_subscription.go
51 lines (45 loc) · 1.12 KB
/
check_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
package mailchimp
import (
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
)
// CheckSubscription ...
func (c *Client) CheckSubscription(listID string, email string) (*MemberResponse, error) {
// Mailchimp downcases emails anyway, so since email is sent as MD5, CheckSubscription with uppercased emails will fail even if subscription exists.
email = strings.ToLower(email)
// Hash email
emailMD5 := fmt.Sprintf("%x", md5.Sum([]byte(email)))
// Make request
resp, err := c.do(
"GET",
fmt.Sprintf("/lists/%s/members/%s", listID, emailMD5),
nil,
)
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
}