-
Notifications
You must be signed in to change notification settings - Fork 2
/
paginator.go
112 lines (96 loc) · 2.87 KB
/
paginator.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
package httpclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/http"
"regexp"
"strings"
)
type noPaginator struct {
}
// make sure it implements the interface
var _ Paginator = (*noPaginator)(nil)
func (noPaginator) HasMore(page int, req *http.Request, resp *http.Response) (bool, *http.Request) {
return false, nil
}
// NoPaginator returns a no-op paginator that doesn't do pagination
func NoPaginator() Paginator {
return &noPaginator{}
}
type linkPaginator struct {
}
// make sure it implements the interface
var _ Paginator = (*linkPaginator)(nil)
var re = regexp.MustCompile("<(.*)>")
func (linkPaginator) HasMore(page int, req *http.Request, resp *http.Response) (bool, *http.Request) {
link := resp.Header.Get("link")
if link != "" {
for _, token := range strings.Split(link, ", ") {
if strings.Contains(token, "rel=\"next\"") {
url := re.FindStringSubmatch(token)
if len(url) > 1 {
newreq, _ := http.NewRequest(req.Method, url[1], nil)
newreq.Header = req.Header
return true, newreq
}
}
}
}
return false, nil
}
// NewLinkPaginator returns a new Paginator that uses the HTTP Link Header for building the next request
func NewLinkPaginator() Paginator {
return &linkPaginator{}
}
type inBodyPaginator struct {
}
// make sure it implements the interface
var _ Paginator = (*inBodyPaginator)(nil)
func (inBodyPaginator) HasMore(page int, req *http.Request, resp *http.Response) (bool, *http.Request) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, nil
}
var mapBody map[string]interface{}
if err := json.Unmarshal(body, &mapBody); err != nil {
return false, nil
}
if v, exists := mapBody["paging"]; exists {
var P struct {
PageIndex int `json:"pageIndex"`
PageSize float64 `json:"pageSize"`
Total float64 `json:"total"`
}
str := strings.Replace(fmt.Sprintf("%#v", v), "map[string]interface {}", "", 1)
if err := json.Unmarshal([]byte(str), &P); err != nil || P.PageSize <= 0 || P.Total <= 0 {
return false, nil
}
delete(mapBody, "paging")
if newBody, err := json.Marshal(mapBody); err != nil {
return false, nil
} else {
newBody = append(newBody, ',')
resp.Body = ioutil.NopCloser(bytes.NewReader(newBody))
}
if totalPages := int(math.Ceil(P.Total / P.PageSize)); P.PageIndex < totalPages {
index := fmt.Sprintf("p=%v", P.PageIndex)
newIndex := fmt.Sprintf("p=%v", (P.PageIndex + 1))
newURL := strings.Replace(req.URL.String(), index, newIndex, 1)
newreq, _ := http.NewRequest(req.Method, newURL, nil)
if user, pass, ok := req.BasicAuth(); ok {
newreq.SetBasicAuth(user, pass)
}
return true, newreq
}
} else {
resp.Body = ioutil.NopCloser(bytes.NewReader(body))
}
return false, nil
}
// InBodyPaginator returns a new paginator that it's pagination info it's inside body
func InBodyPaginator() Paginator {
return &inBodyPaginator{}
}