/
api_contact.go
141 lines (125 loc) · 3.61 KB
/
api_contact.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package elasticemail
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/fatih/structs"
"io/ioutil"
"mime/multipart"
"net/http"
"reflect"
)
const (
contactEndpoint = "contact"
mAddContact = "add"
)
type AddContactParams struct {
Email string `json:"email" url:"email"`
PublicAccountID string `json:"publicaccountid" url:"publicaccountid"`
ActivationReturnUrl string `json:"activationreturnurl" url:"activationreturnurl"`
ActivationTemplate string `json:"activationtemplate" url:"activationtemplate"`
AlreadyActiveUrl string `json:"alreadyactiveurl" url:"alreadyactiveurl"`
ConsentDate string `json:"consentdate" url:"consentdate"`
ConsentIP string `json:"consentip" url:"consentip"`
ConsentTracking int `json:"consenttracking" url:"consenttracking"` // ConsentTracking Enumeration
Field []string `json:"field" url:"field"`
FirstName string `json:"firstname" url:"firstname"`
LastName string `json:"lastname" url:"lastname"`
ListName []string `json:"listname" url:"listname"`
NotifyEmail string `json:"notifyemail" url:"notifyemail"`
PublicListID []string `json:"publiclistid" url:"publiclistid"`
ReturnUrl string `json:"returnurl" url:"returnurl"`
SendActivation bool `json:"sendactivation" url:"sendactivation"`
Source int `json:"source" url:"source"` // ContactSource Enumeration
SourceUrl string `json:"sourceurl" url:"sourceurl"`
}
// Add a new contact and optionally to one of your lists. Note that your API KEY is not required for this call.
// return nil if success
func (m *ElasticEmailImpl) AddContact(params AddContactParams) (err error) {
url := fmt.Sprintf("%s/%s/%s", m.apiBase, contactEndpoint, mAddContact)
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
contentType := bodyWriter.FormDataContentType()
paramsMap := structs.Map(params)
for k, v := range paramsMap {
switch x := v.(type) {
case []string:
for _, mv := range x {
err = bodyWriter.WriteField(k, mv)
if err != nil {
return err
}
}
case bool:
if x {
err = bodyWriter.WriteField(k, "true")
if err != nil {
return err
}
} else {
err = bodyWriter.WriteField(k, "false")
if err != nil {
return err
}
}
case int, int8:
if str, ok := x.(string); ok {
err = bodyWriter.WriteField(k, str)
if err != nil {
return err
}
}
case string:
err = bodyWriter.WriteField(k, x)
if err != nil {
return err
}
default:
value, ok := v.(string)
if !ok {
return errors.New(fmt.Sprintf("not provided param type: %s : %s (%s)", k, v, reflect.TypeOf(v)))
}
err = bodyWriter.WriteField(k, value)
if err != nil {
return err
}
}
}
bodyWriter.Close()
req, err := http.NewRequest("POST", url, bodyBuf)
if err != nil {
return err
}
req.Header.Add("content-type", contentType)
resp, err := m.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
f, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
dd := make(map[string]interface{})
json.Unmarshal(f, &dd)
success, ok := dd["success"]
if !ok {
return errors.New("json response does not contain success field")
}
if success == true {
return nil
} else {
errorMsg, ok := dd["error"]
if !ok {
return errors.New("json response does not contain 'error' field")
}
err = errors.New(errorMsg.(string))
return err
}
} else {
err = errors.New(resp.Status)
return err
}
}