-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.go
195 lines (166 loc) · 5.22 KB
/
contacts.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package activecampaign
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
)
type Contacts struct {
Contacts []Contact `json:"contacts"`
Meta FieldValuesMeta `json:"meta"`
}
type Contact struct {
CreateDate string `json:"cdate"`
Email string `json:"email"`
Phone string `json:"phone"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
ID string `json:"id"`
UpdateDate string `json:"udate"`
Links ContactLinks `json:"links"`
}
type ContactLinks struct {
BounceLogs string `json:"bounceLogs"`
ContactAutomations string `json:"contactAutomations"`
ContactData string `json:"contactData"`
ContactGoals string `json:"contactGoals"`
ContactLists string `json:"contactLists"`
ContactLogs string `json:"contactLogs"`
ContactTags string `json:"contactTags"`
ContactDeals string `json:"contactDeals"`
Deals string `json:"deals"`
FieldValues string `json:"fieldValues"`
GeoIps string `json:"geoIps"`
Notes string `json:"notes"`
Organization string `json:"organization"`
PlusAppend string `json:"plusAppend"`
TrackingLogs string `json:"trackingLogs"`
ScoreValues string `json:"scoreValues"`
AccountContacts string `json:"accountContacts"`
AutomationEntryCounts string `json:"automationEntryCounts"`
}
func (a *ActiveCampaign) Contacts(ctx context.Context, pof *POF) (*Contacts, error) {
res, err := a.send(ctx, http.MethodGet, "contacts", pof, nil)
if err != nil {
return nil, &Error{Op: "contacts", Err: err}
}
defer res.Body.Close()
var contacts Contacts
err = json.NewDecoder(res.Body).Decode(&contacts)
if err != nil {
return nil, &Error{Op: "contacts", Err: err}
}
return &contacts, nil
}
func (a *ActiveCampaign) ListContacts(ctx context.Context, listID string) (*Contacts, error) {
res, err := a.send(ctx, http.MethodGet, "contacts?listid="+listID+"&status=1", nil, nil)
if err != nil {
return nil, &Error{Op: "list contacts", Err: err}
}
defer res.Body.Close()
var contacts Contacts
err = json.NewDecoder(res.Body).Decode(&contacts)
if err != nil {
return nil, &Error{Op: "list contacts", Err: err}
}
return &contacts, nil
}
func (a *ActiveCampaign) ContactFieldValues(ctx context.Context, pof *POF, id string) (*FieldValues, error) {
return a.fieldValues(ctx, pof, "contacts/"+id+"/fieldValues")
}
type ContactCreate struct {
Email string `json:"email"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
Phone string `json:"phone,omitempty"`
}
type ContactCreated struct {
Email string `json:"email"`
CreateDate string `json:"cdate"`
UpdateDate string `json:"udate"`
Links ContactLinks `json:"links"`
ID string `json:"id"`
}
func (a *ActiveCampaign) ContactCreate(ctx context.Context, contact ContactCreate) (*ContactCreated, error) {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(struct {
Contact ContactCreate `json:"contact"`
}{
Contact: contact,
})
if err != nil {
return nil, &Error{Op: "contact create", Err: err}
}
res, err := a.send(ctx, http.MethodPost, "contacts", nil, b)
if err != nil {
return nil, &Error{Op: "contact create", Err: err}
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return nil, &Error{Op: "contact create", Err: errors.New("wrong status code"), Body: body}
}
var contactCreated struct {
Contact ContactCreated `json:"contact"`
}
err = json.NewDecoder(res.Body).Decode(&contactCreated)
if err != nil {
return nil, err
}
return &contactCreated.Contact, nil
}
func (a *ActiveCampaign) ContactDelete(ctx context.Context, id string) error {
res, err := a.send(ctx, http.MethodDelete, "contacts/"+id, nil, nil)
if err != nil {
return &Error{Op: "contact delete", Err: err}
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
return nil
}
var message struct {
Message string `json:"message"`
}
err = json.NewDecoder(res.Body).Decode(&message)
if err != nil {
return &Error{Op: "contact delete", Err: err}
}
return errors.New("contact delete: " + message.Message)
}
type ContactUpdate struct {
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
func (a *ActiveCampaign) ContactUpdate(ctx context.Context, id string, contact ContactUpdate) error {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(struct {
Contact ContactUpdate `json:"contact"`
}{
Contact: contact,
})
if err != nil {
return &Error{Op: "contact update", Err: err}
}
res, err := a.send(ctx, http.MethodPut, "contacts/"+id, nil, b)
if err != nil {
return &Error{Op: "contact update", Err: err}
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
return nil
}
var message struct {
Message string `json:"message"`
}
err = json.NewDecoder(res.Body).Decode(&message)
if err != nil {
return &Error{Op: "contact update", Err: err}
}
return errors.New("contact update: " + message.Message)
}