forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paginate.go
94 lines (75 loc) · 2.27 KB
/
paginate.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
package ccv3
import (
"net/http"
"code.cloudfoundry.org/cli/api/cloudcontroller"
)
type IncludedResources struct {
Users []User
}
func (client Client) paginate(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error) (Warnings, error) {
fullWarningsList := Warnings{}
for {
wrapper, warnings, err := client.wrapFirstPage(request, obj, appendToExternalList)
fullWarningsList = append(fullWarningsList, warnings...)
if err != nil {
return fullWarningsList, err
}
if wrapper.NextPage() == "" {
break
}
request, err = client.newHTTPRequest(requestOptions{
URL: wrapper.NextPage(),
Method: http.MethodGet,
})
if err != nil {
return fullWarningsList, err
}
}
return fullWarningsList, nil
}
func (client Client) paginateWithIncludes(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error) (IncludedResources, Warnings, error) {
fullWarningsList := Warnings{}
var includes IncludedResources
for {
wrapper, warnings, err := client.wrapFirstPage(request, obj, appendToExternalList)
fullWarningsList = append(fullWarningsList, warnings...)
if err != nil {
return IncludedResources{}, fullWarningsList, err
}
includes.Users = append(includes.Users, wrapper.IncludedResources.UserResource...)
if wrapper.NextPage() == "" {
break
}
request, err = client.newHTTPRequest(requestOptions{
URL: wrapper.NextPage(),
Method: http.MethodGet,
})
if err != nil {
return IncludedResources{}, fullWarningsList, err
}
}
return includes, fullWarningsList, nil
}
func (client Client) wrapFirstPage(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error) (*PaginatedResources, Warnings, error) {
warnings := Warnings{}
wrapper := NewPaginatedResources(obj)
response := cloudcontroller.Response{
DecodeJSONResponseInto: &wrapper,
}
err := client.connection.Make(request, &response)
warnings = append(warnings, response.Warnings...)
if err != nil {
return nil, warnings, err
}
list, err := wrapper.Resources()
if err != nil {
return nil, warnings, err
}
for _, item := range list {
err = appendToExternalList(item)
if err != nil {
return nil, warnings, err
}
}
return wrapper, warnings, nil
}