This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
105 lines (87 loc) · 2.7 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
package transloadit
import (
"fmt"
)
type Template struct {
Name string `json:"name"`
// See AddStep for simple usage.
Steps map[string]map[string]interface{} `json:"content"`
}
type templateGetResponse struct {
Name string `json:"name"`
Content struct {
Steps map[string]map[string]interface{} `json:"steps"`
} `json:"content"`
}
type TemplateList struct {
Templates []TemplateListItem `json:"items"`
Count int `json:"count"`
}
type TemplateListItem struct {
Id string `json:"id"`
Name string `json:"name"`
Steps map[string]interface{} `json:"json"`
}
// Creates a new template instance which can be saved to transloadit.
func NewTemplate(name string) *Template {
return &Template{
Name: name,
Steps: make(map[string]map[string]interface{}),
}
}
// Save the template.
func (client *Client) CreateTemplate(template *Template) (string, error) {
content := map[string]interface{}{
"name": template.Name,
"template": map[string]interface{}{
"steps": template.Steps,
},
}
res, err := client.request("POST", "templates", content, nil)
if err != nil {
return "", fmt.Errorf("unable to create template: %s", err)
}
return res["template_id"].(string), nil
}
// Get information about a template using its id.
func (client *Client) GetTemplate(templateId string) (*Template, error) {
var templateGet templateGetResponse
_, err := client.request("GET", "templates/"+templateId, nil, &templateGet)
if err != nil {
return nil, fmt.Errorf("unable to get template: %s", err)
}
var template Template
template.Name = templateGet.Name
template.Steps = templateGet.Content.Steps
return &template, nil
}
// Add another step to the template.
func (template *Template) AddStep(name string, step map[string]interface{}) {
template.Steps[name] = step
}
// Delete a template from the list.
func (client *Client) DeleteTemplate(templateId string) error {
_, err := client.request("DELETE", "templates/"+templateId, nil, nil)
if err != nil {
return fmt.Errorf("unable to delete template: %s", err)
}
return nil
}
// Update the name and content of the template defined using the id.
func (client *Client) EditTemplate(templateId string, newTemplate *Template) error {
// Create signature
content := map[string]interface{}{
"name": newTemplate.Name,
"template": map[string]interface{}{
"steps": newTemplate.Steps,
},
}
_, err := client.request("PUT", "templates/"+templateId, content, nil)
return err
}
// List all templates matching the criterias.
func (client *Client) ListTemplates(options *ListOptions) (*TemplateList, error) {
var templates TemplateList
_, err := client.listRequest("templates", options, &templates)
return &templates, err
}