Skip to content

Commit

Permalink
simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
vtopc committed Oct 25, 2024
1 parent 24f0076 commit e31765f
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ type MetricsPagination struct {
type MetricsIterator struct {
opts MetricsOptions
req *httpRequest
resp MetricsResponse
err error
}

Expand All @@ -73,38 +72,35 @@ func (iter *MetricsIterator) Next(ctx context.Context, resp *MetricsResponse) (m
return false
}

iter.err = iter.fetch(ctx)
iter.err = iter.fetch(ctx, resp)
if iter.err != nil {
return false
}

*resp = iter.resp
iter.opts.Pagination.Skip = iter.opts.Pagination.Skip + iter.opts.Pagination.Limit

if len(iter.resp.Items) < iter.opts.Pagination.Limit {
if len(resp.Items) < iter.opts.Pagination.Limit {
return false
}

return true
}

func (iter *MetricsIterator) fetch(ctx context.Context) error {
func (iter *MetricsIterator) fetch(ctx context.Context, resp *MetricsResponse) error {
payload := newJSONEncodedPayload(iter.opts)

httpResp, err := makePostRequest(ctx, iter.req, payload)
if err != nil {
return err
}

resp := MetricsResponse{
Items: make([]MetricsItem, 0, iter.opts.Pagination.Limit),
}
err = httpResp.parseFromJSON(&resp)
// preallocate
resp.Items = make([]MetricsItem, 0, iter.opts.Pagination.Limit)

err = httpResp.parseFromJSON(resp)
if err != nil {
return errors.Wrap(err, "decoding response")
}

iter.resp = resp

return nil
}

0 comments on commit e31765f

Please sign in to comment.