-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegments.go
97 lines (77 loc) · 2.8 KB
/
segments.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
package contacts
import (
"fmt"
"net/http"
)
type Segment struct {
ID uint `json:"id"`
Name string `json:"name"`
ListID int `json:"list_id"`
Conditions []Condition `json:"conditions"`
}
type Condition struct {
Field string `json:"field"`
Value string `json:"value"`
Operator string `json:"operator"`
AndOr string `json:"and_or"`
}
type SegmentsClient struct {
client *Client
}
// Create a Segment
//
// The response of the initial Create will return a recipient_count of 0 because it takes some time to populate.
// Follow up with a Get to verify segment size.
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Create-a-Segment-POST
func (c *SegmentsClient) Create(segment *Segment) error {
return c.client.makeRequest(http.MethodPost, "/contactdb/segments", segment, &segment)
}
type listSegmentsResponse struct {
Segments []*Segment `json:"segments"`
}
// List all Segments
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#List-All-Segments-GET
func (c *SegmentsClient) List() ([]*Segment, error) {
var resp *listSegmentsResponse
err := c.client.makeRequest(http.MethodGet, "/contactdb/segments", nil, &resp)
if err != nil {
return nil, err
}
return resp.Segments, nil
}
// Get (Retrieve) a Segment
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Retrieve-a-Segment-GET
func (c *SegmentsClient) Get(segmentID uint) (*Segment, error) {
var segment *Segment
err := c.client.makeRequest(http.MethodGet, fmt.Sprintf("/contactdb/segments/%d", segmentID), nil, &segment)
if err != nil {
return nil, err
}
return segment, nil
}
// Update a Segment
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Update-a-Segment-PATCH
func (c *SegmentsClient) Update(segment *Segment) error {
return c.client.makeRequest(http.MethodPatch, fmt.Sprintf("/contactdb/segments/%d", segment.ID), segment, &segment)
}
// Delete a Segment
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Delete-a-Segment-DELETE
func (c *SegmentsClient) Delete(segmentID uint) error {
return c.client.makeRequest(http.MethodDelete, fmt.Sprintf("/contactdb/segments/%d", segmentID), nil, nil)
}
// ListRecipients on a Segment
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#List-Recipients-On-a-Segment-GET
func (c *SegmentsClient) ListRecipients(segmentID, pageSize, page uint) ([]*Recipient, error) {
var resp *listRecipientsResponse
err := c.client.makeRequest(http.MethodGet, fmt.Sprintf("/contactdb/segments/%d/recipients?page_size=%d&page=%d", segmentID, pageSize, page), nil, &resp)
if err != nil {
return nil, err
}
return resp.Recipients, nil
}