-
Notifications
You must be signed in to change notification settings - Fork 81
/
pagination.go
56 lines (48 loc) · 1.25 KB
/
pagination.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
package bitbucketserver
import (
"fmt"
bbv1 "github.com/gfleury/go-bitbucket-v1"
)
type apiResultfunc func(int) (*bbv1.APIResponse, error)
// paginate go over an API call and fetch next results.
func paginate(apiResultfunc apiResultfunc) ([]interface{}, error) {
var nextPageStart int
allValues := []interface{}{}
for {
result, err := apiResultfunc(nextPageStart)
if err != nil {
return nil, err
}
if result.Payload != nil {
// I know I know your eyebrow is 🤨, the lib sometime return the payload and sometime parsed values.
// so we just return the raw payload and we will handle it in the caller
allValues = append(allValues, result.Payload)
} else {
if result.Values["values"] == nil {
return nil, fmt.Errorf("key \"values\" not found in result")
}
values, ok := result.Values["values"].([]interface{})
if !ok {
return nil, fmt.Errorf("key \"values\" is not an array")
}
allValues = append(allValues, values...)
}
np, ok := result.Values["nextPageStart"].(float64)
if !ok {
break
}
nextPageStart = int(np)
isLastPage, ok := result.Values["isLastPage"]
if !ok {
break
}
isLastPageb, ok := isLastPage.(bool)
if !ok {
break
}
if isLastPageb {
break
}
}
return allValues, nil
}