pagination.go

  1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3package pagination
  4
  5import (
  6	"net/http"
  7
  8	"github.com/anthropics/anthropic-sdk-go/internal/apijson"
  9	"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
 10	"github.com/anthropics/anthropic-sdk-go/option"
 11	"github.com/anthropics/anthropic-sdk-go/packages/param"
 12	"github.com/anthropics/anthropic-sdk-go/packages/respjson"
 13)
 14
 15// aliased to make [param.APIUnion] private when embedding
 16type paramUnion = param.APIUnion
 17
 18// aliased to make [param.APIObject] private when embedding
 19type paramObj = param.APIObject
 20
 21type Page[T any] struct {
 22	Data    []T    `json:"data"`
 23	HasMore bool   `json:"has_more"`
 24	FirstID string `json:"first_id,nullable"`
 25	LastID  string `json:"last_id,nullable"`
 26	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
 27	JSON struct {
 28		Data        respjson.Field
 29		HasMore     respjson.Field
 30		FirstID     respjson.Field
 31		LastID      respjson.Field
 32		ExtraFields map[string]respjson.Field
 33		raw         string
 34	} `json:"-"`
 35	cfg *requestconfig.RequestConfig
 36	res *http.Response
 37}
 38
 39// Returns the unmodified JSON received from the API
 40func (r Page[T]) RawJSON() string { return r.JSON.raw }
 41func (r *Page[T]) UnmarshalJSON(data []byte) error {
 42	return apijson.UnmarshalRoot(data, r)
 43}
 44
 45// GetNextPage returns the next page as defined by this pagination style. When
 46// there is no next page, this function will return a 'nil' for the page value, but
 47// will not return an error
 48func (r *Page[T]) GetNextPage() (res *Page[T], err error) {
 49	if r.JSON.HasMore.Valid() && r.HasMore == false {
 50		return nil, nil
 51	}
 52	cfg := r.cfg.Clone(r.cfg.Context)
 53	if r.cfg.Request.URL.Query().Has("before_id") {
 54		next := r.FirstID
 55		if next == "" {
 56			return nil, nil
 57		}
 58		err = cfg.Apply(option.WithQuery("before_id", next))
 59		if err != nil {
 60			return nil, err
 61		}
 62	} else {
 63		next := r.LastID
 64		if next == "" {
 65			return nil, nil
 66		}
 67		err = cfg.Apply(option.WithQuery("after_id", next))
 68		if err != nil {
 69			return nil, err
 70		}
 71	}
 72	var raw *http.Response
 73	cfg.ResponseInto = &raw
 74	cfg.ResponseBodyInto = &res
 75	err = cfg.Execute()
 76	if err != nil {
 77		return nil, err
 78	}
 79	res.SetPageConfig(cfg, raw)
 80	return res, nil
 81}
 82
 83func (r *Page[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res *http.Response) {
 84	if r == nil {
 85		r = &Page[T]{}
 86	}
 87	r.cfg = cfg
 88	r.res = res
 89}
 90
 91type PageAutoPager[T any] struct {
 92	page *Page[T]
 93	cur  T
 94	idx  int
 95	run  int
 96	err  error
 97	paramObj
 98}
 99
100func NewPageAutoPager[T any](page *Page[T], err error) *PageAutoPager[T] {
101	return &PageAutoPager[T]{
102		page: page,
103		err:  err,
104	}
105}
106
107func (r *PageAutoPager[T]) Next() bool {
108	if r.page == nil || len(r.page.Data) == 0 {
109		return false
110	}
111	if r.idx >= len(r.page.Data) {
112		r.idx = 0
113		r.page, r.err = r.page.GetNextPage()
114		if r.err != nil || r.page == nil || len(r.page.Data) == 0 {
115			return false
116		}
117	}
118	r.cur = r.page.Data[r.idx]
119	r.run += 1
120	r.idx += 1
121	return true
122}
123
124func (r *PageAutoPager[T]) Current() T {
125	return r.cur
126}
127
128func (r *PageAutoPager[T]) Err() error {
129	return r.err
130}
131
132func (r *PageAutoPager[T]) Index() int {
133	return r.run
134}