-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_field_values.go
162 lines (141 loc) · 4.09 KB
/
custom_field_values.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
package activecampaign
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
type FieldValues struct {
FieldValues []FieldValue `json:"fieldValues"`
Meta FieldValuesMeta `json:"meta"`
}
type FieldValuesMeta struct {
Total string `json:"total"`
}
type FieldValue struct {
Contact string `json:"contact"`
Field string `json:"field"`
Value string `json:"value"`
CreateDate string `json:"cdate"`
UpdateDate string `json:"udate"`
ID string `json:"id"`
}
func (a *ActiveCampaign) fieldValues(ctx context.Context, pof *POF, url string) (*FieldValues, error) {
res, err := a.send(ctx, http.MethodGet, url, pof, nil)
if err != nil {
return nil, &Error{Op: "field values", Err: err}
}
defer res.Body.Close()
var values FieldValues
err = json.NewDecoder(res.Body).Decode(&values)
if err != nil {
return nil, &Error{Op: "field values", Err: err}
}
return &values, nil
}
func (a *ActiveCampaign) FieldValues(ctx context.Context, pof *POF) (*FieldValues, error) {
return a.fieldValues(ctx, pof, "fieldValues")
}
type ChangeFieldValue struct {
Contact string `json:"contact"`
Field string `json:"field"`
Value string `json:"value"`
}
func (a *ActiveCampaign) FieldValueCreate(ctx context.Context, create ChangeFieldValue) error {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(struct {
FieldValue ChangeFieldValue `json:"fieldValue"`
}{
FieldValue: create,
})
if err != nil {
return &Error{Op: "field value create", Err: err}
}
res, err := a.send(ctx, http.MethodPost, "fieldValues", nil, b)
if err != nil {
return &Error{Op: "field value create", Err: err}
}
defer res.Body.Close()
if !(res.StatusCode == http.StatusCreated || res.StatusCode == http.StatusOK) {
return errors.New("field value create: " + res.Status)
}
return nil
}
func (a *ActiveCampaign) FieldValueUpdate(ctx context.Context, id string, update ChangeFieldValue) error {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(struct {
FieldValue ChangeFieldValue `json:"fieldValue"`
}{
FieldValue: update,
})
if err != nil {
return &Error{Op: "field value update", Err: err}
}
res, err := a.send(ctx, http.MethodPut, "fieldValues/"+id, nil, b)
if err != nil {
return &Error{Op: "field value update", Err: err}
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return &Error{Op: "field value update", Err: err}
}
return errors.New("field value update: " + res.Status + ": " + string(b))
}
return nil
}
type FieldOptions struct {
FieldOptions []FieldOption `json:"fieldOptions"`
}
type FieldOption struct {
Field string `json:"field"`
OrderID string `json:"orderid"`
Value string `json:"value"`
Label string `json:"label"`
IsDefault string `json:"isdefault"`
CreateDate string `json:"cdate"`
UpdateDate string `json:"udate"`
ID string `json:"id"`
}
func (a *ActiveCampaign) FieldOptions(ctx context.Context, field string) ([]FieldOption, error) {
res, err := a.send(ctx, http.MethodGet, fmt.Sprintf("fields/%s/options", field), nil, nil)
if err != nil {
return nil, &Error{Op: "field options", Err: err}
}
defer res.Body.Close()
var values FieldOptions
err = json.NewDecoder(res.Body).Decode(&values)
if err != nil {
return nil, &Error{Op: "field options", Err: err}
}
return values.FieldOptions, nil
}
type CreateFieldOption struct {
Field string `json:"field"`
Value string `json:"value"`
Label string `json:"label"`
}
func (a *ActiveCampaign) FieldOptionCreate(ctx context.Context, create []CreateFieldOption) error {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(struct {
FieldOptions []CreateFieldOption `json:"fieldOptions"`
}{
FieldOptions: create,
})
if err != nil {
return &Error{Op: "field options create", Err: err}
}
res, err := a.send(ctx, http.MethodPost, "fieldOption/bulk", nil, b)
if err != nil {
return &Error{Op: "field options create", Err: err}
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
return errors.New("field options create: " + res.Status)
}
return nil
}