-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathautomations.go
226 lines (196 loc) · 8.43 KB
/
automations.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package mailerlite
import (
"context"
"fmt"
"net/http"
)
const automationEndpoint = "/automations"
// AutomationService defines an interface for automation-related operations.
type AutomationService interface {
List(ctx context.Context, options *ListAutomationOptions) (*RootAutomations, *Response, error)
Get(ctx context.Context, automationID string) (*RootAutomation, *Response, error)
Subscribers(ctx context.Context, options *ListAutomationSubscriberOptions) (*RootAutomationsSubscriber, *Response, error)
}
type automationService struct {
*service
}
type RootAutomation struct {
Data Automation `json:"data"`
}
type RootAutomations struct {
Data []Automation `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
type RootAutomationsSubscriber struct {
Data []AutomationSubscriber `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
type Automation struct {
ID string `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
TriggerData TriggerData `json:"trigger_data"`
Steps []Step `json:"steps"`
Triggers []Triggers `json:"triggers"`
Complete bool `json:"complete"`
Broken bool `json:"broken"`
Warnings []interface{} `json:"warnings"`
EmailsCount int `json:"emails_count"`
FirstEmailScreenshotURL interface{} `json:"first_email_screenshot_url"`
Stats AutomationStats `json:"stats"`
CreatedAt string `json:"created_at"`
HasBannedContent bool `json:"has_banned_content"`
QualifiedSubscribersCount int `json:"qualified_subscribers_count"`
}
type TriggerData struct {
TrackEcommerce bool `json:"track_ecommerce"`
Repeatable bool `json:"repeatable"`
Valid bool `json:"valid"`
}
type Step struct {
ID string `json:"id"`
Type string `json:"type"`
ParentID string `json:"parent_id"`
Unit string `json:"unit,omitempty"`
Complete bool `json:"complete,omitempty"`
CreatedAt string `json:"created_at"`
YesStepId string `json:"yes_step_id,omitempty"`
NoStepId string `json:"no_step_id,omitempty"`
Broken bool `json:"broken"`
UpdatedAt string `json:"updated_at"`
Value string `json:"value,omitempty"`
MatchingType string `json:"matching_type,omitempty"`
Description string `json:"description"`
Name string `json:"name,omitempty"`
Subject string `json:"subject,omitempty"`
From string `json:"from,omitempty"`
FromName string `json:"from_name,omitempty"`
EmailID string `json:"email_id,omitempty"`
Email *Email `json:"email,omitempty"`
Conditions *[]Condition `json:"conditions,omitempty"`
LanguageID int `json:"language_id,omitempty"`
TrackOpens bool `json:"track_opens,omitempty"`
GoogleAnalytics interface{} `json:"google_analytics,omitempty"`
TrackingWasDisabled bool `json:"tracking_was_disabled,omitempty"`
}
type Condition struct {
Type string `json:"type"`
EmailID string `json:"email_id"`
Action string `json:"action"`
LinkID interface{} `json:"link_id"`
Email AutomationEmailMeta `json:"email"`
}
type AutomationEmailMeta struct {
ID string `json:"id"`
Name string `json:"name"`
URL interface{} `json:"url"`
}
type Triggers struct {
ID string `json:"id"`
Type string `json:"type"`
GroupID string `json:"group_id"`
Group AutomationGroupMeta `json:"group"`
ExcludeGroupIds []interface{} `json:"exclude_group_ids"`
ExcludedGroups []interface{} `json:"excluded_groups"`
Broken bool `json:"broken"`
}
type AutomationGroupMeta struct {
ID string `json:"id"`
Name string `json:"name"`
URL interface{} `json:"url"`
}
type AutomationStats struct {
CompletedSubscribersCount int `json:"completed_subscribers_count"`
SubscribersInQueueCount int `json:"subscribers_in_queue_count"`
BounceRate *BounceRate `json:"bounce_rate,omitempty"`
ClickToOpenRate *ClickToOpenRate `json:"click_to_open_rate,omitempty"`
Sent int `json:"sent"`
OpensCount int `json:"opens_count"`
UniqueOpensCount interface{} `json:"unique_opens_count"`
OpenRate *OpenRate `json:"open_rate,omitempty"`
ClicksCount int `json:"clicks_count"`
UniqueClicksCount interface{} `json:"unique_clicks_count"`
ClickRate *ClickRate `json:"click_rate,omitempty"`
UnsubscribesCount int `json:"unsubscribes_count"`
UnsubscribeRate *UnsubscribeRate `json:"unsubscribe_rate,omitempty"`
SpamCount int `json:"spam_count"`
SpamRate *SpamRate `json:"spam_rate,omitempty"`
HardBouncesCount int `json:"hard_bounces_count"`
HardBounceRate *HardBounceRate `json:"hard_bounce_rate,omitempty"`
SoftBouncesCount int `json:"soft_bounces_count"`
SoftBounceRate *SoftBounceRate `json:"soft_bounce_rate,omitempty"`
}
type AutomationSubscriber struct {
ID string `json:"id"`
Status string `json:"status"`
Date string `json:"date"`
Reason interface{} `json:"reason"`
ReasonDescription string `json:"reason_description"`
Subscriber AutomationSubscriberMeta `json:"subscriber"`
StepRuns []StepRun `json:"stepRuns"`
NextStep Step `json:"nextStep"`
CurrentStep Step `json:"currentStep"`
}
type AutomationSubscriberMeta struct {
ID string `json:"id"`
Email string `json:"email"`
}
type StepRun struct {
ID string `json:"id"`
StepID string `json:"step_id"`
Description string `json:"description"`
ScheduledFor string `json:"scheduled_for"`
}
// ListAutomationOptions - modifies the behavior of AutomationService.List method
type ListAutomationOptions struct {
Filters *[]Filter `json:"filters,omitempty"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
}
// ListAutomationSubscriberOptions - modifies the behavior of AutomationService.Subscribers method
type ListAutomationSubscriberOptions struct {
AutomationID string `url:"-"`
Filters *[]Filter `json:"filters,omitempty"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
}
func (s *automationService) List(ctx context.Context, options *ListAutomationOptions) (*RootAutomations, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, automationEndpoint, options)
if err != nil {
return nil, nil, err
}
root := new(RootAutomations)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *automationService) Get(ctx context.Context, automationID string) (*RootAutomation, *Response, error) {
path := fmt.Sprintf("%s/%s", automationEndpoint, automationID)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(RootAutomation)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *automationService) Subscribers(ctx context.Context, options *ListAutomationSubscriberOptions) (*RootAutomationsSubscriber, *Response, error) {
path := fmt.Sprintf("%s/%s/activity", automationEndpoint, options.AutomationID)
req, err := s.client.newRequest(http.MethodGet, path, options)
if err != nil {
return nil, nil, err
}
root := new(RootAutomationsSubscriber)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}