-
Notifications
You must be signed in to change notification settings - Fork 20
/
templates.go
130 lines (99 loc) · 3.63 KB
/
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
package clerk
import "fmt"
type TemplatesService service
type Template struct {
Object string `json:"object"`
Slug string `json:"slug"`
ResourceType string `json:"resource_type"`
TemplateType string `json:"template_type"`
Name string `json:"name"`
Position int `json:"position"`
CanRevert bool `json:"can_revert"`
CanDelete bool `json:"can_delete"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
type TemplateExtended struct {
*Template
Subject string `json:"subject"`
Markup string `json:"markup"`
Body string `json:"body"`
AvailableVariables []string `json:"available_variables"`
RequiredVariables []string `json:"required_variables"`
}
type TemplatePreview struct {
Subject string `json:"subject,omitempty"`
Body string `json:"body"`
}
func (s *TemplatesService) ListAll(templateType string) ([]Template, error) {
templateURL := fmt.Sprintf("%s/%s", TemplatesUrl, templateType)
req, _ := s.client.NewRequest("GET", templateURL)
var templates []Template
_, err := s.client.Do(req, &templates)
if err != nil {
return nil, err
}
return templates, nil
}
func (s *TemplatesService) Read(templateType, slug string) (*TemplateExtended, error) {
templateURL := fmt.Sprintf("%s/%s/%s", TemplatesUrl, templateType, slug)
req, _ := s.client.NewRequest("GET", templateURL)
var templateExtended TemplateExtended
_, err := s.client.Do(req, &templateExtended)
if err != nil {
return nil, err
}
return &templateExtended, nil
}
type UpsertTemplateRequest struct {
Name string `json:"name"`
Subject string `json:"subject,omitempty"`
Markup string `json:"markup,omitempty"`
Body string `json:"body"`
}
type PreviewTemplateRequest struct {
Subject string `json:"subject,omitempty"`
Body string `json:"body"`
}
func (s *TemplatesService) Upsert(templateType, slug string, upsertTemplateRequest *UpsertTemplateRequest) (*TemplateExtended, error) {
templateURL := fmt.Sprintf("%s/%s/%s", TemplatesUrl, templateType, slug)
req, _ := s.client.NewRequest("PUT", templateURL, upsertTemplateRequest)
var upsertedTemplate TemplateExtended
_, err := s.client.Do(req, &upsertedTemplate)
if err != nil {
return nil, err
}
return &upsertedTemplate, nil
}
// Revert reverts a user template to the corresponding system template
func (s *TemplatesService) Revert(templateType, slug string) (*TemplateExtended, error) {
templateURL := fmt.Sprintf("%s/%s/%s/revert", TemplatesUrl, templateType, slug)
req, _ := s.client.NewRequest("POST", templateURL)
var templateExtended TemplateExtended
_, err := s.client.Do(req, &templateExtended)
if err != nil {
return nil, err
}
return &templateExtended, nil
}
// Delete deletes a custom user template
func (s *TemplatesService) Delete(templateType, slug string) (*DeleteResponse, error) {
templateURL := fmt.Sprintf("%s/%s/%s", TemplatesUrl, templateType, slug)
req, _ := s.client.NewRequest("DELETE", templateURL)
var delResponse DeleteResponse
if _, err := s.client.Do(req, &delResponse); err != nil {
return nil, err
}
return &delResponse, nil
}
// Preview returns a rendering of a template with sample data for preview purposes
func (s *TemplatesService) Preview(templateType, slug string, previewTemplateRequest *PreviewTemplateRequest) (*TemplatePreview, error) {
templateURL := fmt.Sprintf("%s/%s/%s/preview", TemplatesUrl, templateType, slug)
req, _ := s.client.NewRequest("POST", templateURL, previewTemplateRequest)
var templatePreview TemplatePreview
_, err := s.client.Do(req, &templatePreview)
if err != nil {
return nil, err
}
return &templatePreview, nil
}