-
Notifications
You must be signed in to change notification settings - Fork 14
/
emails_mailings_service.go
168 lines (144 loc) · 5.74 KB
/
emails_mailings_service.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
package sendpulse_sdk_go
import (
"context"
b64 "encoding/base64"
"fmt"
"net/http"
"strconv"
)
// CampaignsService is a service to interact with campaigns
type CampaignsService struct {
client *Client
}
// newCampaignsService creates CampaignsService
func newCampaignsService(cl *Client) *CampaignsService {
return &CampaignsService{client: cl}
}
// Campaign describes campaign params
type CampaignParams struct {
SenderName string `json:"sender_name"`
SenderEmail string `json:"sender_email"`
Subject string `json:"subject"`
Body string `json:"body,omitempty"`
TemplateID string `json:"template_id,omitempty"`
MailingListID int `json:"list_id,omitempty"`
SegmentID int `json:"segment_id,omitempty"`
IsTest bool `json:"is_test,omitempty"`
SendDate DateTimeType `json:"send_date,omitempty"`
Name string `json:"name,omitempty"`
Attachments map[string]string `json:"attachments"`
Type string `json:"type,omitempty"`
BodyAMP string `json:"body_amp,omitempty"`
}
// Campaign describes a campaign
type Campaign struct {
ID int `json:"id"`
Name string `json:"name"`
Message struct {
SenderName string `json:"sender_name"`
SenderEmail string `json:"sender_email"`
Subject string `json:"subject"`
Body string `json:"body"`
Attachments string `json:"attachments"`
MailingListID int `json:"list_id"`
}
Status int `json:"status"`
AllEmailQty int `json:"all_email_qty"`
TariffEmailQty int `json:"tariff_email_qty"`
PaidEmailQty int `json:"paid_email_qty"`
OverdraftPrice float32 `json:"overdraft_price"`
OverdraftCurrency string `json:"overdraft_currency"`
SendDate DateTimeType `json:"send_date"`
}
// CreateCampaign creates a campaign. Please note that you can send a maximum of 4 campaigns per hour
func (service *CampaignsService) CreateCampaign(ctx context.Context, data CampaignParams) (*Campaign, error) {
path := "/campaigns"
var innerMailing struct {
Campaign
OverdraftPrice string `json:"overdraft_price"`
}
if data.Body != "" {
data.Body = b64.StdEncoding.EncodeToString([]byte(data.Body))
}
if data.BodyAMP != "" {
data.BodyAMP = b64.StdEncoding.EncodeToString([]byte(data.BodyAMP))
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, data, &innerMailing, true)
if err != nil {
return nil, err
}
f64, _ := strconv.ParseFloat(innerMailing.OverdraftPrice, 32)
innerMailing.Campaign.OverdraftPrice = float32(f64)
return &innerMailing.Campaign, err
}
// UpdateCampaign updates a scheduled campaign
func (service *CampaignsService) UpdateCampaign(ctx context.Context, id int, data CampaignParams) error {
path := fmt.Sprintf("/campaigns/%d", id)
var respData struct {
Result bool `json:"result"`
Id int `json:"id"`
}
if data.Body != "" {
data.Body = b64.StdEncoding.EncodeToString([]byte(data.Body))
}
if data.BodyAMP != "" {
data.BodyAMP = b64.StdEncoding.EncodeToString([]byte(data.BodyAMP))
}
_, err := service.client.newRequest(ctx, http.MethodPatch, path, data, &respData, true)
return err
}
// GetCampaign returns an information about specific campaign
func (service *CampaignsService) GetCampaign(ctx context.Context, id int) (*Campaign, error) {
path := fmt.Sprintf("/campaigns/%d", id)
var respData Campaign
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return &respData, err
}
// GetCampaigns returns a list of campaigns
func (service *CampaignsService) GetCampaigns(ctx context.Context, limit int, offset int) ([]*Campaign, error) {
path := fmt.Sprintf("/campaigns?limit=%d&offset=%d", limit, offset)
var items []*Campaign
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &items, true)
return items, err
}
// Task represents a campaign
type Task struct {
ID int `json:"task_id"`
Name string `json:"task_name"`
Status int `json:"task_status"`
}
// GetCampaignsByMailingList returns a list of campaigns by specific mailing list
func (service *CampaignsService) GetCampaignsByMailingList(ctx context.Context, mailingListID, limit, offset int) ([]*Task, error) {
path := fmt.Sprintf("/addressbooks/%d/campaigns?limit=%d&offset=%d", mailingListID, limit, offset)
var tasks []*Task
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &tasks, true)
return tasks, err
}
// GetCampaignCountriesStatistics represents campaign statistics of countries
func (service *CampaignsService) GetCampaignCountriesStatistics(ctx context.Context, id int) (map[string]int, error) {
path := fmt.Sprintf("/campaigns/%d/countries", id)
response := make(map[string]int)
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &response, true)
return response, err
}
// MailingRefStat represents campaign statistics of referrals
type MailingRefStat struct {
Link string `json:"link"`
Count int `json:"count"`
}
// GetCampaignReferralsStatistics returns campaign statistics of referrals
func (service *CampaignsService) GetCampaignReferralsStatistics(ctx context.Context, id int) ([]*MailingRefStat, error) {
path := fmt.Sprintf("/campaigns/%d/referrals", id)
var response []*MailingRefStat
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &response, true)
return response, err
}
// CancelCampaign cancels a scheduled campaign
func (service *CampaignsService) CancelCampaign(ctx context.Context, id int) error {
path := fmt.Sprintf("/campaigns/%d", id)
var response struct {
Result bool `json:"result"`
}
_, err := service.client.newRequest(ctx, http.MethodDelete, path, nil, &response, true)
return err
}