calls.go
func (c *callDateIterator) Next(ctx context.Context) (*CallPage, error) {
	var page *CallPage
	for {
		// just wipe it clean every time to avoid remnants hanging around
		page = new(CallPage)
		if err := c.p.Next(ctx, page); err != nil {
			return nil, err
		}
		if len(page.Calls) == 0 {
			return nil, NoMoreResults
		}
		times := make([]time.Time, len(page.Calls), len(page.Calls))
		for i, call := range page.Calls {
			if !call.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 calls that may or may not be in the time range
				return nil, fmt.Errorf("Couldn't verify the date of call: %#v", call)
			}
			times[i] = call.DateCreated.Time
		}
		if containsResultsInRange(c.start, c.end, times) {
			indexesToDelete := indexesOutsideRange(c.start, c.end, times)
			// reverse order so we don't delete the wrong index
			for i := len(indexesToDelete) - 1; i >= 0; i-- {
				index := indexesToDelete[i]
				page.Calls = append(page.Calls[:index], page.Calls[index+1:]...)
			}
			c.p.SetNextPageURI(page.NextPageURI)
			return page, nil
		}
		if shouldContinuePaging(c.start, times) {
			c.p.SetNextPageURI(page.NextPageURI)
			continue
		} else {
			// should not continue paging and no results in range, stop
			return nil, NoMoreResults
		}
	}
}
func (c *callDateIterator) Next(ctx context.Context) (*CallPage, error) {
	var page *CallPage
	for {
		// just wipe it clean every time to avoid remnants hanging around
		page = new(CallPage)
		if err := c.p.Next(ctx, page); err != nil {
			return nil, err
		}
		if len(page.Calls) == 0 {
			return nil, NoMoreResults
		}
		times := make([]time.Time, len(page.Calls), len(page.Calls))
		for i, call := range page.Calls {
			if !call.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 calls that may or may not be in the time range
				return nil, fmt.Errorf("Couldn't verify the date of call: %#v", call)
			}
			times[i] = call.DateCreated.Time
		}
		if containsResultsInRange(c.start, c.end, times) {
			indexesToDelete := indexesOutsideRange(c.start, c.end, times)
			// reverse order so we don't delete the wrong index
			for i := len(indexesToDelete) - 1; i >= 0; i-- {
				index := indexesToDelete[i]
				page.Calls = append(page.Calls[:index], page.Calls[index+1:]...)
			}
			c.p.SetNextPageURI(page.NextPageURI)
			return page, nil
		}
		if shouldContinuePaging(c.start, times) {
			c.p.SetNextPageURI(page.NextPageURI)
			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.