forked from ewarehousing-solutions/bigcommerce-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.go
173 lines (163 loc) · 5.02 KB
/
address.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"
"log"
"net/http"
"strconv"
)
// Address is for Customer Address endpoint
type Address struct {
ID int64 `json:"id,omitempty"`
CustomerID int64 `json:"customer_id,omitempty"`
Address1 string `json:"address1"`
Address2 string `json:"address2,omitempty"`
AddressType string `json:"address_type,omitempty"`
City string `json:"city"`
Company string `json:"company,omitempty"`
Country string `json:"country,omitempty"`
CountryCode string `json:"country_code"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Phone string `json:"phone,omitempty"`
PostalCode string `json:"postal_code,omitempty"`
StateOrProvince string `json:"state_or_province,omitempty"`
FormFields []FormField `json:"form_fields,omitempty"`
}
// GetAddresses returns all addresses for a curstomer, handling pagination
// customerID is bigcommerce customer id
func (bc *Client) GetAddresses(customerID int64) ([]Address, error) {
cs := []Address{}
var csp []Address
page := 1
more := true
var err error
var retries int
for more {
csp, more, err = bc.GetAddressPage(customerID, 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
}
// GetAddressPage returns all addresses for a curstomer, handling pagination
// customerID is bigcommerce customer id
// page: the page number to download
func (bc *Client) GetAddressPage(customerID int64, page int) ([]Address, bool, error) {
url := "/v3/customers/addresses?customer_id:in=" + strconv.FormatInt(customerID, 10) + "&page=" + strconv.Itoa(page)
req := bc.getAPIRequest(http.MethodGet, url, nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, false, err
}
defer res.Body.Close()
body, err := processBody(res)
if err != nil {
return nil, false, err
}
var pp struct {
Data []Address `json:"data"`
Meta struct {
Pagination Pagination `json:"pagination"`
} `json:"meta"`
}
err = json.Unmarshal(body, &pp)
if err != nil {
return nil, false, err
}
return pp.Data, pp.Meta.Pagination.CurrentPage < pp.Meta.Pagination.TotalPages, nil
}
// CreateAddress creates a new address for a customer from given data, ignoring ID (duplicating address)
func (bc *Client) CreateAddress(customerID int64, address *Address) (*Address, error) {
url := "/v3/customers/addresses"
// extra safety feature so we don't edit other customers' address
address.CustomerID = customerID
addressJSON, _ := json.Marshal([]Address{*address})
// log.Printf("addressJSON: %s", string(addressJSON))
req := bc.getAPIRequest(http.MethodPost, url, bytes.NewReader(addressJSON))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
var addr struct {
Addresses []Address `json:"data"`
}
defer res.Body.Close()
body, err := processBody(res)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &addr)
if err != nil {
return nil, fmt.Errorf("error parsing body: %s %s", err, string(body))
}
if len(addr.Addresses) == 0 {
return nil, fmt.Errorf("no address returned, %s", string(body))
}
return &addr.Addresses[0], nil
}
// UpdateAddress updates an existing address, address ID is required
func (bc *Client) UpdateAddress(customerID int64, address *Address) (*Address, error) {
url := "/v3/customers/addresses"
// extra safety feature so we don't edit other customers' address
address.CustomerID = customerID
if address.ID == 0 {
return nil, fmt.Errorf("address ID is required")
}
addressJSON, _ := json.Marshal([]Address{*address})
// log.Printf("addressJSON: %s", string(addressJSON))
req := bc.getAPIRequest(http.MethodPut, url, bytes.NewReader(addressJSON))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
var addr struct {
Addresses []Address `json:"data"`
}
defer res.Body.Close()
body, err := processBody(res)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &addr)
if err != nil {
return nil, fmt.Errorf("error parsing body: %s %s", err, string(body))
}
if len(addr.Addresses) == 0 {
return nil, fmt.Errorf("no address returned, %s", string(body))
}
return &addr.Addresses[0], nil
}
// DeleteAddress deletes an existing address, address ID is required
func (bc *Client) DeleteAddress(customerID, addressID int64) error {
url := "/v3/customers/addresses?id:in=" + strconv.FormatInt(addressID, 10)
req := bc.getAPIRequest(http.MethodDelete, url, nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode == http.StatusNoContent {
return nil
}
body, err := processBody(res)
if err != nil {
log.Printf("error processing body: %s", err)
return err
}
var addr Address
err = json.Unmarshal(body, &addr)
if err != nil {
log.Printf("error parsing body: %s %s", err, string(body))
return err
}
return nil
}