feat(bookstack-api-9xo): implement BooksService List and Get

Add listResponse generic type and ListOptions.queryString() helper.
Implement BooksService.List with pagination support and Get with
proper error handling. Include mock server tests.
This commit is contained in:
Oliver Jakoubek 2026-01-30 09:49:49 +01:00
commit c241399cab
5 changed files with 155 additions and 33 deletions

32
http.go
View file

@ -7,6 +7,8 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strconv"
)
// do executes an authenticated API request and unmarshals the response.
@ -75,6 +77,12 @@ func (c *Client) do(ctx context.Context, method, path string, body, result any)
return nil
}
// listResponse wraps the common Bookstack list API response format.
type listResponse[T any] struct {
Data []T `json:"data"`
Total int `json:"total"`
}
// ListOptions contains common options for list operations.
type ListOptions struct {
Count int // Max items per page (default 100, max 500)
@ -82,3 +90,27 @@ type ListOptions struct {
Sort string // Sort field (e.g., "name", "-created_at")
Filter map[string]string // Filters (e.g., {"name": "value"})
}
// queryString builds a URL query string from ListOptions.
func (o *ListOptions) queryString() string {
if o == nil {
return ""
}
v := url.Values{}
if o.Count > 0 {
v.Set("count", strconv.Itoa(o.Count))
}
if o.Offset > 0 {
v.Set("offset", strconv.Itoa(o.Offset))
}
if o.Sort != "" {
v.Set("sort", o.Sort)
}
for key, val := range o.Filter {
v.Set("filter["+key+"]", val)
}
if len(v) == 0 {
return ""
}
return "?" + v.Encode()
}