alerts.go
func (a *alertDateIterator) Next(ctx context.Context) (*AlertPage, error) {
	var page *AlertPage
	for {
		// just wipe it clean every time to avoid remnants hanging around
		page = new(AlertPage)
		if err := a.p.Next(ctx, page); err != nil {
			return nil, err
		}
		if len(page.Alerts) == 0 {
			return nil, NoMoreResults
		}
		times := make([]time.Time, len(page.Alerts), len(page.Alerts))
		for i, alert := range page.Alerts {
			if !alert.DateCreated.Valid {
				// we really should not ever hit this case but if we can't parse
				// a date, better to give you back an error than to give you back
				// a list of alerts that may or may not be in the time range
				return nil, fmt.Errorf("Couldn't verify the date of alert: %#v", alert)
			}
			times[i] = alert.DateCreated.Time
		}
		if containsResultsInRange(a.start, a.end, times) {
			indexesToDelete := indexesOutsideRange(a.start, a.end, times)
			// reverse order so we don't delete the wrong index
			for i := len(indexesToDelete) - 1; i >= 0; i-- {
				index := indexesToDelete[i]
				page.Alerts = append(page.Alerts[:index], page.Alerts[index+1:]...)
			}
			a.p.SetNextPageURI(page.Meta.NextPageURL)
			return page, nil
		}
		if shouldContinuePaging(a.start, times) {
			a.p.SetNextPageURI(page.Meta.NextPageURL)
			continue
		} else {
			// should not continue paging and no results in range, stop
			return nil, NoMoreResults
		}
	}
}
func (a *alertDateIterator) Next(ctx context.Context) (*AlertPage, error) {
	var page *AlertPage
	for {
		// just wipe it clean every time to avoid remnants hanging around
		page = new(AlertPage)
		if err := a.p.Next(ctx, page); err != nil {
			return nil, err
		}
		if len(page.Alerts) == 0 {
			return nil, NoMoreResults
		}
		times := make([]time.Time, len(page.Alerts), len(page.Alerts))
		for i, alert := range page.Alerts {
			if !alert.DateCreated.Valid {
				// we really should not ever hit this case but if we can't parse
				// a date, better to give you back an error than to give you back
				// a list of alerts that may or may not be in the time range
				return nil, fmt.Errorf("Couldn't verify the date of alert: %#v", alert)
			}
			times[i] = alert.DateCreated.Time
		}
		if containsResultsInRange(a.start, a.end, times) {
			indexesToDelete := indexesOutsideRange(a.start, a.end, times)
			// reverse order so we don't delete the wrong index
			for i := len(indexesToDelete) - 1; i >= 0; i-- {
				index := indexesToDelete[i]
				page.Alerts = append(page.Alerts[:index], page.Alerts[index+1:]...)
			}
			a.p.SetNextPageURI(page.Meta.NextPageURL)
			return page, nil
		}
		if shouldContinuePaging(a.start, times) {
			a.p.SetNextPageURI(page.Meta.NextPageURL)
			continue
		} else {
			// should not continue paging and no results in range, stop
			return nil, NoMoreResults
		}
	}
}
func ( a * alertDateIterator ) Next ( ctx context . Context ) ( * AlertPage , error ) { var page * AlertPage for { // just wipe it clean every time to avoid remnants hanging around page = new ( AlertPage ) if err := a . p . Next ( ctx , page ) ; err != nil { return nil , err } if len ( page . Alerts ) == 0 { return nil , NoMoreResults } times := make ( [ ] time . Time , len ( page . Alerts ) , len ( page . Alerts ) ) for i , alert := range page . Alerts { if ! alert . DateCreated . Valid { // we really should not ever hit this case but if we can't parse // a date, better to give you back an error than to give you back // a list of alerts that may or may not be in the time range return nil , fmt . Errorf ( " " , alert ) } times [ i ] = alert . DateCreated . Time } if containsResultsInRange ( a . start , a . end , times ) { indexesToDelete := indexesOutsideRange ( a . start , a . end , times ) // reverse order so we don't delete the wrong index for i := len ( indexesToDelete ) - 1 ; i >= 0 ; i -- { index := indexesToDelete [ i ] page . Alerts = append ( page . Alerts [ : index ] , page . Alerts [ index + 1 : ] ... ) } a . p . SetNextPageURI ( page . Meta . NextPageURL ) return page , nil } if shouldContinuePaging ( a . start , times ) { a . p . SetNextPageURI ( page . Meta . NextPageURL ) continue } else { // should not continue paging and no results in range, stop return nil , NoMoreResults } } }
// Next returns the next page of resources. We may need to fetch multiple
// pages from the Twilio API before we find one in the right date range, so
// latency may be higher than usual. If page is non-nil, it contains at least
// one result.
Next returns the next page of resources . We may need to fetch multiple pages from the Twilio API before we find one in the right date range so latency may be higher than usual . If page is non - nil it contains at least one result .