This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
notifications.go
192 lines (162 loc) · 6.33 KB
/
notifications.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package pi
import (
"fmt"
"net/http"
)
type notificationsCommand struct {
Post postNotificationCommand `description:"post Notification setting" command:"create" subcommands-optional:"true"`
Put putNotificationCommand `description:"update Notification setting" command:"update" subcommands-optional:"true"`
Get getNotificationsCommand `description:"get Notifications" command:"get" subcommands-optional:"true"`
Delete deleteNotificationCommand `description:"delete Notification" command:"delete" subcommands-optional:"true"`
}
type getNotificationsCommand struct {
Username string `short:"u" long:"username" description:"User name of owner."`
GraphID string `short:"g" long:"graph-id" description:"ID for identifying the graph." required:"true"`
}
type postNotificationCommand struct {
Username string `short:"u" long:"username" description:"User name of graph owner."`
GraphID string `short:"g" long:"graph-id" description:"ID for identifying the graph." required:"true"`
ID string `short:"i" long:"notifiation-id" description:"ID for identifying the notification setting." required:"true"`
Name string `short:"n" long:"name" description:"It is the name of the notification settings." required:"true"`
Target string `short:"t" long:"target" description:"Specify the target to be notified." required:"true"`
Condition string `short:"d" long:"condition" description:"Specify the condition used to judge whether to notify or not." required:"true"`
Threshold string `short:"s" long:"threshold" description:"Specify the threshold value for deciding whether to notify or not. The number must match the graph type(int or float)." required:"true"`
ChannelID string `short:"c" long:"channel-id" description:"Specify the ID of the channel to be notified." required:"true"`
}
type postNotificationParam struct {
ID string `json:"id"`
Name string `json:"name"`
Target string `json:"target"`
Condition string `json:"condition"`
Threshold string `json:"threshold"`
ChannelID string `json:"channelID"`
}
type putNotificationCommand struct {
Username string `short:"u" long:"username" description:"User name of graph owner."`
GraphID string `short:"g" long:"graph-id" description:"ID for identifying the graph." required:"true"`
ID string `short:"i" long:"notifiation-id" description:"ID for identifying the notification setting." required:"true"`
Name string `short:"n" long:"name" description:"It is the name of the notification settings."`
Target string `short:"t" long:"target" description:"Specify the target to be notified."`
Condition string `short:"d" long:"condition" description:"Specify the condition used to judge whether to notify or not."`
Threshold string `short:"s" long:"threshold" description:"Specify the threshold value for deciding whether to notify or not. The number must match the graph type(int or float)."`
ChannelID string `short:"c" long:"channel-id" description:"Specify the ID of the channel to be notified."`
}
type putNotificationParam struct {
Name string `json:"name,omitempty"`
Target string `json:"target,omitempty"`
Condition string `json:"condition,omitempty"`
Threshold string `json:"threshold,omitempty"`
ChannelID string `json:"channelID,omitempty"`
}
type deleteNotificationCommand struct {
Username string `short:"u" long:"username" description:"User name of owner."`
GraphID string `short:"g" long:"graph-id" description:"ID for identifying the graph." required:"true"`
ID string `short:"i" long:"notifiation-id" description:"ID for identifying the notification setting." required:"true"`
}
func (gN *getNotificationsCommand) Execute(args []string) error {
req, err := generateGetNotificationsRequest(gN)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateGetNotificationsRequest(gN *getNotificationsCommand) (*http.Request, error) {
username, err := getUsername(gN.Username)
if err != nil {
return nil, err
}
req, err := generateRequestWithToken(
"GET",
fmt.Sprintf("v1/users/%s/graphs/%s/notifications", username, gN.GraphID),
nil,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate get api request : %s", err)
}
return req, nil
}
func (pN *postNotificationCommand) Execute(args []string) error {
req, err := generatePostNotificationRequest(pN)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generatePostNotificationRequest(pN *postNotificationCommand) (*http.Request, error) {
username, err := getUsername(pN.Username)
if err != nil {
return nil, err
}
paramStruct := &postNotificationParam{
ID: pN.ID,
Name: pN.Name,
Target: pN.Target,
Condition: pN.Condition,
Threshold: pN.Threshold,
ChannelID: pN.ChannelID,
}
req, err := generateRequestWithToken(
"POST",
fmt.Sprintf("v1/users/%s/graphs/%s/notifications", username, pN.GraphID),
paramStruct,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate create api request : %s", err)
}
return req, nil
}
func (pN *putNotificationCommand) Execute(args []string) error {
req, err := generatePutNotificationRequest(pN)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generatePutNotificationRequest(pN *putNotificationCommand) (*http.Request, error) {
username, err := getUsername(pN.Username)
if err != nil {
return nil, err
}
paramStruct := &putNotificationParam{
Name: pN.Name,
Target: pN.Target,
Condition: pN.Condition,
Threshold: pN.Threshold,
ChannelID: pN.ChannelID,
}
req, err := generateRequestWithToken(
"PUT",
fmt.Sprintf("v1/users/%s/graphs/%s/notifications/%s", username, pN.GraphID, pN.ID),
paramStruct,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate create api request : %s", err)
}
return req, nil
}
func (dN *deleteNotificationCommand) Execute(args []string) error {
req, err := generateDeleteNotificationRequest(dN)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateDeleteNotificationRequest(dN *deleteNotificationCommand) (*http.Request, error) {
username, err := getUsername(dN.Username)
if err != nil {
return nil, err
}
req, err := generateRequestWithToken(
"DELETE",
fmt.Sprintf("v1/users/%s/graphs/%s/notifications/%s", username, dN.GraphID, dN.ID),
nil,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate get api request : %s", err)
}
return req, nil
}