Skip to content

Commit

Permalink
New: Added webhok service
Browse files Browse the repository at this point in the history
  • Loading branch information
hiscaler committed May 20, 2022
1 parent 7f2e090 commit b44397c
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ Methods:
- Create
- Delete

### Webhooks

Service Name: wooClient.Service.Webhook

Methods:

- All
- Batch
- Create
- Delete
- One
- Update

### Settings

- Groups
Expand Down
174 changes: 174 additions & 0 deletions webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package woocommerce

import (
"errors"
"fmt"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/google/go-querystring/query"
"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 nil
}

// All List all webhooks
func (s webhookService) All(params WebhooksQueryParams) (items []entity.Webhook, isLastPage bool, err error) {
if err = params.Validate(); err != nil {
return
}

params.TidyVars()
urlValues, _ := query.Values(params)
resp, err := s.httpClient.R().SetQueryParamsFromValues(urlValues).Get("/products/webhooks")
if err != nil {
return
}

if resp.IsSuccess() {
if err = jsoniter.Unmarshal(resp.Body(), &items); err == nil {
isLastPage = len(items) < params.PerPage
}
}
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
}
2 changes: 2 additions & 0 deletions woo.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type services struct {
Report reportService
TaxRate taxRateService
TaxClass taxClassService
Webhook webhookService
Setting settingService
SettingOption settingOptionService
PaymentGateway paymentGatewayService
Expand Down Expand Up @@ -216,6 +217,7 @@ func NewClient(config config.Config) *WooCommerce {
Report: (reportService)(xService),
TaxRate: (taxRateService)(xService),
TaxClass: (taxClassService)(xService),
Webhook: (webhookService)(xService),
Setting: (settingService)(xService),
SettingOption: (settingOptionService)(xService),
PaymentGateway: (paymentGatewayService)(xService),
Expand Down

0 comments on commit b44397c

Please sign in to comment.