-
Notifications
You must be signed in to change notification settings - Fork 154
/
custom_pager.go
59 lines (45 loc) · 1.15 KB
/
custom_pager.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
package httphelper
import (
"log"
"net/url"
"github.com/tidwall/gjson"
"github.com/chnsz/golangsdk/pagination"
)
// URLFunc the URL generation function for the next query.
//
// return nil, nil // will terminate the query.
type URLFunc func(body *gjson.Result, curUrl url.URL) (*url.URL, error)
type CustomPager struct {
pagination.PageResult
uuid string
NextURLFunc URLFunc
DataPath string
}
func (p CustomPager) IsEmpty() (bool, error) {
rst, err := bodyToGJson(p.Body)
if err != nil {
return true, err
}
count := len(rst.Get(p.DataPath).Array())
log.Printf("[DEBUG] [CustomPager] [%s] response count: %v, dataPath: %s", p.uuid, count, p.DataPath)
return count == 0, nil
}
func (p CustomPager) NextPageURL() (string, error) {
body, err := bodyToGJson(p.Body)
if err != nil {
return "", err
}
u, err := p.NextURLFunc(body, p.URL)
if err != nil {
return "", err
}
if u == nil {
log.Printf("[ERROR] [CustomPager] [%v] next URL is empty, stop query", p.uuid)
return "", nil
}
log.Printf("[DEBUG] [CustomPager] [%s] NextPageURL: %s", p.uuid, u.String())
return u.String(), nil
}
func (p CustomPager) GetBody() any {
return p.Body
}