/
templates.go
130 lines (108 loc) · 3.83 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 dnsimple
import (
"context"
"fmt"
)
// TemplatesService handles communication with the template related
// methods of the DNSimple API.
//
// See https://developer.dnsimple.com/v2/templates/
type TemplatesService struct {
client *Client
}
// Template represents a Template in DNSimple.
type Template struct {
ID int64 `json:"id,omitempty"`
SID string `json:"sid,omitempty"`
AccountID int64 `json:"account_id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
func templatePath(accountID string, templateIdentifier string) (path string) {
path = fmt.Sprintf("/%v/templates", accountID)
if templateIdentifier != "" {
path += fmt.Sprintf("/%v", templateIdentifier)
}
return
}
// TemplateResponse represents a response from an API method that returns a Template struct.
type TemplateResponse struct {
Response
Data *Template `json:"data"`
}
// TemplatesResponse represents a response from an API method that returns a collection of Template struct.
type TemplatesResponse struct {
Response
Data []Template `json:"data"`
}
// ListTemplates list the templates for an account.
//
// See https://developer.dnsimple.com/v2/templates/#list
func (s *TemplatesService) ListTemplates(ctx context.Context, accountID string, options *ListOptions) (*TemplatesResponse, error) {
path := versioned(templatePath(accountID, ""))
templatesResponse := &TemplatesResponse{}
path, err := addURLQueryOptions(path, options)
if err != nil {
return nil, err
}
resp, err := s.client.get(ctx, path, templatesResponse)
if err != nil {
return templatesResponse, err
}
templatesResponse.HTTPResponse = resp
return templatesResponse, nil
}
// CreateTemplate creates a new template.
//
// See https://developer.dnsimple.com/v2/templates/#create
func (s *TemplatesService) CreateTemplate(ctx context.Context, accountID string, templateAttributes Template) (*TemplateResponse, error) {
path := versioned(templatePath(accountID, ""))
templateResponse := &TemplateResponse{}
resp, err := s.client.post(ctx, path, templateAttributes, templateResponse)
if err != nil {
return nil, err
}
templateResponse.HTTPResponse = resp
return templateResponse, nil
}
// GetTemplate fetches a template.
//
// See https://developer.dnsimple.com/v2/templates/#get
func (s *TemplatesService) GetTemplate(ctx context.Context, accountID string, templateIdentifier string) (*TemplateResponse, error) {
path := versioned(templatePath(accountID, templateIdentifier))
templateResponse := &TemplateResponse{}
resp, err := s.client.get(ctx, path, templateResponse)
if err != nil {
return nil, err
}
templateResponse.HTTPResponse = resp
return templateResponse, nil
}
// UpdateTemplate updates a template.
//
// See https://developer.dnsimple.com/v2/templates/#update
func (s *TemplatesService) UpdateTemplate(ctx context.Context, accountID string, templateIdentifier string, templateAttributes Template) (*TemplateResponse, error) {
path := versioned(templatePath(accountID, templateIdentifier))
templateResponse := &TemplateResponse{}
resp, err := s.client.patch(ctx, path, templateAttributes, templateResponse)
if err != nil {
return nil, err
}
templateResponse.HTTPResponse = resp
return templateResponse, nil
}
// DeleteTemplate deletes a template.
//
// See https://developer.dnsimple.com/v2/templates/#delete
func (s *TemplatesService) DeleteTemplate(ctx context.Context, accountID string, templateIdentifier string) (*TemplateResponse, error) {
path := versioned(templatePath(accountID, templateIdentifier))
templateResponse := &TemplateResponse{}
resp, err := s.client.delete(ctx, path, nil, nil)
if err != nil {
return nil, err
}
templateResponse.HTTPResponse = resp
return templateResponse, nil
}