-
Notifications
You must be signed in to change notification settings - Fork 0
/
wooRequest.go
101 lines (87 loc) · 2.6 KB
/
wooRequest.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
package gowoocommerce
import (
"encoding/json"
"errors"
"fmt"
)
// WooRequest is implemented for Batch/Post and Get
type WooRequest interface {
Send(w *WooConnection) ([]byte, error)
}
// WooPostRequest can be used for synchronous requests to the products, attributes, or categories endpoint
type WooPostRequest struct {
Endpoint string
Payload WooItem
}
// Send implements the WooRequest interface
func (p WooPostRequest) Send(w *WooConnection) ([]byte, error) {
var err error
if w.initialized == false {
return nil, fmt.Errorf("Please initialize with your credentials first. WooConnection.Init()")
}
body, err := json.Marshal(p.Payload)
if err != nil {
fmt.Println(err)
return nil, err
}
for i := 0; i < w.maxRetries; i++ {
resp, err := w.Request("POST", p.Endpoint, body)
if err == nil {
return resp, nil
}
fmt.Println(err)
}
return nil, fmt.Errorf("Error sending request - %v", err)
}
// WooBatchPostRequest sends a payload of batch creations, updates and/or deletions
type WooBatchPostRequest struct {
Endpoint string `json:"-"`
Create []WooItem `json:"create,omitempty"` // Create requests must not have IDs -the WC backend will generate them
Update []WooItem `json:"update,omitempty"` // Update requests must have IDs
Delete []int `json:"delete,omitempty"` // Delete requests can only be IDs
}
// Send implements the WooRequest Interface
func (b WooBatchPostRequest) Send(w *WooConnection) ([]byte, error) {
if w.initialized == false {
return nil, errors.New("Please initialize with your credentials first. WooConnection.Init()")
}
vars := struct {
url string
body []byte
err error
resp []byte
}{
url: w.credentials.domain + b.Endpoint,
}
vars.body, vars.err = json.Marshal(b)
if vars.err != nil {
return nil, vars.err
}
for i := 0; i < w.maxRetries; i++ {
vars.resp, vars.err = w.Request("POST", b.Endpoint, vars.body) //w.Post(b.Endpoint, vars.body)
if vars.err == nil {
return vars.resp, nil
}
fmt.Println(vars.err)
}
return nil, fmt.Errorf("Error sending request - %v", vars.err)
}
// WooGetRequest implements GET request via a WooConnection
type WooGetRequest struct {
Endpoint string
}
// Send implementes the WooRequest interface
func (g WooGetRequest) Send(w *WooConnection) ([]byte, error) {
if w.initialized == false {
return nil, errors.New("Please initialize with your credentials first. WooConnection.Init()")
}
var err error
for i := 0; i < w.maxRetries; i++ {
resp, err := w.Request("GET", g.Endpoint, nil)
if err == nil {
return resp, nil
}
fmt.Println(err)
}
return nil, fmt.Errorf("Error sending request - %v", err)
}