-
Notifications
You must be signed in to change notification settings - Fork 79
/
templates.go
132 lines (101 loc) Β· 3.13 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
131
132
package gochimp3
import (
"errors"
"fmt"
)
const (
templates_path = "/templates"
single_template_path = templates_path + "/%s"
template_default_path = single_template_path + "/default-content"
)
type TemplateQueryParams struct {
ExtendedQueryParams
CreatedBy string
SinceCreatedAt string
BeforeCreatedAt string
Type string
FolderId string
}
func (q *TemplateQueryParams) Params() map[string]string {
m := q.ExtendedQueryParams.Params()
m["created_by"] = q.CreatedBy
m["since_created_at"] = q.SinceCreatedAt
m["before_created_at"] = q.BeforeCreatedAt
m["type"] = q.Type
m["folder_id"] = q.FolderId
return m
}
type ListOfTemplates struct {
baseList
Templates []TemplateResponse `json:"templates"`
}
type TemplateResponse struct {
withLinks
ID uint `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
DragAndDrop bool `json:"drag_and_drop"`
Responsive bool `json:"responsive"`
Category string `json:"category"`
DateCreated string `json:"date_created"`
CreatedBy string `json:"created_by"`
Active bool `json:"activer"`
FolderId string `json:"folder_id"`
Thumbnail string `json:"thumbnail"`
ShareUrl string `json:"share_url"`
api *API
}
type TemplateCreationRequest struct {
Name string `json:"name"`
Html string `json:"html"`
FolderId string `json:"folder_id"`
}
type TemplateDefaultContentResponse struct {
withLinks
Sections map[string]string `json:"sections"`
api *API
}
func (template *TemplateResponse) CanMakeRequest() error {
if template.ID == 0 {
return errors.New("No ID provided on template")
}
return nil
}
func (api *API) GetTemplates(params *TemplateQueryParams) (*ListOfTemplates, error) {
response := new(ListOfTemplates)
err := api.Request("GET", templates_path, params, nil, response)
if err != nil {
return nil, err
}
for _, l := range response.Templates {
l.api = api
}
return response, nil
}
func (api *API) GetTemplate(id string, params *BasicQueryParams) (*TemplateResponse, error) {
endpoint := fmt.Sprintf(single_template_path, id)
response := new(TemplateResponse)
response.api = api
return response, api.Request("GET", endpoint, params, nil, response)
}
func (api *API) CreateTemplate(body *TemplateCreationRequest) (*TemplateResponse, error) {
response := new(TemplateResponse)
response.api = api
return response, api.Request("POST", templates_path, nil, body, response)
}
func (api *API) UpdateTemplate(id string, body *TemplateCreationRequest) (*TemplateResponse, error) {
endpoint := fmt.Sprintf(single_template_path, id)
response := new(TemplateResponse)
response.api = api
return response, api.Request("PATCH", endpoint, nil, body, response)
}
func (api *API) DeleteTemplate(id string) (bool, error) {
endpoint := fmt.Sprintf(single_template_path, id)
return api.RequestOk("DELETE", endpoint)
}
func (api *API) GetTemplateDefaultContent(id string, params *BasicQueryParams) (*TemplateDefaultContentResponse, error) {
endpoint := fmt.Sprintf(template_default_path, id)
response := new(TemplateDefaultContentResponse)
response.api = api
return response, api.Request("GET", endpoint, params, nil, response)
}