github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/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 (requester RealRequester) paginate(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error, specificPage bool) (IncludedResources, Warnings, error) {
    10  	fullWarningsList := Warnings{}
    11  	var includes IncludedResources
    12  
    13  	for {
    14  		wrapper, warnings, err := requester.wrapFirstPage(request, obj, appendToExternalList)
    15  		fullWarningsList = append(fullWarningsList, warnings...)
    16  		if err != nil {
    17  			return IncludedResources{}, fullWarningsList, err
    18  		}
    19  
    20  		includes.Apps = append(includes.Apps, wrapper.IncludedResources.Apps...)
    21  		includes.Users = append(includes.Users, wrapper.IncludedResources.Users...)
    22  		includes.Organizations = append(includes.Organizations, wrapper.IncludedResources.Organizations...)
    23  		includes.Spaces = append(includes.Spaces, wrapper.IncludedResources.Spaces...)
    24  		includes.ServiceBrokers = append(includes.ServiceBrokers, wrapper.IncludedResources.ServiceBrokers...)
    25  		includes.ServiceInstances = append(includes.ServiceInstances, wrapper.IncludedResources.ServiceInstances...)
    26  		includes.ServiceOfferings = append(includes.ServiceOfferings, wrapper.IncludedResources.ServiceOfferings...)
    27  		includes.ServicePlans = append(includes.ServicePlans, wrapper.IncludedResources.ServicePlans...)
    28  
    29  		if specificPage || wrapper.NextPage() == "" {
    30  			break
    31  		}
    32  
    33  		request, err = requester.newHTTPRequest(requestOptions{
    34  			URL:    wrapper.NextPage(),
    35  			Method: http.MethodGet,
    36  		})
    37  		if err != nil {
    38  			return IncludedResources{}, fullWarningsList, err
    39  		}
    40  	}
    41  
    42  	return includes, fullWarningsList, nil
    43  }
    44  
    45  func (requester RealRequester) wrapFirstPage(request *cloudcontroller.Request, obj interface{}, appendToExternalList func(interface{}) error) (*PaginatedResources, Warnings, error) {
    46  	warnings := Warnings{}
    47  	wrapper := NewPaginatedResources(obj)
    48  	response := cloudcontroller.Response{
    49  		DecodeJSONResponseInto: &wrapper,
    50  	}
    51  
    52  	err := requester.connection.Make(request, &response)
    53  	warnings = append(warnings, response.Warnings...)
    54  	if err != nil {
    55  		return nil, warnings, err
    56  	}
    57  
    58  	list, err := wrapper.Resources()
    59  	if err != nil {
    60  		return nil, warnings, err
    61  	}
    62  
    63  	for _, item := range list {
    64  		err = appendToExternalList(item)
    65  		if err != nil {
    66  			return nil, warnings, err
    67  		}
    68  	}
    69  
    70  	return wrapper, warnings, nil
    71  }