-
Notifications
You must be signed in to change notification settings - Fork 12
/
tax_class.go
73 lines (60 loc) · 1.54 KB
/
tax_class.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
package woocommerce
import (
"errors"
"fmt"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/hiscaler/woocommerce-go/entity"
jsoniter "github.com/json-iterator/go"
"strings"
)
type taxClassService service
// All List all tax classes
func (s taxClassService) All() (items []entity.TaxClass, err error) {
resp, err := s.httpClient.R().Get("/tax/classes")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}
// Create tax class request
type CreateTaxClassRequest struct {
Name string `json:"name"`
}
func (m CreateTaxClassRequest) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.Name, validation.Required.Error("名称不能为空")),
)
}
// Create Create a tax class
func (s taxClassService) Create(req CreateTaxClassRequest) (item entity.TaxClass, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().SetBody(req).Post("/taxes/classes")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
// Delete Delete a tax classes
func (s taxClassService) Delete(slug string, force bool) (item entity.TaxClass, err error) {
slug = strings.TrimSpace(slug)
if slug == "" {
err = errors.New("slug 参数不能为空")
return
}
resp, err := s.httpClient.R().SetBody(map[string]bool{"force": force}).Delete(fmt.Sprintf("/taxes/classes/%s", slug))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}