forked from customerio/go-customerio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_campaign.go
186 lines (166 loc) · 5.68 KB
/
update_campaign.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
package customerio
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
type UpdateCampaignActionRequest struct {
ParentActionID int64 `json:"parent_action_id,omitempty"`
Created time.Time `json:"created,omitempty"`
Updated time.Time `json:"updated,omitempty"`
Body string `json:"body,omitempty"`
BodyAmp string `json:"body_amp,omitempty"`
Language string `json:"language,omitempty"`
SendingState string `json:"sending_state,omitempty"`
FromID int64 `json:"from_id,omitempty"`
ReplyToID int64 `json:"reply_to_id,omitempty"`
Recipient string `json:"recipient,omitempty"`
Subject string `json:"subject,omitempty"`
PreheaderText string `json:"preheader_text,omitempty"`
Headers []struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"headers,omitempty"`
}
type UpdateCampaignActionResponse struct {
ID string `json:"id"`
CampaignID int64 `json:"campaign_id"`
ParentActionID int64 `json:"parent_action_id"`
DeduplicateID string `json:"deduplicate_id"`
Name string `json:"name"`
Layout string `json:"layout"`
Body string `json:"body"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Type string `json:"type"`
SendingState string `json:"sending_state"`
From string `json:"from"`
FromID int64 `json:"from_id"`
ReplyTo string `json:"reply_to"`
ReplyToID int64 `json:"reply_to_id"`
Preprocessor string `json:"preprocessor"`
Recipient string `json:"recipient"`
Subject string `json:"subject"`
Bcc string `json:"bcc"`
FakeBcc bool `json:"fake_bcc"`
PreheaderText string `json:"preheader_text"`
}
func (t *UpdateCampaignActionResponse) UnmarshalJSON(b []byte) error {
var r struct {
Action struct {
ID string `json:"id"`
CampaignID int64 `json:"campaign_id"`
ParentActionID int64 `json:"parent_action_id"`
DeduplicateID string `json:"deduplicate_id"`
Name string `json:"name"`
Layout string `json:"layout"`
Body string `json:"body"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
Type string `json:"type"`
SendingState string `json:"sending_state"`
From string `json:"from"`
FromID int64 `json:"from_id"`
ReplyTo string `json:"reply_to"`
ReplyToID int64 `json:"reply_to_id"`
Preprocessor string `json:"preprocessor"`
Recipient string `json:"recipient"`
Subject string `json:"subject"`
Bcc string `json:"bcc"`
FakeBcc bool `json:"fake_bcc"`
PreheaderText string `json:"preheader_text"`
} `json:"action"`
}
if err := json.Unmarshal(b, &r); err != nil {
return err
}
t.ID = r.Action.ID
t.CampaignID = r.Action.CampaignID
t.ParentActionID = r.Action.ParentActionID
t.DeduplicateID = r.Action.DeduplicateID
t.Name = r.Action.Name
t.Layout = r.Action.Layout
t.Body = r.Action.Body
t.Created = time.Unix(r.Action.Created, 0)
t.Updated = time.Unix(r.Action.Updated, 0)
t.Type = r.Action.Type
t.SendingState = r.Action.SendingState
t.From = r.Action.From
t.FromID = r.Action.FromID
t.ReplyTo = r.Action.ReplyTo
t.ReplyToID = r.Action.ReplyToID
t.Preprocessor = r.Action.Preprocessor
t.Recipient = r.Action.Recipient
t.Subject = r.Action.Subject
t.Bcc = r.Action.Bcc
t.FakeBcc = r.Action.FakeBcc
t.PreheaderText = r.Action.PreheaderText
return nil
}
type DataError struct {
Errors []Errors `json:"errors"`
}
type Errors struct {
Detail string `json:"detail"`
Status int `json:"status"`
}
func (e *DataError) Error() string {
if len(e.Errors) == 0 {
return "error message undefined"
}
return e.Errors[0].Detail
}
func (c *APIClient) UpdateCampaignLocalizedAction(ctx context.Context, campaignID string, actionID string, locale string, req *UpdateCampaignActionRequest) (*UpdateCampaignActionResponse, error) {
var requestPath string
if locale != "" {
requestPath = fmt.Sprintf("/v1/campaigns/%s/actions/%s/language/%s", url.PathEscape(campaignID), url.PathEscape(actionID), url.PathEscape(locale))
} else {
requestPath = fmt.Sprintf("/v1/campaigns/%s/actions/%s", url.PathEscape(campaignID), url.PathEscape(actionID))
}
body, statusCode, err := c.doRequest(ctx, "PUT", requestPath, req)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
var tmpError = &DataError{}
if err := json.Unmarshal(body, &tmpError); err != nil {
error := Errors{
Status: statusCode,
Detail: string(body),
}
return nil, &DataError{[]Errors{error}}
}
return nil, tmpError
}
var result UpdateCampaignActionResponse
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
return &result, nil
}
func (c *BetaAPIClient) UpdateCampaignAction(ctx context.Context, campaignID string, actionID string, req *UpdateCampaignActionRequest) (*UpdateCampaignActionResponse, error) {
requestPath := fmt.Sprintf("/v1/campaigns/%s/actions/%s", url.PathEscape(campaignID), url.PathEscape(actionID))
body, statusCode, err := c.doRequest(ctx, "PUT", requestPath, req)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
var tmpError = &DataError{}
if err := json.Unmarshal(body, &tmpError); err != nil {
error := Errors{
Status: statusCode,
Detail: string(body),
}
return nil, &DataError{[]Errors{error}}
}
return nil, tmpError
}
var result UpdateCampaignActionResponse
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
return &result, nil
}