|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +package pagination |
| 4 | + |
| 5 | +import ( |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/stainless-sdks/gitpod-go/internal/apijson" |
| 9 | + "github.com/stainless-sdks/gitpod-go/internal/requestconfig" |
| 10 | + "github.com/stainless-sdks/gitpod-go/option" |
| 11 | +) |
| 12 | + |
| 13 | +type ServicesPage[T any] struct { |
| 14 | + Services []T `json:"services"` |
| 15 | + NextToken string `json:"nextToken"` |
| 16 | + JSON servicesPageJSON `json:"-"` |
| 17 | + cfg *requestconfig.RequestConfig |
| 18 | + res *http.Response |
| 19 | +} |
| 20 | + |
| 21 | +// servicesPageJSON contains the JSON metadata for the struct [ServicesPage[T]] |
| 22 | +type servicesPageJSON struct { |
| 23 | + Services apijson.Field |
| 24 | + NextToken apijson.Field |
| 25 | + raw string |
| 26 | + ExtraFields map[string]apijson.Field |
| 27 | +} |
| 28 | + |
| 29 | +func (r *ServicesPage[T]) UnmarshalJSON(data []byte) (err error) { |
| 30 | + return apijson.UnmarshalRoot(data, r) |
| 31 | +} |
| 32 | + |
| 33 | +func (r servicesPageJSON) RawJSON() string { |
| 34 | + return r.raw |
| 35 | +} |
| 36 | + |
| 37 | +// GetNextPage returns the next page as defined by this pagination style. When |
| 38 | +// there is no next page, this function will return a 'nil' for the page value, but |
| 39 | +// will not return an error |
| 40 | +func (r *ServicesPage[T]) GetNextPage() (res *ServicesPage[T], err error) { |
| 41 | + next := r.NextToken |
| 42 | + if len(next) == 0 { |
| 43 | + return nil, nil |
| 44 | + } |
| 45 | + cfg := r.cfg.Clone(r.cfg.Context) |
| 46 | + cfg.Apply(option.WithQuery("token", next)) |
| 47 | + var raw *http.Response |
| 48 | + cfg.ResponseInto = &raw |
| 49 | + cfg.ResponseBodyInto = &res |
| 50 | + err = cfg.Execute() |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + res.SetPageConfig(cfg, raw) |
| 55 | + return res, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (r *ServicesPage[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res *http.Response) { |
| 59 | + if r == nil { |
| 60 | + r = &ServicesPage[T]{} |
| 61 | + } |
| 62 | + r.cfg = cfg |
| 63 | + r.res = res |
| 64 | +} |
| 65 | + |
| 66 | +type ServicesPageAutoPager[T any] struct { |
| 67 | + page *ServicesPage[T] |
| 68 | + cur T |
| 69 | + idx int |
| 70 | + run int |
| 71 | + err error |
| 72 | +} |
| 73 | + |
| 74 | +func NewServicesPageAutoPager[T any](page *ServicesPage[T], err error) *ServicesPageAutoPager[T] { |
| 75 | + return &ServicesPageAutoPager[T]{ |
| 76 | + page: page, |
| 77 | + err: err, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func (r *ServicesPageAutoPager[T]) Next() bool { |
| 82 | + if r.page == nil || len(r.page.Services) == 0 { |
| 83 | + return false |
| 84 | + } |
| 85 | + if r.idx >= len(r.page.Services) { |
| 86 | + r.idx = 0 |
| 87 | + r.page, r.err = r.page.GetNextPage() |
| 88 | + if r.err != nil || r.page == nil || len(r.page.Services) == 0 { |
| 89 | + return false |
| 90 | + } |
| 91 | + } |
| 92 | + r.cur = r.page.Services[r.idx] |
| 93 | + r.run += 1 |
| 94 | + r.idx += 1 |
| 95 | + return true |
| 96 | +} |
| 97 | + |
| 98 | +func (r *ServicesPageAutoPager[T]) Current() T { |
| 99 | + return r.cur |
| 100 | +} |
| 101 | + |
| 102 | +func (r *ServicesPageAutoPager[T]) Err() error { |
| 103 | + return r.err |
| 104 | +} |
| 105 | + |
| 106 | +func (r *ServicesPageAutoPager[T]) Index() int { |
| 107 | + return r.run |
| 108 | +} |
| 109 | + |
| 110 | +type TaskExecutionsPage[T any] struct { |
| 111 | + TaskExecutions []T `json:"taskExecutions"` |
| 112 | + NextToken string `json:"nextToken"` |
| 113 | + JSON taskExecutionsPageJSON `json:"-"` |
| 114 | + cfg *requestconfig.RequestConfig |
| 115 | + res *http.Response |
| 116 | +} |
| 117 | + |
| 118 | +// taskExecutionsPageJSON contains the JSON metadata for the struct |
| 119 | +// [TaskExecutionsPage[T]] |
| 120 | +type taskExecutionsPageJSON struct { |
| 121 | + TaskExecutions apijson.Field |
| 122 | + NextToken apijson.Field |
| 123 | + raw string |
| 124 | + ExtraFields map[string]apijson.Field |
| 125 | +} |
| 126 | + |
| 127 | +func (r *TaskExecutionsPage[T]) UnmarshalJSON(data []byte) (err error) { |
| 128 | + return apijson.UnmarshalRoot(data, r) |
| 129 | +} |
| 130 | + |
| 131 | +func (r taskExecutionsPageJSON) RawJSON() string { |
| 132 | + return r.raw |
| 133 | +} |
| 134 | + |
| 135 | +// GetNextPage returns the next page as defined by this pagination style. When |
| 136 | +// there is no next page, this function will return a 'nil' for the page value, but |
| 137 | +// will not return an error |
| 138 | +func (r *TaskExecutionsPage[T]) GetNextPage() (res *TaskExecutionsPage[T], err error) { |
| 139 | + next := r.NextToken |
| 140 | + if len(next) == 0 { |
| 141 | + return nil, nil |
| 142 | + } |
| 143 | + cfg := r.cfg.Clone(r.cfg.Context) |
| 144 | + cfg.Apply(option.WithQuery("token", next)) |
| 145 | + var raw *http.Response |
| 146 | + cfg.ResponseInto = &raw |
| 147 | + cfg.ResponseBodyInto = &res |
| 148 | + err = cfg.Execute() |
| 149 | + if err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + res.SetPageConfig(cfg, raw) |
| 153 | + return res, nil |
| 154 | +} |
| 155 | + |
| 156 | +func (r *TaskExecutionsPage[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res *http.Response) { |
| 157 | + if r == nil { |
| 158 | + r = &TaskExecutionsPage[T]{} |
| 159 | + } |
| 160 | + r.cfg = cfg |
| 161 | + r.res = res |
| 162 | +} |
| 163 | + |
| 164 | +type TaskExecutionsPageAutoPager[T any] struct { |
| 165 | + page *TaskExecutionsPage[T] |
| 166 | + cur T |
| 167 | + idx int |
| 168 | + run int |
| 169 | + err error |
| 170 | +} |
| 171 | + |
| 172 | +func NewTaskExecutionsPageAutoPager[T any](page *TaskExecutionsPage[T], err error) *TaskExecutionsPageAutoPager[T] { |
| 173 | + return &TaskExecutionsPageAutoPager[T]{ |
| 174 | + page: page, |
| 175 | + err: err, |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func (r *TaskExecutionsPageAutoPager[T]) Next() bool { |
| 180 | + if r.page == nil || len(r.page.TaskExecutions) == 0 { |
| 181 | + return false |
| 182 | + } |
| 183 | + if r.idx >= len(r.page.TaskExecutions) { |
| 184 | + r.idx = 0 |
| 185 | + r.page, r.err = r.page.GetNextPage() |
| 186 | + if r.err != nil || r.page == nil || len(r.page.TaskExecutions) == 0 { |
| 187 | + return false |
| 188 | + } |
| 189 | + } |
| 190 | + r.cur = r.page.TaskExecutions[r.idx] |
| 191 | + r.run += 1 |
| 192 | + r.idx += 1 |
| 193 | + return true |
| 194 | +} |
| 195 | + |
| 196 | +func (r *TaskExecutionsPageAutoPager[T]) Current() T { |
| 197 | + return r.cur |
| 198 | +} |
| 199 | + |
| 200 | +func (r *TaskExecutionsPageAutoPager[T]) Err() error { |
| 201 | + return r.err |
| 202 | +} |
| 203 | + |
| 204 | +func (r *TaskExecutionsPageAutoPager[T]) Index() int { |
| 205 | + return r.run |
| 206 | +} |
0 commit comments