-
Notifications
You must be signed in to change notification settings - Fork 65
/
mandrill_templates.go
156 lines (143 loc) · 6.5 KB
/
mandrill_templates.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
// Copyright 2013 Matthew Baird
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gochimp
import (
"errors"
"log"
)
// see https://mandrillapp.com/api/docs/templates.html
const templates_add_endpoint string = "/templates/add.json" //Add a new template
const templates_info_endpoint string = "/templates/info.json" //Get the information for an existing template
const templates_update_endpoint string = "/templates/update.json" //Update the code for an existing template
// Publish the content for the template. Any new messages sent using this template will start
//using the content that was previously in draft.
const templates_publish_endpoint string = "/templates/publish.json"
const templates_delete_endpoint string = "/templates/delete.json" //Delete a template
const templates_list_endpoint string = "/templates/list.json" //Return a list of all the templates available to this user
const templates_time_series_endpoint string = "/templates/time-series.json" //Return the recent history (hourly stats for the last 30 days) for a template
const templates_render_endpoint string = "/templates/render.json" //Inject content and optionally merge fields into a template, returning the HTML that results
// can error with one of the following: Unknown_Template, Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) TemplateAdd(name string, code string, publish bool) (Template, error) {
if name == "" {
return Template{}, errors.New("name cannot be blank")
}
if code == "" {
return Template{}, errors.New("code cannot be blank")
}
var params map[string]interface{} = make(map[string]interface{})
params["name"] = name
params["code"] = code
params["publish"] = publish
return execute(a, params, templates_add_endpoint)
}
// can error with one of the following: Unknown_Template, Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) TemplateInfo(name string) (Template, error) {
if name == "" {
return Template{}, errors.New("name cannot be blank")
}
var params map[string]interface{} = make(map[string]interface{})
params["name"] = name
return execute(a, params, templates_info_endpoint)
}
// can error with one of the following: Unknown_Template, Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) TemplateUpdate(name string, code string, publish bool) (Template, error) {
if name == "" {
return Template{}, errors.New("name cannot be blank")
}
if code == "" {
return Template{}, errors.New("code cannot be blank")
}
var params map[string]interface{} = make(map[string]interface{})
params["name"] = name
params["code"] = code
params["publish"] = publish
return execute(a, params, templates_update_endpoint)
}
// can error with one of the following: Unknown_Template, Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) TemplatePublish(name string) (Template, error) {
if name == "" {
return Template{}, errors.New("name cannot be blank")
}
var params map[string]interface{} = make(map[string]interface{})
params["name"] = name
return execute(a, params, templates_publish_endpoint)
}
// can error with one of the following: Unknown_Template, Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) TemplateDelete(name string) (Template, error) {
if name == "" {
return Template{}, errors.New("name cannot be blank")
}
var params map[string]interface{} = make(map[string]interface{})
params["name"] = name
return execute(a, params, templates_delete_endpoint)
}
// can error with one of the following: Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) TemplateList() ([]Template, error) {
var response []Template
var params map[string]interface{} = make(map[string]interface{})
err := parseMandrillJson(a, templates_list_endpoint, params, &response)
return response, err
}
// can error with one of the following: Unknown_Template, Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) TemplateTimeSeries(name string) ([]Template, error) {
if name == "" {
return []Template{}, errors.New("name cannot be blank")
}
var response []Template
var params map[string]interface{} = make(map[string]interface{})
params["name"] = name
err := parseMandrillJson(a, templates_time_series_endpoint, params, &response)
return response, err
}
// can error with one of the following: Unknown_Template, Invalid_Key, ValidationError, GeneralError
func (a *MandrillAPI) TemplateRender(templateName string, templateContent []Var, mergeVars []Var) (string, error) {
if templateName == "" {
return "", errors.New("templateName cannot be blank")
}
var response map[string]interface{}
var params map[string]interface{} = make(map[string]interface{})
params["template_name"] = templateName
params["template_content"] = templateContent
params["merge_vars"] = mergeVars
err := parseMandrillJson(a, templates_render_endpoint, params, &response)
var retval string = ""
var ok bool = false
if err == nil {
retval, ok = response["html"].(string)
if ok != true {
log.Fatal("Received response with html parameter, however type was not string, this should not happen")
}
}
return retval, err
}
func execute(a *MandrillAPI, params map[string]interface{}, endpoint string) (Template, error) {
var response Template
err := parseMandrillJson(a, endpoint, params, &response)
return response, err
}
type Template struct {
Name string `json:"name"`
Code string `json:"code"`
PublishName string `json:"publish_name"`
PublishCode string `json:"publish_code"`
Slug string `json:"slug"`
Subject string `json:"subject"`
CreatedAt APITime `json:"published_at"`
UpdateAt APITime `json:"updated_at"`
FromEmail string `json:"from_email"`
FromName string `json:"from_name"`
Text string `json:"text"`
PublishFromEmail string `json:"publish_from_email"`
PublishFromName string `json:"publish_from_name"`
PublishText string `json:"publish_text"`
PublishSubject string `json:"publish_subject"`
PublishAt APITime `json:"published_at"`
}