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:
parent
43b8aac9a5
commit
c241399cab
5 changed files with 155 additions and 33 deletions
32
http.go
32
http.go
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue