Complete Client struct with fluent configuration

Add full client configuration with fluent builder pattern:

- WithTimeout(duration) - configurable request timeout
- WithLogger(slog.Logger) - optional debug logging
- DefaultTimeout constant (30 seconds)
- Default HTTP client with timeout on construction

All fluent methods return same client instance for chaining.
Client is thread-safe for concurrent use.

Closes: kanboard-api-uls
This commit is contained in:
Oliver Jakoubek 2026-01-15 18:14:22 +01:00
commit a56456cc00
3 changed files with 204 additions and 4 deletions

View file

@ -1,16 +1,23 @@
package kanboard
import (
"log/slog"
"net/http"
"strings"
"time"
)
// DefaultTimeout is the default HTTP timeout for API requests.
const DefaultTimeout = 30 * time.Second
// Client is the Kanboard API client.
// It is safe for concurrent use by multiple goroutines.
type Client struct {
baseURL string
endpoint string
httpClient *http.Client
auth Authenticator
logger *slog.Logger
}
// NewClient creates a new Kanboard API client.
@ -22,9 +29,11 @@ func NewClient(baseURL string) *Client {
baseURL = strings.TrimSuffix(baseURL, "/")
return &Client{
baseURL: baseURL,
endpoint: baseURL + "/jsonrpc.php",
httpClient: http.DefaultClient,
baseURL: baseURL,
endpoint: baseURL + "/jsonrpc.php",
httpClient: &http.Client{
Timeout: DefaultTimeout,
},
}
}
@ -41,7 +50,25 @@ func (c *Client) WithBasicAuth(username, password string) *Client {
}
// WithHTTPClient sets a custom HTTP client.
// This replaces the default client entirely, including any timeout settings.
func (c *Client) WithHTTPClient(client *http.Client) *Client {
c.httpClient = client
return c
}
// WithTimeout sets the HTTP client timeout.
// This creates a new HTTP client with the specified timeout.
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.httpClient = &http.Client{
Timeout: timeout,
Transport: c.httpClient.Transport,
}
return c
}
// WithLogger sets the logger for debug output.
// If set, the client will log request/response details at debug level.
func (c *Client) WithLogger(logger *slog.Logger) *Client {
c.logger = logger
return c
}