-
Notifications
You must be signed in to change notification settings - Fork 11
/
webhook.go
161 lines (148 loc) · 4.21 KB
/
webhook.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
package bigcommerce
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strconv"
"strings"
)
type WebhookPayload struct {
Scope string `json:"scope"`
StoreID string `json:"store_id"`
Data struct {
Type string `json:"type"`
ID int64 `json:"id"`
CouponID string `json:"couponId"`
CartID string `json:"cartId"`
OrderID int64 `json:"orderId"`
Address struct {
CustomerID int64 `json:"customer_id"`
} `json:"address"`
Inventory InventoryEntry `json:"inventory"`
Message struct {
OrderMessageID int64 `json:"order_message_id"`
} `json:"message"`
Sku struct {
ProductID int64 `json:"product_id"`
VariantID int64 `json:"variant_id"`
} `json:"sku"`
Status struct {
PreviousStatusID int64 `json:"previous_status_id"`
NewStatusID int64 `json:"new_status_id"`
} `json:"status"`
} `json:"data"`
Hash string `json:"hash"`
CreatedAt int64 `json:"created_at"`
Producer string `json:"producer"`
}
type Webhook struct {
ID int64 `json:"id"`
ClientID string `json:"client_id"`
StoreHash string `json:"store_hash"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
Scope string `json:"scope"`
Destination string `json:"destination"`
IsActive bool `json:"is_active"`
Headers map[string]string `json:"headers"`
}
// GetWebhookPayload returns a WebhookPayload object and the raw payload from the BigCommerce API
// Arguments: r - the http.Request object
// Returns:
// *WebhookPayload - the WebhookPayload object
// []byte - the raw payload from the BigCommerce API
// error - the error, if any
func GetWebhookPayload(r *http.Request) (*WebhookPayload, []byte, error) {
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, nil, err
}
r.Body.Close()
var payload WebhookPayload
err = json.Unmarshal(bytes, &payload)
if err != nil {
return nil, bytes, err
}
return &payload, bytes, nil
}
func (bc *Client) GetWebhooks() ([]Webhook, error) {
url := "/v3/hooks?limit=250"
req := bc.getAPIRequest(http.MethodGet, url, nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := processBody(res)
if err != nil {
return nil, err
}
var webhooksResponse struct {
Data []Webhook `json:"data"`
Meta struct {
Pagination Pagination `json:"pagination"`
} `json:"meta"`
}
err = json.Unmarshal(body, &webhooksResponse)
if err != nil {
return nil, err
}
return webhooksResponse.Data, nil
}
// CreateWebhook creates a new webhook or activates it if it already exists but inactive
func (bc *Client) CreateWebhook(scope, destination string, headers map[string]string) (int64, error) {
url := "/v3/hooks"
webhooks, err := bc.GetWebhooks()
if err != nil {
return 0, err
}
for _, webhook := range webhooks {
if webhook.Scope == scope && webhook.Destination == destination && reflect.DeepEqual(headers, headers) {
if webhook.IsActive {
return webhook.ID, nil
}
req := bc.getAPIRequest(http.MethodPut, url+"/"+strconv.FormatInt(webhook.ID, 10), strings.NewReader(`{"is_active": true}`))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return 0, err
}
defer res.Body.Close()
body, err := processBody(res)
if err != nil {
return 0, fmt.Errorf("error processing response body: %v %s", err, string(body))
}
return webhook.ID, nil
}
}
payload := struct {
Scope string `json:"scope"`
Destination string `json:"destination"`
Headers map[string]string `json:"headers,omitempty"`
}{
Scope: scope,
Destination: destination,
}
if headers != nil {
payload.Headers = headers
}
reqJSON, _ := json.Marshal(payload)
req := bc.getAPIRequest(http.MethodPost, url, bytes.NewReader(reqJSON))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return 0, err
}
defer res.Body.Close()
body, err := processBody(res)
if err != nil {
return 0, fmt.Errorf("error processing response body: %v %s (%s)", err, string(body), string(reqJSON))
}
var respWebhook Webhook
err = json.Unmarshal(body, &respWebhook)
if err != nil {
return 0, err
}
return respWebhook.ID, nil
}