-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfield.go
47 lines (39 loc) · 999 Bytes
/
field.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
package gomailtrain
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
// Field type
type Field struct {
ListID int `json:"-"`
Name string `json:"name"`
Type string `json:"type"`
Group string `json:"group"`
GroupTemplate string `json:"group_template"`
Visible bool `json:"visible"`
}
// AddField to a list
func (a *API) AddField(f Field) error {
ep, err := url.ParseRequestURI(a.endPoint.String() + "/api/field/" + strconv.Itoa(f.ListID))
if err != nil {
return err
}
if !isInList(validFieldTypes, f.Type) {
return fmt.Errorf("%s is not a valid type", f.Type)
}
data := createData(f)
req, err := http.NewRequest(http.MethodPost, ep.String(), strings.NewReader(data.Encode()))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
_, err = a.Request(req)
if err != nil {
return err
}
return nil
}