Add generic listAll helper using Go 1.23+ iter.Seq2 for memory-efficient pagination. Implement ListAll() on BooksService and PagesService. Tests cover multi-page iteration, early break, errors, and empty results.
37 lines
937 B
Go
37 lines
937 B
Go
package bookstack
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"iter"
|
|
)
|
|
|
|
// BooksService handles operations on books.
|
|
type BooksService struct {
|
|
client *Client
|
|
}
|
|
|
|
// List returns a list of books with optional filtering.
|
|
func (s *BooksService) List(ctx context.Context, opts *ListOptions) ([]Book, error) {
|
|
var resp listResponse[Book]
|
|
err := s.client.do(ctx, "GET", "/api/books"+opts.queryString(), nil, &resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return resp.Data, nil
|
|
}
|
|
|
|
// ListAll returns an iterator over all books, handling pagination automatically.
|
|
func (s *BooksService) ListAll(ctx context.Context) iter.Seq2[Book, error] {
|
|
return listAll[Book](ctx, s.client, "/api/books")
|
|
}
|
|
|
|
// Get retrieves a single book by ID.
|
|
func (s *BooksService) Get(ctx context.Context, id int) (*Book, error) {
|
|
var book Book
|
|
err := s.client.do(ctx, "GET", fmt.Sprintf("/api/books/%d", id), nil, &book)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &book, nil
|
|
}
|