-
Notifications
You must be signed in to change notification settings - Fork 10
/
woo.go
333 lines (315 loc) · 9.92 KB
/
woo.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Package woocommerce is a Woo Commerce lib.
//
// Quick start:
// b, err := os.ReadFile("./config/config_test.json")
// if err != nil {
// panic(fmt.Sprintf("Read config error: %s", err.Error()))
// }
// var c config.Config
// err = jsoniter.Unmarshal(b, &c)
// if err != nil {
// panic(fmt.Sprintf("Parse config file error: %s", err.Error()))
// }
//
// wooClient = NewClient(c)
// // Query an order
// order, err := wooClient.Services.Order.One(1)
// if err != nil {
// fmt.Println(err)
// } else {
// fmt.Println(fmt.Sprintf("%#v", order))
// }
package woocommerce
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/hiscaler/gox/inx"
"github.com/hiscaler/gox/stringx"
"github.com/hiscaler/woocommerce-go/config"
jsoniter "github.com/json-iterator/go"
"github.com/json-iterator/go/extra"
"log"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"unsafe"
)
const (
Version = "1.0.3"
UserAgent = "WooCommerce API Client-Golang/" + Version
HashAlgorithm = "HMAC-SHA256"
)
// https://woocommerce.github.io/woocommerce-rest-api-docs/?php#request-response-format
const (
BadRequestError = 400 // 错误的请求
UnauthorizedError = 401 // 身份验证或权限错误
NotFoundError = 404 // 访问资源不存在
InternalServerError = 500 // 服务器内部错误
MethodNotImplementedError = 501 // 方法未实现
)
var ErrNotFound = errors.New("WooCommerce: not found")
func init() {
extra.RegisterFuzzyDecoders()
}
type WooCommerce struct {
Debug bool // Is debug mode
Logger *log.Logger // Log
Services services // WooCommerce API services
}
type service struct {
debug bool // Is debug mode
logger *log.Logger // Log
httpClient *resty.Client // HTTP client
}
type services struct {
Coupon couponService
Customer customerService
Order orderService
OrderNote orderNoteService
OrderRefund orderRefundService
Product productService
ProductVariation productVariationService
ProductAttribute productAttributeService
ProductAttributeTerm productAttributeTermService
ProductCategory productCategoryService
ProductShippingClass productShippingClassService
ProductTag productTagService
ProductReview productReviewService
Report reportService
TaxRate taxRateService
TaxClass taxClassService
Webhook webhookService
Setting settingService
SettingOption settingOptionService
PaymentGateway paymentGatewayService
ShippingZone shippingZoneService
ShippingZoneLocation shippingZoneLocationService
ShippingZoneMethod shippingZoneMethodService
ShippingMethod shippingMethodService
SystemStatus systemStatusService
SystemStatusTool systemStatusToolService
Data dataService
}
// OAuth signature
func oauthSignature(config config.Config, method, endpoint string, params url.Values) string {
sb := strings.Builder{}
sb.WriteString(config.ConsumerSecret)
if config.Version != "v1" && config.Version != "v2" {
sb.WriteByte('&')
}
consumerSecret := sb.String()
sb.Reset()
sb.WriteString(method)
sb.WriteByte('&')
sb.WriteString(url.QueryEscape(endpoint))
sb.WriteByte('&')
sb.WriteString(url.QueryEscape(params.Encode()))
mac := hmac.New(sha256.New, stringx.ToBytes(consumerSecret))
mac.Write(stringx.ToBytes(sb.String()))
signatureBytes := mac.Sum(nil)
return base64.StdEncoding.EncodeToString(signatureBytes)
}
// NewClient Creates a new WooCommerce client
//
// You must give a config with NewClient method params.
// After you can operate data use this client.
func NewClient(config config.Config) *WooCommerce {
logger := log.New(os.Stdout, "[ WooCommerce ] ", log.LstdFlags|log.Llongfile)
wooClient := &WooCommerce{
Debug: config.Debug,
Logger: logger,
}
// Add default value
if config.Version == "" {
config.Version = "v3"
} else {
config.Version = strings.ToLower(config.Version)
if !inx.StringIn(config.Version, "v1", "v2", "v3") {
config.Version = "v3"
}
}
if config.Timeout < 2 {
config.Timeout = 2
}
httpClient := resty.New().
SetDebug(config.Debug).
SetBaseURL(strings.TrimRight(config.URL, "/") + "/wp-json/wc/" + config.Version).
SetHeaders(map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
"User-Agent": UserAgent,
}).
SetAllowGetMethodPayload(true).
SetTimeout(config.Timeout * time.Second).
SetTransport(&http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !config.VerifySSL},
DialContext: (&net.Dialer{
Timeout: config.Timeout * time.Second,
}).DialContext,
}).
OnBeforeRequest(func(client *resty.Client, request *resty.Request) error {
params := url.Values{}
for k, vs := range request.QueryParam {
var v string
switch len(vs) {
case 0:
continue
case 1:
v = vs[0]
default:
// if is array params, must convert to string, example: status=1&status=2 replace to status=1,2
v = strings.Join(vs, ",")
}
params.Set(k, v)
}
if strings.HasPrefix(config.URL, "https") {
// basicAuth
if config.AddAuthenticationToURL {
params.Add("consumer_key", config.ConsumerKey)
params.Add("consumer_secret", config.ConsumerSecret)
} else {
// Set to header
client.SetAuthScheme("Basic").
SetAuthToken(fmt.Sprintf("%s %s", config.ConsumerKey, config.ConsumerSecret))
}
} else {
// oAuth
params.Add("oauth_consumer_key", config.ConsumerKey)
params.Add("oauth_timestamp", strconv.Itoa(int(time.Now().Unix())))
nonce := make([]byte, 16)
rand.Read(nonce)
sha1Nonce := fmt.Sprintf("%x", sha1.Sum(nonce))
params.Add("oauth_nonce", sha1Nonce)
params.Add("oauth_signature_method", HashAlgorithm)
params.Add("oauth_signature", oauthSignature(config, request.Method, client.BaseURL+request.URL, params))
}
request.QueryParam = params
return nil
}).
OnAfterResponse(func(client *resty.Client, response *resty.Response) (err error) {
if response.IsError() {
r := struct {
Code string `json:"code"`
Message string `json:"message"`
}{}
if err = jsoniter.Unmarshal(response.Body(), &r); err == nil {
err = ErrorWrap(response.StatusCode(), r.Message)
}
}
if err != nil {
logger.Printf("OnAfterResponse error: %s", err.Error())
}
return
})
if config.Debug {
httpClient.EnableTrace()
}
jsoniter.RegisterTypeDecoderFunc("float64", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
switch iter.WhatIsNext() {
case jsoniter.StringValue:
var t float64
v := strings.TrimSpace(iter.ReadString())
if v != "" {
var err error
if t, err = strconv.ParseFloat(v, 64); err != nil {
iter.Error = err
return
}
}
*((*float64)(ptr)) = t
default:
*((*float64)(ptr)) = iter.ReadFloat64()
}
})
httpClient.JSONMarshal = jsoniter.Marshal
httpClient.JSONUnmarshal = jsoniter.Unmarshal
xService := service{
debug: config.Debug,
logger: logger,
httpClient: httpClient,
}
wooClient.Services = services{
Coupon: (couponService)(xService),
Customer: (customerService)(xService),
Order: (orderService)(xService),
OrderNote: (orderNoteService)(xService),
OrderRefund: (orderRefundService)(xService),
Product: (productService)(xService),
ProductVariation: (productVariationService)(xService),
ProductAttribute: (productAttributeService)(xService),
ProductAttributeTerm: (productAttributeTermService)(xService),
ProductCategory: (productCategoryService)(xService),
ProductShippingClass: (productShippingClassService)(xService),
ProductTag: (productTagService)(xService),
ProductReview: (productReviewService)(xService),
Report: (reportService)(xService),
TaxRate: (taxRateService)(xService),
TaxClass: (taxClassService)(xService),
Webhook: (webhookService)(xService),
Setting: (settingService)(xService),
SettingOption: (settingOptionService)(xService),
PaymentGateway: (paymentGatewayService)(xService),
ShippingZone: (shippingZoneService)(xService),
ShippingZoneLocation: (shippingZoneLocationService)(xService),
ShippingZoneMethod: (shippingZoneMethodService)(xService),
ShippingMethod: (shippingMethodService)(xService),
SystemStatus: (systemStatusService)(xService),
SystemStatusTool: (systemStatusToolService)(xService),
Data: (dataService)(xService),
}
return wooClient
}
// Parse response header, get total and total pages, and check it is last page
func parseResponseTotal(currentPage int, resp *resty.Response) (total, totalPages int, isLastPage bool) {
if currentPage == 0 {
currentPage = 1
}
value := resp.Header().Get("X-Wp-Total")
if value != "" {
total, _ = strconv.Atoi(value)
}
value = resp.Header().Get("X-Wp-Totalpages")
if value != "" {
totalPages, _ = strconv.Atoi(value)
}
isLastPage = currentPage >= totalPages
return
}
// ErrorWrap wrap an error, if status code is 200, return nil, otherwise return an error
func ErrorWrap(code int, message string) error {
if code == http.StatusOK {
return nil
}
if code == NotFoundError {
return ErrNotFound
}
message = strings.TrimSpace(message)
if message == "" {
switch code {
case BadRequestError:
message = "Bad request"
case UnauthorizedError:
message = "Unauthorized operation, please confirm whether you have permission"
case NotFoundError:
message = "Resource not found"
case InternalServerError:
message = "Server internal error"
case MethodNotImplementedError:
message = "method not implemented"
default:
message = "Unknown error"
}
}
return fmt.Errorf("%d: %s", code, message)
}