forked from TimmyGuy/bigcommerce-api-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoupons.go
173 lines (165 loc) · 4.14 KB
/
coupons.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
package bigcommerce
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
)
type Coupon struct {
ID int64 `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Amount string `json:"amount"`
MinPurchase string `json:"min_purchase"`
Expires string `json:"expires"`
Enabled bool `json:"enabled"`
Code string `json:"code"`
AppliesTo struct {
Entity string `json:"entity"`
Ids []int64 `json:"ids"`
} `json:"applies_to"`
NumUses int `json:"num_uses"`
MaxUses int `json:"max_uses"`
MaxUsesPerCustomer int `json:"max_uses_per_customer"`
RestrictedTo []interface{} `json:"restricted_to"`
ShippingMethods struct {
} `json:"shipping_methods"`
DateCreated string `json:"date_created"`
}
func (bc *Client) CreateCoupon(coupon Coupon) (*Coupon, error) {
var body []byte
body, _ = json.Marshal(coupon)
req := bc.getAPIRequest(http.MethodPost, "/v3/coupons", bytes.NewReader(body))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
b, err := processBody(res)
if err != nil {
return nil, err
}
var couponResponse struct {
Data Coupon `json:"data,omitempty"`
Meta struct {
} `json:"meta,omitempty"`
}
err = json.Unmarshal(b, &couponResponse)
if err != nil {
return nil, err
}
return &couponResponse.Data, nil
}
func (bc *Client) GetCoupon(couponID int64) (*Coupon, error) {
req := bc.getAPIRequest(http.MethodGet, "/v3/coupons/"+strconv.FormatInt(couponID, 10), nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
b, err := processBody(res)
if err != nil {
return nil, err
}
var couponResponse struct {
Data Coupon `json:"data,omitempty"`
Meta struct {
} `json:"meta,omitempty"`
}
err = json.Unmarshal(b, &couponResponse)
if err != nil {
return nil, err
}
return &couponResponse.Data, nil
}
func (bc *Client) UpdateCoupon(couponID int64, coupon Coupon) (*Coupon, error) {
var body []byte
body, _ = json.Marshal(coupon)
req := bc.getAPIRequest(http.MethodPut, "/v3/coupons/"+strconv.FormatInt(couponID, 10), bytes.NewReader(body))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
b, err := processBody(res)
if err != nil {
return nil, err
}
var couponResponse struct {
Data Coupon `json:"data,omitempty"`
Meta struct {
} `json:"meta,omitempty"`
}
err = json.Unmarshal(b, &couponResponse)
if err != nil {
return nil, err
}
return &couponResponse.Data, nil
}
func (bc *Client) DeleteCoupon(couponID int64) error {
req := bc.getAPIRequest(http.MethodDelete, "/v3/coupons/"+strconv.FormatInt(couponID, 10), nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return err
}
b, err := processBody(res)
if err != nil {
return err
}
var couponResponse struct {
Data Coupon `json:"data,omitempty"`
Meta struct {
} `json:"meta,omitempty"`
}
err = json.Unmarshal(b, &couponResponse)
if err != nil {
return err
}
return nil
}
func (bc *Client) GetAllCoupons(args map[string]string) ([]Coupon, error) {
cs := []Coupon{}
var csp []Coupon
page := 1
more := true
var err error
var retries int
for more {
csp, more, err = bc.GetCoupons(args, page)
if err != nil {
retries++
if retries > bc.MaxRetries {
return cs, fmt.Errorf("max retries reached")
}
break
}
cs = append(cs, csp...)
page++
}
return cs, err
}
func (bc *Client) GetCoupons(args map[string]string, page int) ([]Coupon, bool, error) {
fpart := ""
for k, v := range args {
fpart += "&" + k + "=" + v
}
url := "/v3/coupons?page=" + strconv.Itoa(page) + fpart
req := bc.getAPIRequest(http.MethodGet, url, nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, false, err
}
b, err := processBody(res)
if err != nil {
return nil, false, err
}
var couponResponse struct {
Data []Coupon `json:"data,omitempty"`
Meta struct {
Pagination Pagination `json:"pagination,omitempty"`
} `json:"meta,omitempty"`
}
err = json.Unmarshal(b, &couponResponse)
if err != nil {
return nil, false, err
}
return couponResponse.Data, couponResponse.Meta.Pagination.CurrentPage < couponResponse.Meta.Pagination.TotalPages, nil
}