forked from hanzoai/gochimp3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.go
More file actions
116 lines (89 loc) · 2.64 KB
/
templates.go
File metadata and controls
116 lines (89 loc) · 2.64 KB
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
package gochimp3
import (
"errors"
"fmt"
)
const (
templates_path = "/templates"
single_template_path = templates_path + "/%s"
)
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"`
}
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)
}