diff --git a/api_client_iter_events.go b/api_client_iter_events.go new file mode 100644 index 0000000..4086001 --- /dev/null +++ b/api_client_iter_events.go @@ -0,0 +1,30 @@ +//go:build go1.23 + +package flareio + +import ( + "iter" + "net/http" + "net/url" +) + +// IterEvent contains results for a given page. +type IterEventsResult struct { + // Response associated with the fetched page. + // + // The response's body must be closed. + Response *http.Response + + // Next is the token to be used to fetch the next page. + Next string +} + +func (client *ApiClient) IterEventsPostJson( + pagesPath string, + eventsPath string, + parms *url.Values, + body map[string]interface{}, +) iter.Seq2[*IterEventsResult, error] { + return func(yield func(*IterEventsResult, error) bool) { + } +} diff --git a/ratelimit.go b/ratelimit.go new file mode 100644 index 0000000..3723c74 --- /dev/null +++ b/ratelimit.go @@ -0,0 +1,37 @@ +//go:build go1.23 + +package flareio + +import ( + "time" +) + +type limiter struct { + tickInterval time.Duration + sleeper func(time.Duration) + sleptFor time.Duration + nextTick time.Time +} + +func newLimiter( + tickInterval time.Duration, +) *limiter { + return &limiter{ + tickInterval: tickInterval, + } +} + +func (l *limiter) sleep(duration time.Duration) { + if l.sleeper != nil { + l.sleeper(duration) + } else { + time.Sleep(duration) + } + l.sleptFor += duration +} + +func (l *limiter) tick() { + untilNextTick := time.Until(l.nextTick) + l.sleep(untilNextTick) + l.sleptFor += max(untilNextTick, 0) +} diff --git a/ratelimit_test.go b/ratelimit_test.go new file mode 100644 index 0000000..114227e --- /dev/null +++ b/ratelimit_test.go @@ -0,0 +1,20 @@ +//go:build go1.23 + +package flareio + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestLimiter(t *testing.T) { + limiter := newLimiter(1) + assert.NotNil(t, limiter, "limiter should be created") + assert.Equal(t, time.Duration(0), limiter.sleptFor, "initial sleep time should be 0") + + // First tick should be instant + limiter.tick() + assert.Equal(t, time.Duration(0), limiter.sleptFor, "first tick should be instant") +}