-
Notifications
You must be signed in to change notification settings - Fork 12
/
webhook.go
182 lines (152 loc) · 4.65 KB
/
webhook.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
package woocommerce
import (
"errors"
"fmt"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/hiscaler/woocommerce-go/entity"
jsoniter "github.com/json-iterator/go"
)
type webhookService service
type WebhooksQueryParams struct {
queryParams
Search string `url:"search"`
After string `url:"after"`
Before string `url:"before"`
Exclude []int `url:"exclude"`
Include []int `url:"include"`
Status string `url:"status"`
}
func (m WebhooksQueryParams) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.Before, validation.When(m.Before != "", validation.By(func(value interface{}) error {
dateStr, _ := value.(string)
return IsValidateTime(dateStr)
}))),
validation.Field(&m.After, validation.When(m.After != "", validation.By(func(value interface{}) error {
dateStr, _ := value.(string)
return IsValidateTime(dateStr)
}))),
)
}
// All List all webhooks
func (s webhookService) All(params WebhooksQueryParams) (items []entity.Webhook, total, totalPages int, isLastPage bool, err error) {
if err = params.Validate(); err != nil {
return
}
params.TidyVars()
params.After = ToISOTimeString(params.After, false, true)
params.Before = ToISOTimeString(params.Before, true, false)
resp, err := s.httpClient.R().SetQueryParamsFromValues(toValues(params)).Get("/products/webhooks")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
total, totalPages, isLastPage = parseResponseTotal(params.Page, resp)
}
return
}
// One Retrieve a webhook
func (s webhookService) One(id int) (item entity.Webhook, err error) {
resp, err := s.httpClient.R().Get(fmt.Sprintf("/products/webhooks/%d", id))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
// Create
type CreateWebhookRequest struct {
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
Topic string `json:"topic,omitempty"`
DeliveryURL string `json:"delivery_url,omitempty"`
Secret string `json:"secret,omitempty"`
}
func (m CreateWebhookRequest) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.DeliveryURL, validation.When(m.DeliveryURL != "", is.URL.Error("投递 URL 格式错误"))),
validation.Field(&m.Status, validation.When(m.Status != "", validation.In("active", "paused", "disabled").Error("无效的状态值"))),
)
}
// Create Create a product attribute
func (s webhookService) Create(req CreateWebhookRequest) (item entity.Webhook, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().SetBody(req).Post("/products/webhooks")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
type UpdateWebhookRequest = CreateWebhookRequest
// Update Update a webhook
func (s webhookService) Update(id int, req UpdateWebhookRequest) (item entity.Webhook, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().SetBody(req).Put(fmt.Sprintf("/products/webhooks/%d", id))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
// Delete a webhook
func (s webhookService) Delete(id int, force bool) (item entity.Webhook, err error) {
resp, err := s.httpClient.R().
SetBody(map[string]bool{"force": force}).
Delete(fmt.Sprintf("/products/webhooks/%d", id))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
// Batch update webhooks
type BatchWebhooksCreateItem = CreateWebhookRequest
type BatchWebhooksUpdateItem struct {
ID string `json:"id"`
BatchWebhooksCreateItem
}
type BatchWebhooksRequest struct {
Create []BatchWebhooksCreateItem `json:"create,omitempty"`
Update []BatchWebhooksUpdateItem `json:"update,omitempty"`
Delete []int `json:"delete,omitempty"`
}
func (m BatchWebhooksRequest) Validate() error {
if len(m.Create) == 0 && len(m.Update) == 0 && len(m.Delete) == 0 {
return errors.New("无效的请求数据")
}
return nil
}
type BatchWebhooksResult struct {
Create []entity.Webhook `json:"create"`
Update []entity.Webhook `json:"update"`
Delete []entity.Webhook `json:"delete"`
}
// Batch Batch create/update/delete webhooks
func (s webhookService) Batch(req BatchWebhooksRequest) (res BatchWebhooksResult, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().SetBody(req).Post("/products/webhooks/batch")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &res)
}
return
}