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
/
channels.go
178 lines (149 loc) · 4.87 KB
/
channels.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
package pi
import (
"encoding/json"
"fmt"
"net/http"
)
type channelsCommand struct {
Post createChannelCommand `description:"create Channel" command:"create" subcommands-optional:"true"`
Update updateChannelCommand `description:"update Channel Definition" command:"update" subcommands-optional:"true"`
Get getChannelsCommand `description:"get Channel Definitions" command:"get" subcommands-optional:"true"`
Delete deleteChannelCommand `description:"delete Channel" command:"delete" subcommands-optional:"true"`
}
type createChannelCommand struct {
Username string `short:"u" long:"username" description:"User name of channel owner."`
ID string `short:"i" long:"channel-id" description:"ID for identifying the channel." required:"true"`
Name string `short:"n" long:"name" description:"The name of the channel." required:"true"`
Type string `short:"t" long:"type" description:"The type for notification." required:"true"`
Detail string `short:"d" long:"detail" description:"Object that specifies the details of the type. It is specified as JSON string." required:"true"`
}
type createChannelParam struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Detail json.RawMessage `json:"detail"`
}
type updateChannelCommand struct {
Username string `short:"u" long:"username" description:"User name of channel owner."`
ID string `short:"i" long:"channel-id" description:"ID for identifying the channel." required:"true"`
Name string `short:"n" long:"name" description:"The name of the channel."`
Type string `short:"t" long:"type" description:"The type for notification."`
Detail string `short:"d" long:"detail" description:"Object that specifies the details of the type. It is specified as JSON string."`
}
type updateChannelParam struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Detail json.RawMessage `json:"detail,omitempty"`
}
type getChannelsCommand struct {
Username string `short:"u" long:"username" description:"User name of channel owner."`
}
type deleteChannelCommand struct {
Username string `short:"u" long:"username" description:"User name of channel owner."`
ID string `short:"i" long:"channel-id" description:"ID for identifying the channel." required:"true"`
}
func (cC *createChannelCommand) Execute(args []string) error {
req, err := generateCreateChannelRequest(cC)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateCreateChannelRequest(cC *createChannelCommand) (*http.Request, error) {
username, err := getUsername(cC.Username)
if err != nil {
return nil, err
}
paramStruct := &createChannelParam{
ID: cC.ID,
Name: cC.Name,
Type: cC.Type,
Detail: json.RawMessage(cC.Detail),
}
req, err := generateRequestWithToken(
"POST",
fmt.Sprintf("v1/users/%s/channels", username),
paramStruct,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate create api request : %s", err)
}
return req, nil
}
func (uC *updateChannelCommand) Execute(args []string) error {
req, err := generateUpdateChannelRequest(uC)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateUpdateChannelRequest(uC *updateChannelCommand) (*http.Request, error) {
username, err := getUsername(uC.Username)
if err != nil {
return nil, err
}
paramStruct := &updateChannelParam{
ID: uC.ID,
Name: uC.Name,
Type: uC.Type,
Detail: json.RawMessage(uC.Detail),
}
req, err := generateRequestWithToken(
"PUT",
fmt.Sprintf("v1/users/%s/channels/%s", username, uC.ID),
paramStruct,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate update api request : %s", err)
}
return req, nil
}
func (gC *getChannelsCommand) Execute(args []string) error {
req, err := generateGetChannelsRequest(gC)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateGetChannelsRequest(gC *getChannelsCommand) (*http.Request, error) {
username, err := getUsername(gC.Username)
if err != nil {
return nil, err
}
req, err := generateRequestWithToken(
"GET",
fmt.Sprintf("v1/users/%s/channels", username),
nil,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate get api request : %s", err)
}
return req, nil
}
func (dC *deleteChannelCommand) Execute(args []string) error {
req, err := generateDeleteChannelRequest(dC)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateDeleteChannelRequest(dC *deleteChannelCommand) (*http.Request, error) {
username, err := getUsername(dC.Username)
if err != nil {
return nil, err
}
req, err := generateRequestWithToken(
"DELETE",
fmt.Sprintf("v1/users/%s/channels/%s", username, dC.ID),
nil,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate delete api request : %s", err)
}
return req, nil
}