-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWooCommerceAPI.go
More file actions
135 lines (108 loc) · 3.51 KB
/
Copy pathWooCommerceAPI.go
File metadata and controls
135 lines (108 loc) · 3.51 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
retryablehttp "github.com/hashicorp/go-retryablehttp"
)
type WooCommerceAPI struct {
url string
auth string
httpClient *retryablehttp.Client
}
func NewWooCommerceAPI(url, consumerKey, consumerSecret string, timeout time.Duration, maxRetries int) *WooCommerceAPI {
client := retryablehttp.NewClient()
client.HTTPClient.Timeout = timeout
client.RetryMax = maxRetries
return &WooCommerceAPI{
url: url,
auth: fmt.Sprintf("%s:%s", consumerKey, consumerSecret),
httpClient: client,
}
}
func (api *WooCommerceAPI) request(method, endpoint string, data interface{}, params map[string]string) ([]byte, error) {
url := fmt.Sprintf("%s/wp-json/wc/v3/%s", api.url, endpoint)
var reqBody []byte
var err error
if data != nil {
reqBody, err = json.Marshal(data)
if err != nil {
return nil, err
}
}
req, err := retryablehttp.NewRequest(method, url, bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
req.SetBasicAuth(api.auth, "")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
q := req.URL.Query()
for key, value := range params {
q.Add(key, value)
}
req.URL.RawQuery = q.Encode()
resp, err := api.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("API request failed: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("API request failed with status code %d: %s", resp.StatusCode, string(body))
}
return body, nil
}
func (api *WooCommerceAPI) Get(endpoint string, params map[string]string) ([]byte, error) {
return api.request("GET", endpoint, nil, params)
}
func (api *WooCommerceAPI) Post(endpoint string, data interface{}) ([]byte, error) {
return api.request("POST", endpoint, data, nil)
}
func (api *WooCommerceAPI) Put(endpoint string, data interface{}) ([]byte, error) {
return api.request("PUT", endpoint, data, nil)
}
func (api *WooCommerceAPI) Delete(endpoint string) ([]byte, error) {
return api.request("DELETE", endpoint, nil, nil)
}
func (api *WooCommerceAPI) GetAllProducts(perPage int) ([]map[string]interface{}, error) {
var products []map[string]interface{}
page := 1
for {
params := map[string]string{
"per_page": fmt.Sprintf("%d", perPage),
"page": fmt.Sprintf("%d", page),
}
body, err := api.Get("products", params)
if err != nil {
return nil, err
}
var pageProducts []map[string]interface{}
if err := json.Unmarshal(body, &pageProducts); err != nil {
return nil, err
}
if len(pageProducts) == 0 {
break
}
products = append(products, pageProducts...)
page++
}
return products, nil
}
func main() {
api := NewWooCommerceAPI("https://example.com", "consumer_key", "consumer_secret", 10*time.Second, 3)
products, err := api.GetAllProducts(100)
if err != nil {
log.Fatalf("Failed to get products: %v", err)
}
for _, product := range products {
fmt.Println(product)
}
}