2026-01-30 09:36:58 +01:00
|
|
|
package bookstack
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-30 09:48:01 +01:00
|
|
|
"bytes"
|
2026-01-30 09:36:58 +01:00
|
|
|
"context"
|
2026-01-30 09:48:01 +01:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2026-01-30 09:36:58 +01:00
|
|
|
"net/http"
|
2026-01-30 09:49:49 +01:00
|
|
|
"net/url"
|
|
|
|
|
"strconv"
|
2026-01-30 09:36:58 +01:00
|
|
|
)
|
|
|
|
|
|
2026-01-30 09:48:01 +01:00
|
|
|
// do executes an authenticated API request and unmarshals the response.
|
|
|
|
|
// method is the HTTP method, path is appended to BaseURL (e.g., "/api/books"),
|
|
|
|
|
// body is JSON-encoded as the request body (nil for no body),
|
|
|
|
|
// and result is the target for JSON unmarshaling (nil to discard response body).
|
|
|
|
|
func (c *Client) do(ctx context.Context, method, path string, body, result any) error {
|
|
|
|
|
var bodyReader io.Reader
|
|
|
|
|
if body != nil {
|
|
|
|
|
data, err := json.Marshal(body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("marshaling request body: %w", err)
|
|
|
|
|
}
|
|
|
|
|
bodyReader = bytes.NewReader(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, bodyReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("creating request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Token %s:%s", c.tokenID, c.tokenSecret))
|
|
|
|
|
if body != nil {
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
|
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("executing request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("reading response body: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
|
|
|
apiErr := &APIError{
|
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
|
Body: string(respBody),
|
|
|
|
|
}
|
|
|
|
|
// Try to parse error details from JSON response
|
|
|
|
|
var errResp struct {
|
|
|
|
|
Error struct {
|
|
|
|
|
Code string `json:"code"`
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
} `json:"error"`
|
|
|
|
|
}
|
|
|
|
|
if json.Unmarshal(respBody, &errResp) == nil && errResp.Error.Message != "" {
|
|
|
|
|
apiErr.Code = errResp.Error.Code
|
|
|
|
|
apiErr.Message = errResp.Error.Message
|
|
|
|
|
} else {
|
|
|
|
|
apiErr.Message = http.StatusText(resp.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
return apiErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result != nil && len(respBody) > 0 {
|
|
|
|
|
if err := json.Unmarshal(respBody, result); err != nil {
|
|
|
|
|
return fmt.Errorf("unmarshaling response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-30 09:36:58 +01:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 09:52:10 +01:00
|
|
|
// doRaw executes an authenticated API request and returns the raw response body.
|
|
|
|
|
// Used for export endpoints that return non-JSON content (markdown, PDF, etc.).
|
|
|
|
|
func (c *Client) doRaw(ctx context.Context, method, path string) ([]byte, error) {
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("creating request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Token %s:%s", c.tokenID, c.tokenSecret))
|
|
|
|
|
|
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("executing request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("reading response body: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
|
|
|
apiErr := &APIError{
|
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
|
Body: string(body),
|
|
|
|
|
Message: http.StatusText(resp.StatusCode),
|
|
|
|
|
}
|
|
|
|
|
return nil, apiErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return body, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 09:49:49 +01:00
|
|
|
// listResponse wraps the common Bookstack list API response format.
|
|
|
|
|
type listResponse[T any] struct {
|
|
|
|
|
Data []T `json:"data"`
|
|
|
|
|
Total int `json:"total"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 09:36:58 +01:00
|
|
|
// ListOptions contains common options for list operations.
|
|
|
|
|
type ListOptions struct {
|
|
|
|
|
Count int // Max items per page (default 100, max 500)
|
|
|
|
|
Offset int // Offset for pagination
|
|
|
|
|
Sort string // Sort field (e.g., "name", "-created_at")
|
|
|
|
|
Filter map[string]string // Filters (e.g., {"name": "value"})
|
|
|
|
|
}
|
2026-01-30 09:49:49 +01:00
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
}
|