-
Notifications
You must be signed in to change notification settings - Fork 143
/
template.go
243 lines (215 loc) · 6.3 KB
/
template.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package mailgun
import (
"context"
"errors"
"strconv"
)
type TemplateEngine string
// Used by CreateTemplate() and AddTemplateVersion() to specify the template engine
const (
TemplateEngineHandlebars = TemplateEngine("handlebars")
TemplateEngineGo = TemplateEngine("go")
)
type Template struct {
Name string `json:"name"`
Description string `json:"description"`
CreatedAt RFC2822Time `json:"createdAt"`
Version TemplateVersion `json:"version,omitempty"`
}
type templateResp struct {
Item Template `json:"template"`
Message string `json:"message"`
}
type templateListResp struct {
Items []Template `json:"items"`
Paging Paging `json:"paging"`
}
// Create a new template which can be used to attach template versions to
func (mg *MailgunImpl) CreateTemplate(ctx context.Context, template *Template) error {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
if template.Name != "" {
payload.addValue("name", template.Name)
}
if template.Description != "" {
payload.addValue("description", template.Description)
}
if template.Version.Engine != "" {
payload.addValue("engine", string(template.Version.Engine))
}
if template.Version.Template != "" {
payload.addValue("template", template.Version.Template)
}
if template.Version.Comment != "" {
payload.addValue("comment", template.Version.Comment)
}
if template.Version.Tag != "" {
payload.addValue("tag", template.Version.Tag)
}
var resp templateResp
if err := postResponseFromJSON(ctx, r, payload, &resp); err != nil {
return err
}
*template = resp.Item
return nil
}
// GetTemplate gets a template given the template name
func (mg *MailgunImpl) GetTemplate(ctx context.Context, name string) (Template, error) {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
r.addParameter("active", "yes")
var resp templateResp
err := getResponseFromJSON(ctx, r, &resp)
if err != nil {
return Template{}, err
}
return resp.Item, nil
}
// Update the name and description of a template
func (mg *MailgunImpl) UpdateTemplate(ctx context.Context, template *Template) error {
if template.Name == "" {
return errors.New("UpdateTemplate() Template.Name cannot be empty")
}
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + template.Name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
if template.Name != "" {
p.addValue("name", template.Name)
}
if template.Description != "" {
p.addValue("description", template.Description)
}
var resp templateResp
err := putResponseFromJSON(ctx, r, p, &resp)
if err != nil {
return err
}
*template = resp.Item
return nil
}
// Delete a template given a template name
func (mg *MailgunImpl) DeleteTemplate(ctx context.Context, name string) error {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
}
type TemplatesIterator struct {
templateListResp
mg Mailgun
err error
}
type ListTemplateOptions struct {
Limit int
Active bool
}
// List all available templates
func (mg *MailgunImpl) ListTemplates(opts *ListTemplateOptions) *TemplatesIterator {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
if opts != nil {
if opts.Limit != 0 {
r.addParameter("limit", strconv.Itoa(opts.Limit))
}
if opts.Active {
r.addParameter("active", "yes")
}
}
url, err := r.generateUrlWithParameters()
return &TemplatesIterator{
mg: mg,
templateListResp: templateListResp{Paging: Paging{Next: url, First: url}},
err: err,
}
}
// If an error occurred during iteration `Err()` will return non nil
func (ti *TemplatesIterator) Err() error {
return ti.err
}
// Next retrieves the next page of items from the api. Returns false when there
// no more pages to retrieve or if there was an error. Use `.Err()` to retrieve
// the error
func (ti *TemplatesIterator) Next(ctx context.Context, items *[]Template) bool {
if ti.err != nil {
return false
}
ti.err = ti.fetch(ctx, ti.Paging.Next)
if ti.err != nil {
return false
}
cpy := make([]Template, len(ti.Items))
copy(cpy, ti.Items)
*items = cpy
if len(ti.Items) == 0 {
return false
}
return true
}
// First retrieves the first page of items from the api. Returns false if there
// was an error. It also sets the iterator object to the first page.
// Use `.Err()` to retrieve the error.
func (ti *TemplatesIterator) First(ctx context.Context, items *[]Template) bool {
if ti.err != nil {
return false
}
ti.err = ti.fetch(ctx, ti.Paging.First)
if ti.err != nil {
return false
}
cpy := make([]Template, len(ti.Items))
copy(cpy, ti.Items)
*items = cpy
return true
}
// Last retrieves the last page of items from the api.
// Calling Last() is invalid unless you first call First() or Next()
// Returns false if there was an error. It also sets the iterator object
// to the last page. Use `.Err()` to retrieve the error.
func (ti *TemplatesIterator) Last(ctx context.Context, items *[]Template) bool {
if ti.err != nil {
return false
}
ti.err = ti.fetch(ctx, ti.Paging.Last)
if ti.err != nil {
return false
}
cpy := make([]Template, len(ti.Items))
copy(cpy, ti.Items)
*items = cpy
return true
}
// Previous retrieves the previous page of items from the api. Returns false when there
// no more pages to retrieve or if there was an error. Use `.Err()` to retrieve
// the error if any
func (ti *TemplatesIterator) Previous(ctx context.Context, items *[]Template) bool {
if ti.err != nil {
return false
}
if ti.Paging.Previous == "" {
return false
}
ti.err = ti.fetch(ctx, ti.Paging.Previous)
if ti.err != nil {
return false
}
cpy := make([]Template, len(ti.Items))
copy(cpy, ti.Items)
*items = cpy
if len(ti.Items) == 0 {
return false
}
return true
}
func (ti *TemplatesIterator) fetch(ctx context.Context, url string) error {
ti.Items = nil
r := newHTTPRequest(url)
r.setClient(ti.mg.Client())
r.setBasicAuth(basicAuthUser, ti.mg.APIKey())
return getResponseFromJSON(ctx, r, &ti.templateListResp)
}