-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcampaign.go
More file actions
115 lines (102 loc) · 3.21 KB
/
campaign.go
File metadata and controls
115 lines (102 loc) · 3.21 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
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
package plunk
import (
"context"
"net/http"
"net/url"
"strconv"
)
// Campaign audience type constants.
const (
AudienceAll = "ALL"
AudienceSegment = "SEGMENT"
AudienceFiltered = "FILTERED"
)
// Campaign status constants.
const (
CampaignDraft = "DRAFT"
CampaignScheduled = "SCHEDULED"
CampaignSending = "SENDING"
CampaignSent = "SENT"
)
// Campaign represents a Plunk campaign.
type Campaign struct {
ID string `json:"id"`
Name string `json:"name"`
Subject string `json:"subject"`
Type string `json:"type"`
Status string `json:"status"`
ScheduledAt *string `json:"scheduledAt"`
}
// CreateCampaignRequest is the request for [Client.CreateCampaign].
type CreateCampaignRequest struct {
Name string `json:"name"`
Subject string `json:"subject"`
Body string `json:"body"`
From string `json:"from"`
AudienceType string `json:"audienceType"`
Description string `json:"description,omitempty"`
FromName string `json:"fromName,omitempty"`
ReplyTo string `json:"replyTo,omitempty"`
SegmentID string `json:"segmentId,omitempty"`
AudienceFilter map[string]any `json:"audienceFilter,omitempty"`
}
// CreateCampaign creates a new campaign. It requires a secret API key.
func (c *Client) CreateCampaign(ctx context.Context, req *CreateCampaignRequest) (*Campaign, error) {
var resp struct {
Success bool `json:"success"`
Data Campaign `json:"data"`
}
if err := c.do(ctx, http.MethodPost, "/campaigns", req, &resp); err != nil {
return nil, err
}
return &resp.Data, nil
}
// ListCampaignsRequest is the request for [Client.ListCampaigns].
type ListCampaignsRequest struct {
Limit int
Cursor string
Status string
}
// ListCampaignsResponse is the response from [Client.ListCampaigns].
type ListCampaignsResponse struct {
Campaigns []Campaign `json:"campaigns"`
Total int `json:"total"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
TotalPages int `json:"totalPages"`
}
// ListCampaigns retrieves a paginated list of campaigns. It requires a secret
// API key.
func (c *Client) ListCampaigns(ctx context.Context, req *ListCampaignsRequest) (*ListCampaignsResponse, error) {
q := url.Values{}
if req != nil {
if req.Limit > 0 {
q.Set("limit", strconv.Itoa(req.Limit))
}
if req.Cursor != "" {
q.Set("cursor", req.Cursor)
}
if req.Status != "" {
q.Set("status", req.Status)
}
}
path := "/campaigns"
if len(q) > 0 {
path += "?" + q.Encode()
}
var resp ListCampaignsResponse
if err := c.do(ctx, http.MethodGet, path, nil, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// SendCampaignRequest is the request for [Client.SendCampaign].
type SendCampaignRequest struct {
ScheduledFor *string `json:"scheduledFor"`
}
// SendCampaign sends or schedules a campaign. Set ScheduledFor to an ISO 8601
// timestamp to schedule, or leave it nil to send immediately. It requires a
// secret API key.
func (c *Client) SendCampaign(ctx context.Context, id string, req *SendCampaignRequest) error {
return c.do(ctx, http.MethodPost, "/campaigns/"+id+"/send", req, nil)
}