-
Notifications
You must be signed in to change notification settings - Fork 6
/
template.go
217 lines (186 loc) · 6.05 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
package transloadit
import (
"context"
"encoding/json"
"fmt"
)
// Template contains details about a single template.
type Template struct {
ID string
Name string
Content TemplateContent
RequireSignatureAuth bool
}
// TemplateContent contains details about the content of a single template.
// The Steps fields maps to the `steps` key in the JSON format. The AdditionalProperties
// field allows you to store additional keys (such as `notify_url`) on the same
// level as the `steps` key.
// For example, the following instance
//
// TemplateContent{
// Steps: map[string]interface{}{
// ":original": map[string]interface{}{
// "robot": "/upload/handle",
// },
// "resize": map[string]interface{}{
// "robot": "/image/resize",
// },
// },
// AdditionalProperties: map[string]interface{}{
// "notify_url": "https://example.com",
// "allow_steps_override": false,
// },
// }
//
// is represented by following JSON:
//
// {
// "steps": {
// ":original": {
// "robot": "/upload/handle"
// },
// "resize": {
// "robot": "/image/resize"
// }
// },
// "allow_steps_override": false,
// "notify_url": "https://example.com"
// }
type TemplateContent struct {
Steps map[string]interface{}
AdditionalProperties map[string]interface{}
}
func (content *TemplateContent) UnmarshalJSON(b []byte) error {
var data map[string]interface{}
if err := json.Unmarshal(b, &data); err != nil {
return err
}
if stepsRaw, ok := data["steps"]; ok {
steps, ok := stepsRaw.(map[string]interface{})
if !ok {
return fmt.Errorf("transloadit: steps property in template content is not an object but %v", stepsRaw)
}
content.Steps = steps
delete(data, "steps")
}
if content.AdditionalProperties == nil {
content.AdditionalProperties = make(map[string]interface{}, len(data))
}
for key, val := range data {
content.AdditionalProperties[key] = val
}
return nil
}
func (content TemplateContent) MarshalJSON() ([]byte, error) {
// Add a hint for the size of the map to reduce the number of necessary allocations
// when filling the map.
numKeys := len(content.AdditionalProperties) + 1
data := make(map[string]interface{}, numKeys)
data["steps"] = content.Steps
for key, val := range content.AdditionalProperties {
data[key] = val
}
return json.Marshal(data)
}
// TemplateList contains a list of templates.
type TemplateList struct {
Templates []Template `json:"items"`
Count int `json:"count"`
}
// NewTemplate returns a new Template struct with initialized values. This
// template will not be saved to Transloadit. To do so, please use the
// Client.CreateTemplate function.
func NewTemplate() Template {
return Template{
Content: TemplateContent{
make(map[string]interface{}),
make(map[string]interface{}),
},
}
}
// AddStep will add the provided step to the Template.Content.Steps map.
func (template *Template) AddStep(name string, step map[string]interface{}) {
template.Content.Steps[name] = step
}
// templateInternal is the struct we use for encoding/decoding the Template
// JSON since we need to convert between boolean and integer.
type templateInternal struct {
ID string `json:"id"`
Name string `json:"name"`
Content TemplateContent `json:"content"`
RequireSignatureAuth int `json:"require_signature_auth"`
}
func (template *Template) UnmarshalJSON(b []byte) error {
var internal templateInternal
if err := json.Unmarshal(b, &internal); err != nil {
return err
}
template.Name = internal.Name
template.Content = internal.Content
template.ID = internal.ID
if internal.RequireSignatureAuth == 1 {
template.RequireSignatureAuth = true
} else {
template.RequireSignatureAuth = false
}
return nil
}
func (template Template) MarshalJSON() ([]byte, error) {
var internal templateInternal
internal.Name = template.Name
internal.Content = template.Content
internal.ID = template.ID
if template.RequireSignatureAuth {
internal.RequireSignatureAuth = 1
} else {
internal.RequireSignatureAuth = 0
}
return json.Marshal(internal)
}
// CreateTemplate will save the provided template struct as a new template
// and return the ID of the new template.
func (client *Client) CreateTemplate(ctx context.Context, template Template) (string, error) {
content := map[string]interface{}{
"name": template.Name,
"template": template.Content,
}
if template.RequireSignatureAuth {
content["require_signature_auth"] = 1
}
if err := client.request(ctx, "POST", "templates", content, &template); err != nil {
return "", err
}
return template.ID, nil
}
// GetTemplate will retrieve details about the template associated with the
// provided template ID.
func (client *Client) GetTemplate(ctx context.Context, templateID string) (template Template, err error) {
err = client.request(ctx, "GET", "templates/"+templateID, nil, &template)
return template, err
}
// DeleteTemplate will delete the template associated with the provided
// template ID.
func (client *Client) DeleteTemplate(ctx context.Context, templateID string) error {
return client.request(ctx, "DELETE", "templates/"+templateID, nil, nil)
}
// UpdateTemplate will update the template associated with the provided
// template ID to match the new name and new content. Please be aware that you
// are not able to change a template's ID.
func (client *Client) UpdateTemplate(ctx context.Context, templateID string, newTemplate Template) error {
// Create signature
content := map[string]interface{}{
"name": newTemplate.Name,
"template": newTemplate.Content,
}
if newTemplate.RequireSignatureAuth {
content["require_signature_auth"] = 1
} else {
content["require_signature_auth"] = 0
}
return client.request(ctx, "PUT", "templates/"+templateID, content, nil)
}
// ListTemplates will retrieve all templates matching the criteria.
func (client *Client) ListTemplates(ctx context.Context, options *ListOptions) (list TemplateList, err error) {
err = client.listRequest(ctx, "templates", options, &list)
return list, err
}