github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  func (client Client) paginate(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error) (Warnings, error) {
    10  	fullWarningsList := Warnings{}
    11  
    12  	for {
    13  		wrapper := NewPaginatedResources(obj)
    14  		response := cloudcontroller.Response{
    15  			Result: &wrapper,
    16  		}
    17  
    18  		err := client.connection.Make(request, &response)
    19  		fullWarningsList = append(fullWarningsList, response.Warnings...)
    20  		if err != nil {
    21  			return fullWarningsList, err
    22  		}
    23  
    24  		list, err := wrapper.Resources()
    25  		if err != nil {
    26  			return fullWarningsList, err
    27  		}
    28  
    29  		for _, item := range list {
    30  			err = appendToExternalList(item)
    31  			if err != nil {
    32  				return fullWarningsList, err
    33  			}
    34  		}
    35  
    36  		if wrapper.NextPage() == "" {
    37  			break
    38  		}
    39  
    40  		request, err = client.newHTTPRequest(requestOptions{
    41  			URL:    wrapper.NextPage(),
    42  			Method: http.MethodGet,
    43  		})
    44  		if err != nil {
    45  			return fullWarningsList, err
    46  		}
    47  	}
    48  
    49  	return fullWarningsList, nil
    50  }