Skip to content

Commit

Permalink
feat: update via SDK Studio (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored Mar 12, 2024
1 parent 29c227c commit 2439bd2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/param/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ func (f Field[T]) String() string {
if s, ok := any(f.Value).(fmt.Stringer); ok {
return s.String()
}
return fmt.Sprintf("%#v", f.Value)
return fmt.Sprintf("%v", f.Value)
}
92 changes: 92 additions & 0 deletions internal/shared/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,95 @@ func (r *CursorLimitPaginationAutoPager[T]) Err() error {
func (r *CursorLimitPaginationAutoPager[T]) Index() int {
return r.run
}

type SinglePage[T any] struct {
Items []T `json:"-,inline"`
JSON singlePageJSON `json:"-"`
cfg *requestconfig.RequestConfig
res *http.Response
}

// singlePageJSON contains the JSON metadata for the struct [SinglePage[T]]
type singlePageJSON struct {
Items apijson.Field
raw string
ExtraFields map[string]apijson.Field
}

func (r *SinglePage[T]) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}

func (r singlePageJSON) RawJSON() string {
return r.raw
}

// NextPage returns the next page as defined by this pagination style. When there
// is no next page, this function will return a 'nil' for the page value, but will
// not return an error
func (r *SinglePage[T]) GetNextPage() (res *SinglePage[T], err error) {
// This page represents a response that isn't actually paginated at the API level
// so there will never be a next page.
cfg := (*requestconfig.RequestConfig)(nil)
if cfg == nil {
return nil, nil
}
var raw *http.Response
cfg.ResponseInto = &raw
cfg.ResponseBodyInto = &res
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}

func (r *SinglePage[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res *http.Response) {
r.cfg = cfg
r.res = res
}

type SinglePageAutoPager[T any] struct {
page *SinglePage[T]
cur T
idx int
run int
err error
}

func NewSinglePageAutoPager[T any](page *SinglePage[T], err error) *SinglePageAutoPager[T] {
return &SinglePageAutoPager[T]{
page: page,
err: err,
}
}

func (r *SinglePageAutoPager[T]) Next() bool {
if r.page == nil || len(r.page.Items) == 0 {
return false
}
if r.idx >= len(r.page.Items) {
r.idx = 0
r.page, r.err = r.page.GetNextPage()
if r.err != nil || r.page == nil || len(r.page.Items) == 0 {
return false
}
}
r.cur = r.page.Items[r.idx]
r.run += 1
r.idx += 1
return true
}

func (r *SinglePageAutoPager[T]) Current() T {
return r.cur
}

func (r *SinglePageAutoPager[T]) Err() error {
return r.err
}

func (r *SinglePageAutoPager[T]) Index() int {
return r.run
}

0 comments on commit 2439bd2

Please sign in to comment.