github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/paginate.go (about) 1 package ccv3 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 ) 8 9 type IncludedResources struct { 10 Users []User 11 } 12 13 func (client Client) paginate(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error) (Warnings, error) { 14 fullWarningsList := Warnings{} 15 16 for { 17 wrapper, warnings, err := client.wrapFirstPage(request, obj, appendToExternalList) 18 fullWarningsList = append(fullWarningsList, warnings...) 19 if err != nil { 20 return fullWarningsList, err 21 } 22 23 if wrapper.NextPage() == "" { 24 break 25 } 26 27 request, err = client.newHTTPRequest(requestOptions{ 28 URL: wrapper.NextPage(), 29 Method: http.MethodGet, 30 }) 31 if err != nil { 32 return fullWarningsList, err 33 } 34 } 35 36 return fullWarningsList, nil 37 } 38 39 func (client Client) paginateWithIncludes(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error) (IncludedResources, Warnings, error) { 40 fullWarningsList := Warnings{} 41 var includes IncludedResources 42 43 for { 44 wrapper, warnings, err := client.wrapFirstPage(request, obj, appendToExternalList) 45 fullWarningsList = append(fullWarningsList, warnings...) 46 if err != nil { 47 return IncludedResources{}, fullWarningsList, err 48 } 49 50 includes.Users = append(includes.Users, wrapper.IncludedResources.UserResource...) 51 52 if wrapper.NextPage() == "" { 53 break 54 } 55 56 request, err = client.newHTTPRequest(requestOptions{ 57 URL: wrapper.NextPage(), 58 Method: http.MethodGet, 59 }) 60 if err != nil { 61 return IncludedResources{}, fullWarningsList, err 62 } 63 } 64 65 return includes, fullWarningsList, nil 66 } 67 68 func (client Client) wrapFirstPage(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error) (*PaginatedResources, Warnings, error) { 69 warnings := Warnings{} 70 wrapper := NewPaginatedResources(obj) 71 response := cloudcontroller.Response{ 72 DecodeJSONResponseInto: &wrapper, 73 } 74 75 err := client.connection.Make(request, &response) 76 warnings = append(warnings, response.Warnings...) 77 if err != nil { 78 return nil, warnings, err 79 } 80 81 list, err := wrapper.Resources() 82 if err != nil { 83 return nil, warnings, err 84 } 85 86 for _, item := range list { 87 err = appendToExternalList(item) 88 if err != nil { 89 return nil, warnings, err 90 } 91 } 92 93 return wrapper, warnings, nil 94 }