github.com/anthropics/anthropic-sdk-go@v1.70.1/packages/pagination/pagination.go (about) 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/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 16 type paramUnion = param.APIUnion 17 18 // aliased to make [param.APIObject] private when embedding 19 type paramObj = param.APIObject 20 21 type Page[T any] struct { 22 Data []T `json:"data"` 23 HasMore bool `json:"has_more"` 24 FirstID string `json:"first_id" api:"nullable"` 25 LastID string `json:"last_id" api:"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 40 func (r Page[T]) RawJSON() string { return r.JSON.raw } 41 func (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 48 func (r *Page[T]) GetNextPage() (res *Page[T], err error) { 49 if len(r.Data) == 0 { 50 return nil, nil 51 } 52 53 if r.JSON.HasMore.Valid() && r.HasMore == false { 54 return nil, nil 55 } 56 cfg := r.cfg.Clone(r.cfg.Context) 57 if r.cfg.Request.URL.Query().Has("before_id") { 58 next := r.FirstID 59 if next == "" { 60 return nil, nil 61 } 62 err = cfg.Apply(option.WithQuery("before_id", next)) 63 if err != nil { 64 return nil, err 65 } 66 } else { 67 next := r.LastID 68 if next == "" { 69 return nil, nil 70 } 71 err = cfg.Apply(option.WithQuery("after_id", next)) 72 if err != nil { 73 return nil, err 74 } 75 } 76 var raw *http.Response 77 cfg.ResponseInto = &raw 78 cfg.ResponseBodyInto = &res 79 err = cfg.Execute() 80 if err != nil { 81 return nil, err 82 } 83 res.SetPageConfig(cfg, raw) 84 return res, nil 85 } 86 87 func (r *Page[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res *http.Response) { 88 if r == nil { 89 r = &Page[T]{} 90 } 91 r.cfg = cfg 92 r.res = res 93 } 94 95 type PageAutoPager[T any] struct { 96 page *Page[T] 97 cur T 98 idx int 99 run int 100 err error 101 paramObj 102 } 103 104 func NewPageAutoPager[T any](page *Page[T], err error) *PageAutoPager[T] { 105 return &PageAutoPager[T]{ 106 page: page, 107 err: err, 108 } 109 } 110 111 func (r *PageAutoPager[T]) Next() bool { 112 if r.page == nil || len(r.page.Data) == 0 { 113 return false 114 } 115 if r.idx >= len(r.page.Data) { 116 r.idx = 0 117 r.page, r.err = r.page.GetNextPage() 118 if r.err != nil || r.page == nil || len(r.page.Data) == 0 { 119 return false 120 } 121 } 122 r.cur = r.page.Data[r.idx] 123 r.run += 1 124 r.idx += 1 125 return true 126 } 127 128 func (r *PageAutoPager[T]) Current() T { 129 return r.cur 130 } 131 132 func (r *PageAutoPager[T]) Err() error { 133 return r.err 134 } 135 136 func (r *PageAutoPager[T]) Index() int { 137 return r.run 138 } 139 140 type TokenPage[T any] struct { 141 Data []T `json:"data"` 142 HasMore bool `json:"has_more"` 143 NextPage string `json:"next_page" api:"nullable"` 144 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 145 JSON struct { 146 Data respjson.Field 147 HasMore respjson.Field 148 NextPage respjson.Field 149 ExtraFields map[string]respjson.Field 150 raw string 151 } `json:"-"` 152 cfg *requestconfig.RequestConfig 153 res *http.Response 154 } 155 156 // Returns the unmodified JSON received from the API 157 func (r TokenPage[T]) RawJSON() string { return r.JSON.raw } 158 func (r *TokenPage[T]) UnmarshalJSON(data []byte) error { 159 return apijson.UnmarshalRoot(data, r) 160 } 161 162 // GetNextPage returns the next page as defined by this pagination style. When 163 // there is no next page, this function will return a 'nil' for the page value, but 164 // will not return an error 165 func (r *TokenPage[T]) GetNextPage() (res *TokenPage[T], err error) { 166 if len(r.Data) == 0 { 167 return nil, nil 168 } 169 170 if r.JSON.HasMore.Valid() && r.HasMore == false { 171 return nil, nil 172 } 173 next := r.NextPage 174 if len(next) == 0 { 175 return nil, nil 176 } 177 cfg := r.cfg.Clone(r.cfg.Context) 178 err = cfg.Apply(option.WithQuery("page_token", next)) 179 if err != nil { 180 return nil, err 181 } 182 var raw *http.Response 183 cfg.ResponseInto = &raw 184 cfg.ResponseBodyInto = &res 185 err = cfg.Execute() 186 if err != nil { 187 return nil, err 188 } 189 res.SetPageConfig(cfg, raw) 190 return res, nil 191 } 192 193 func (r *TokenPage[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res *http.Response) { 194 if r == nil { 195 r = &TokenPage[T]{} 196 } 197 r.cfg = cfg 198 r.res = res 199 } 200 201 type TokenPageAutoPager[T any] struct { 202 page *TokenPage[T] 203 cur T 204 idx int 205 run int 206 err error 207 paramObj 208 } 209 210 func NewTokenPageAutoPager[T any](page *TokenPage[T], err error) *TokenPageAutoPager[T] { 211 return &TokenPageAutoPager[T]{ 212 page: page, 213 err: err, 214 } 215 } 216 217 func (r *TokenPageAutoPager[T]) Next() bool { 218 if r.page == nil || len(r.page.Data) == 0 { 219 return false 220 } 221 if r.idx >= len(r.page.Data) { 222 r.idx = 0 223 r.page, r.err = r.page.GetNextPage() 224 if r.err != nil || r.page == nil || len(r.page.Data) == 0 { 225 return false 226 } 227 } 228 r.cur = r.page.Data[r.idx] 229 r.run += 1 230 r.idx += 1 231 return true 232 } 233 234 func (r *TokenPageAutoPager[T]) Current() T { 235 return r.cur 236 } 237 238 func (r *TokenPageAutoPager[T]) Err() error { 239 return r.err 240 } 241 242 func (r *TokenPageAutoPager[T]) Index() int { 243 return r.run 244 } 245 246 type PageCursor[T any] struct { 247 Data []T `json:"data"` 248 NextPage string `json:"next_page" api:"nullable"` 249 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 250 JSON struct { 251 Data respjson.Field 252 NextPage respjson.Field 253 ExtraFields map[string]respjson.Field 254 raw string 255 } `json:"-"` 256 cfg *requestconfig.RequestConfig 257 res *http.Response 258 } 259 260 // Returns the unmodified JSON received from the API 261 func (r PageCursor[T]) RawJSON() string { return r.JSON.raw } 262 func (r *PageCursor[T]) UnmarshalJSON(data []byte) error { 263 return apijson.UnmarshalRoot(data, r) 264 } 265 266 // GetNextPage returns the next page as defined by this pagination style. When 267 // there is no next page, this function will return a 'nil' for the page value, but 268 // will not return an error 269 func (r *PageCursor[T]) GetNextPage() (res *PageCursor[T], err error) { 270 if len(r.Data) == 0 { 271 return nil, nil 272 } 273 next := r.NextPage 274 if len(next) == 0 { 275 return nil, nil 276 } 277 cfg := r.cfg.Clone(r.cfg.Context) 278 err = cfg.Apply(option.WithQuery("page", next)) 279 if err != nil { 280 return nil, err 281 } 282 var raw *http.Response 283 cfg.ResponseInto = &raw 284 cfg.ResponseBodyInto = &res 285 err = cfg.Execute() 286 if err != nil { 287 return nil, err 288 } 289 res.SetPageConfig(cfg, raw) 290 return res, nil 291 } 292 293 func (r *PageCursor[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res *http.Response) { 294 if r == nil { 295 r = &PageCursor[T]{} 296 } 297 r.cfg = cfg 298 r.res = res 299 } 300 301 type PageCursorAutoPager[T any] struct { 302 page *PageCursor[T] 303 cur T 304 idx int 305 run int 306 err error 307 paramObj 308 } 309 310 func NewPageCursorAutoPager[T any](page *PageCursor[T], err error) *PageCursorAutoPager[T] { 311 return &PageCursorAutoPager[T]{ 312 page: page, 313 err: err, 314 } 315 } 316 317 func (r *PageCursorAutoPager[T]) Next() bool { 318 if r.page == nil || len(r.page.Data) == 0 { 319 return false 320 } 321 if r.idx >= len(r.page.Data) { 322 r.idx = 0 323 r.page, r.err = r.page.GetNextPage() 324 if r.err != nil || r.page == nil || len(r.page.Data) == 0 { 325 return false 326 } 327 } 328 r.cur = r.page.Data[r.idx] 329 r.run += 1 330 r.idx += 1 331 return true 332 } 333 334 func (r *PageCursorAutoPager[T]) Current() T { 335 return r.cur 336 } 337 338 func (r *PageCursorAutoPager[T]) Err() error { 339 return r.err 340 } 341 342 func (r *PageCursorAutoPager[T]) Index() int { 343 return r.run 344 } 345 346 type BidirectionalPageCursor[T any] struct { 347 Data []T `json:"data"` 348 NextPage string `json:"next_page" api:"nullable"` 349 PrevPage string `json:"prev_page" api:"nullable"` 350 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 351 JSON struct { 352 Data respjson.Field 353 NextPage respjson.Field 354 PrevPage respjson.Field 355 ExtraFields map[string]respjson.Field 356 raw string 357 } `json:"-"` 358 cfg *requestconfig.RequestConfig 359 res *http.Response 360 } 361 362 // Returns the unmodified JSON received from the API 363 func (r BidirectionalPageCursor[T]) RawJSON() string { return r.JSON.raw } 364 func (r *BidirectionalPageCursor[T]) UnmarshalJSON(data []byte) error { 365 return apijson.UnmarshalRoot(data, r) 366 } 367 368 // GetNextPage returns the next page as defined by this pagination style. When 369 // there is no next page, this function will return a 'nil' for the page value, but 370 // will not return an error 371 func (r *BidirectionalPageCursor[T]) GetNextPage() (res *BidirectionalPageCursor[T], err error) { 372 if len(r.Data) == 0 { 373 return nil, nil 374 } 375 next := r.NextPage 376 if len(next) == 0 { 377 return nil, nil 378 } 379 cfg := r.cfg.Clone(r.cfg.Context) 380 err = cfg.Apply(option.WithQuery("page", next)) 381 if err != nil { 382 return nil, err 383 } 384 var raw *http.Response 385 cfg.ResponseInto = &raw 386 cfg.ResponseBodyInto = &res 387 err = cfg.Execute() 388 if err != nil { 389 return nil, err 390 } 391 res.SetPageConfig(cfg, raw) 392 return res, nil 393 } 394 395 func (r *BidirectionalPageCursor[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res *http.Response) { 396 if r == nil { 397 r = &BidirectionalPageCursor[T]{} 398 } 399 r.cfg = cfg 400 r.res = res 401 } 402 403 type BidirectionalPageCursorAutoPager[T any] struct { 404 page *BidirectionalPageCursor[T] 405 cur T 406 idx int 407 run int 408 err error 409 paramObj 410 } 411 412 func NewBidirectionalPageCursorAutoPager[T any](page *BidirectionalPageCursor[T], err error) *BidirectionalPageCursorAutoPager[T] { 413 return &BidirectionalPageCursorAutoPager[T]{ 414 page: page, 415 err: err, 416 } 417 } 418 419 func (r *BidirectionalPageCursorAutoPager[T]) Next() bool { 420 if r.page == nil || len(r.page.Data) == 0 { 421 return false 422 } 423 if r.idx >= len(r.page.Data) { 424 r.idx = 0 425 r.page, r.err = r.page.GetNextPage() 426 if r.err != nil || r.page == nil || len(r.page.Data) == 0 { 427 return false 428 } 429 } 430 r.cur = r.page.Data[r.idx] 431 r.run += 1 432 r.idx += 1 433 return true 434 } 435 436 func (r *BidirectionalPageCursorAutoPager[T]) Current() T { 437 return r.cur 438 } 439 440 func (r *BidirectionalPageCursorAutoPager[T]) Err() error { 441 return r.err 442 } 443 444 func (r *BidirectionalPageCursorAutoPager[T]) Index() int { 445 return r.run 446 }