github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/app_events/app_events.go (about)

     1  package app_events
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api/resources"
     5  	"github.com/cloudfoundry/cli/cf/api/strategy"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	"github.com/cloudfoundry/cli/cf/net"
     9  )
    10  
    11  type AppEventsRepository interface {
    12  	RecentEvents(appGuid string, limit int64) ([]models.EventFields, error)
    13  }
    14  
    15  type CloudControllerAppEventsRepository struct {
    16  	config   core_config.Reader
    17  	gateway  net.Gateway
    18  	strategy strategy.EndpointStrategy
    19  }
    20  
    21  func NewCloudControllerAppEventsRepository(config core_config.Reader, gateway net.Gateway, strategy strategy.EndpointStrategy) CloudControllerAppEventsRepository {
    22  	return CloudControllerAppEventsRepository{
    23  		config:   config,
    24  		gateway:  gateway,
    25  		strategy: strategy,
    26  	}
    27  }
    28  
    29  func (repo CloudControllerAppEventsRepository) RecentEvents(appGuid string, limit int64) ([]models.EventFields, error) {
    30  	count := int64(0)
    31  	events := make([]models.EventFields, 0, limit)
    32  	apiErr := repo.listEvents(appGuid, limit, func(eventField models.EventFields) bool {
    33  		count++
    34  		events = append(events, eventField)
    35  		return count < limit
    36  	})
    37  
    38  	return events, apiErr
    39  }
    40  
    41  func (repo CloudControllerAppEventsRepository) listEvents(appGuid string, limit int64, cb func(models.EventFields) bool) error {
    42  	return repo.gateway.ListPaginatedResources(
    43  		repo.config.ApiEndpoint(),
    44  		repo.strategy.EventsURL(appGuid, limit),
    45  		repo.strategy.EventsResource(),
    46  
    47  		func(resource interface{}) bool {
    48  			return cb(resource.(resources.EventResource).ToFields())
    49  		})
    50  }