forked from TimmyGuy/bigcommerce-api-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposts.go
77 lines (70 loc) · 1.98 KB
/
posts.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
package bigcommerce
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
)
// Post is a BC blog post
type Post struct {
ID int64 `json:"id"`
Title string `json:"title"`
URL string `json:"url"`
PreviewURL string `json:"preview_url"`
Body string `json:"body"`
Tags []string `json:"tags"`
Summary string `json:"summary"`
IsPublished bool `json:"is_published"`
PublishedDate interface{} `json:"publisheddate"`
PublishedDateISO8601 string `json:"publisheddate_iso8601"`
MetaDescription string `json:"meta_description"`
MetaKeywords string `json:"meta_keywords"`
Author string `json:"author"`
ThumbnailPath string `json:"thumbnail_path"`
}
// GetAllPosts downloads all posts from BigCommerce, handling pagination
func (bc *Client) GetAllPosts() ([]Post, error) {
cs := []Post{}
var csp []Post
page := 1
more := true
var err error
retries := 0
for more {
csp, more, err = bc.GetPosts(page)
if err != nil {
retries++
if retries > bc.MaxRetries {
log.Println("Max retries reached")
return cs, fmt.Errorf("max retries reached")
}
break
}
cs = append(cs, csp...)
page++
}
return cs, err
}
// GetPosts downloads all posts from BigCommerce, handling pagination
// page: the page number to download
func (bc *Client) GetPosts(page int) ([]Post, bool, error) {
url := "/v2/blog/posts?limit=250&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 []Post
err = json.Unmarshal(body, &pp)
if err != nil {
log.Printf("Error unmarshalling posts: %s %s", err, string(body))
return nil, false, err
}
return pp, len(pp) == 250, nil
}