-
Notifications
You must be signed in to change notification settings - Fork 927
/
paginate.go
68 lines (55 loc) · 1.99 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
package ccv3
import (
"net/http"
"code.cloudfoundry.org/cli/api/cloudcontroller"
)
func (requester RealRequester) paginate(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error) (IncludedResources, Warnings, error) {
fullWarningsList := Warnings{}
var includes IncludedResources
for {
wrapper, warnings, err := requester.wrapFirstPage(request, obj, appendToExternalList)
fullWarningsList = append(fullWarningsList, warnings...)
if err != nil {
return IncludedResources{}, fullWarningsList, err
}
includes.Users = append(includes.Users, wrapper.IncludedResources.Users...)
includes.Organizations = append(includes.Organizations, wrapper.IncludedResources.Organizations...)
includes.Spaces = append(includes.Spaces, wrapper.IncludedResources.Spaces...)
includes.ServiceOfferings = append(includes.ServiceOfferings, wrapper.IncludedResources.ServiceOfferings...)
includes.ServiceBrokers = append(includes.ServiceBrokers, wrapper.IncludedResources.ServiceBrokers...)
if wrapper.NextPage() == "" {
break
}
request, err = requester.newHTTPRequest(requestOptions{
URL: wrapper.NextPage(),
Method: http.MethodGet,
})
if err != nil {
return IncludedResources{}, fullWarningsList, err
}
}
return includes, fullWarningsList, nil
}
func (requester RealRequester) wrapFirstPage(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error) (*PaginatedResources, Warnings, error) {
warnings := Warnings{}
wrapper := NewPaginatedResources(obj)
response := cloudcontroller.Response{
DecodeJSONResponseInto: &wrapper,
}
err := requester.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
}