-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact_properties.go
More file actions
68 lines (55 loc) · 1.33 KB
/
contact_properties.go
File metadata and controls
68 lines (55 loc) · 1.33 KB
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
package loops
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type ContactProperty struct {
Key string `json:"key"`
Label string `json:"label"`
Type string `json:"type"`
}
func (c *Client) ListContactProperties(customOnly bool) ([]ContactProperty, error) {
req, err := c.newRequest(http.MethodGet, "/contacts/properties", nil)
if err != nil {
return nil, err
}
if customOnly {
q := req.URL.Query()
q.Set("list", "custom")
req.URL.RawQuery = q.Encode()
}
resp, err := c.do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errorFromResponse(resp)
}
var result []ContactProperty
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return result, nil
}
func (c *Client) CreateContactProperty(name, propType string) error {
b, err := json.Marshal(map[string]string{"name": name, "type": propType})
if err != nil {
return fmt.Errorf("failed to encode request: %w", err)
}
req, err := c.newRequest(http.MethodPost, "/contacts/properties", bytes.NewReader(b))
if err != nil {
return err
}
resp, err := c.do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errorFromResponse(resp)
}
return nil
}