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
/
Copy pathwebhooks.go
154 lines (127 loc) · 3.92 KB
/
webhooks.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
package pi
import (
"fmt"
"net/http"
)
type webhooksCommand struct {
Create createWebhookCommand `description:"create a Webhook" command:"create" subcommands-optional:"true"`
Get getWebhooksCommand `description:"get registered Webhooks" command:"get" subcommands-optional:"true"`
Unvoke invokeWebhookCommand `description:"invoke Webhook" command:"invoke" subcommands-optional:"true"`
Delete deleteWebhookCommand `description:"delete a Webhook" command:"delete" subcommands-optional:"true"`
}
type createWebhookCommand struct {
Username string `short:"u" long:"username" description:"User name of graph owner."`
ID string `short:"g" long:"graph-id" description:"ID for identifying the pixelation graph." required:"true"`
Type string `short:"t" long:"type" description:"Specify the behavior when this Webhook is invoked." choice:"increment" choice:"decrement" required:"true"`
}
type createWebhookParam struct {
ID string `json:"graphID"`
Type string `json:"type"`
}
type getWebhooksCommand struct {
Username string `short:"u" long:"username" description:"User name of graph owner."`
}
type invokeWebhookCommand struct {
Username string `short:"u" long:"username" description:"User name of graph owner."`
WebhookHash string `short:"w" long:"webhookHash" description:"Specify webhookHash of registered webhook." required:"true"`
}
type deleteWebhookCommand struct {
Username string `short:"u" long:"username" description:"User name of graph owner."`
WebhookHash string `short:"w" long:"webhookHash" description:"Specify webhookHash of registered webhook." required:"true"`
}
func (cW *createWebhookCommand) Execute(args []string) error {
req, err := generateCreateWebhookRequest(cW)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateCreateWebhookRequest(cW *createWebhookCommand) (*http.Request, error) {
username, err := getUsername(cW.Username)
if err != nil {
return nil, err
}
paramStruct := &createWebhookParam{
ID: cW.ID,
Type: cW.Type,
}
req, err := generateRequestWithToken(
"POST",
fmt.Sprintf("v1/users/%s/webhooks", username),
paramStruct,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate create api request : %s", err)
}
return req, nil
}
func (gW *getWebhooksCommand) Execute(args []string) error {
req, err := generateGetWebhooksRequest(gW)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateGetWebhooksRequest(gW *getWebhooksCommand) (*http.Request, error) {
username, err := getUsername(gW.Username)
if err != nil {
return nil, err
}
req, err := generateRequestWithToken(
"GET",
fmt.Sprintf("v1/users/%s/webhooks", username),
nil,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate get api request : %s", err)
}
return req, nil
}
func (iW *invokeWebhookCommand) Execute(args []string) error {
req, err := generateInvokeWebhookRequest(iW)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateInvokeWebhookRequest(iW *invokeWebhookCommand) (*http.Request, error) {
username, err := getUsername(iW.Username)
if err != nil {
return nil, err
}
req, err := generateRequestWithToken(
"POST",
fmt.Sprintf("v1/users/%s/webhooks/%s", username, iW.WebhookHash),
nil,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate invoke api request : %s", err)
}
return req, nil
}
func (dW *deleteWebhookCommand) Execute(args []string) error {
req, err := generateDeleteWebhookRequest(dW)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateDeleteWebhookRequest(dW *deleteWebhookCommand) (*http.Request, error) {
username, err := getUsername(dW.Username)
if err != nil {
return nil, err
}
req, err := generateRequestWithToken(
"DELETE",
fmt.Sprintf("v1/users/%s/webhooks/%s", username, dW.WebhookHash),
nil,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate delete api request : %s", err)
}
return req, nil
}