forked from chiefy/go-statuspage-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.go
More file actions
169 lines (149 loc) · 3.97 KB
/
Copy pathcomponent.go
File metadata and controls
169 lines (149 loc) · 3.97 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
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
package statuspage
import (
"fmt"
"net/url"
"time"
)
type Component struct {
CreatedAt *time.Time `json:"created_at,omitempty"`
Description *string `json:"description,omitempty"`
ID *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
PageID *string `json:"page_id,omitempty"`
Position *int `json:"position,omitempty"`
Status *string `json:"status,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
GroupID *string `json:"group_id,omitempty"`
Showcase *bool `json:"showcase,omitempty"`
}
func (c *Component) String() string {
var out string
line := "-----------------"
out = fmt.Sprintf("\n%s\nCreated: %s\nName: %s\nID: %s\nStatus: %s\n%s\n",
line,
*c.CreatedAt,
*c.Name,
*c.ID,
*c.Status,
line,
)
return out
}
type ComponentUpdateData struct {
Data string
}
func (c *ComponentUpdateData) String() string {
return c.Data
}
type ComponentCreateData struct {
Name string
Description string
GroupID string
Showcase bool
}
func (c *ComponentCreateData) String() string {
return encodeParams(map[string]interface{}{
"component[name]": c.Name,
"component[description]": c.Description,
"component[group_id]": c.GroupID,
"component[showcase]": c.Showcase,
})
}
type ComponentsResponse []Component
func (c *Client) doGetComponents(path string) ([]Component, error) {
resp := ComponentsResponse{}
err := c.doGet(path, nil, &resp)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) GetAllComponents() ([]Component, error) {
return c.doGetComponents("components.json")
}
func (c *Client) GetComponentByID(id string) (*Component, error) {
cs, err := c.GetAllComponents()
if err != nil {
return nil, err
}
for _, cp := range cs {
if *cp.ID == id {
return &cp, nil
}
}
return nil, fmt.Errorf("search error: Component with ID %s not found", id)
}
func (c *Client) GetComponentByName(name string) (*Component, error) {
cs, err := c.GetAllComponents()
if err != nil {
return nil, err
}
for _, cp := range cs {
if *cp.Name == name {
return &cp, nil
}
}
return nil, fmt.Errorf("search error: Component with name %s not found", name)
}
func (c *Client) doUpdateComponent(comp *Component, params fmt.Stringer) (*Component, error) {
resp := Component{}
path := fmt.Sprintf("components/%s.json", *comp.ID)
err := c.doPatch(path, params, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (c *Client) updateComponent(comp *Component, attr string) (*Component, error) {
var newVal string
switch attr {
case "name":
newVal = *comp.Name
case "description":
newVal = *comp.Description
case "status":
newVal = *comp.Status
}
params := &ComponentUpdateData{
Data: fmt.Sprintf("component[%s]=%s", attr, url.QueryEscape(newVal)),
}
uc, err := c.doUpdateComponent(comp, params)
if err != nil {
return nil, err
}
return uc, nil
}
func (c *Client) UpdateComponentName(comp *Component) (*Component, error) {
return c.updateComponent(comp, "name")
}
func (c *Client) UpdateComponentStatus(comp *Component) (*Component, error) {
return c.updateComponent(comp, "status")
}
func (c *Client) UpdateComponentDesc(comp *Component) (*Component, error) {
return c.updateComponent(comp, "description")
}
func (c *Client) CreateComponent(name, description, groupID string, showcase bool) (*Component, error) {
newComp := &ComponentCreateData{
Name: name,
Description: description,
GroupID: groupID,
Showcase: showcase,
}
resp := Component{}
err := c.doPost("components.json", newComp, resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// DeleteComponent deletes a component.
// As per the API docs, this endpoint only returns 204 on successful deletion -
// it does not return data.
func (c *Client) DeleteComponent(comp *Component) (error) {
path := "components/" + *comp.ID + ".json"
err := c.doDelete(path, nil, nil)
if err != nil {
return err
}
return nil
}