forked from ewarehousing-solutions/bigcommerce-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannels.go
84 lines (78 loc) · 1.92 KB
/
channels.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
package bigcommerce
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
)
type Channel struct {
IconURL string `json:"icon_url"`
IsListableFromUI bool `json:"is_listable_from_ui"`
IsVisible bool `json:"is_visible"`
DateCreated time.Time `json:"date_created"`
ExternalID string `json:"external_id"`
Type string `json:"type"`
Platform string `json:"platform"`
IsEnabled bool `json:"is_enabled"`
DateModified time.Time `json:"date_modified"`
Name string `json:"name"`
ID int `json:"id"`
Status string `json:"status"`
}
func (bc *Client) GetAllChannels() ([]Channel, error) {
cs := []Channel{}
var csp []Channel
page := 1
more := true
var err error
retries := 0
for more {
csp, more, err = bc.GetChannels(page)
if err != nil {
retries++
if retries > bc.MaxRetries {
log.Println("Max retries reached")
return cs, err
}
break
}
cs = append(cs, csp...)
page++
}
return cs, err
}
func (bc *Client) GetChannels(page int) ([]Channel, bool, error) {
url := "/v3/channels?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()
if res.StatusCode == http.StatusNoContent {
return nil, false, ErrNoContent
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, false, err
}
var pp struct {
Status int `json:"status"`
Title string `json:"title"`
Data []Channel `json:"data"`
Meta struct {
Pagination Pagination `json:"pagination"`
} `json:"meta"`
}
err = json.Unmarshal(body, &pp)
if err != nil {
return nil, false, err
}
if pp.Status != 0 {
return nil, false, errors.New(pp.Title)
}
return pp.Data, pp.Meta.Pagination.CurrentPage < pp.Meta.Pagination.TotalPages, nil
}