2026-01-14 12:42:00 +01:00
|
|
|
package checkvist
|
|
|
|
|
|
Implement Client struct with functional options
Add client.go with:
- Client struct with baseURL, username, remoteKey, token, tokenExp,
httpClient, retryConf, logger, and sync.RWMutex for thread safety
- NewClient constructor accepting username, remoteKey, and variadic options
- Default values: 30s timeout, Checkvist base URL
Add options.go with:
- RetryConfig struct (MaxRetries, BaseDelay, MaxDelay, Jitter)
- DefaultRetryConfig() with 3 retries, 1s base, 30s max, jitter enabled
- Option type for functional options pattern
- WithHTTPClient, WithTimeout, WithRetryConfig, WithLogger, WithBaseURL
Closes checkvist-api-ymg
2026-01-14 13:25:03 +01:00
|
|
|
import (
|
|
|
|
|
"log/slog"
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-14 12:42:00 +01:00
|
|
|
// options.go contains functional options for configuring the Client.
|
Implement Client struct with functional options
Add client.go with:
- Client struct with baseURL, username, remoteKey, token, tokenExp,
httpClient, retryConf, logger, and sync.RWMutex for thread safety
- NewClient constructor accepting username, remoteKey, and variadic options
- Default values: 30s timeout, Checkvist base URL
Add options.go with:
- RetryConfig struct (MaxRetries, BaseDelay, MaxDelay, Jitter)
- DefaultRetryConfig() with 3 retries, 1s base, 30s max, jitter enabled
- Option type for functional options pattern
- WithHTTPClient, WithTimeout, WithRetryConfig, WithLogger, WithBaseURL
Closes checkvist-api-ymg
2026-01-14 13:25:03 +01:00
|
|
|
|
|
|
|
|
// RetryConfig configures the retry behavior for failed requests.
|
|
|
|
|
type RetryConfig struct {
|
|
|
|
|
// MaxRetries is the maximum number of retry attempts.
|
|
|
|
|
MaxRetries int
|
|
|
|
|
// BaseDelay is the initial delay before the first retry.
|
|
|
|
|
BaseDelay time.Duration
|
|
|
|
|
// MaxDelay is the maximum delay between retries.
|
|
|
|
|
MaxDelay time.Duration
|
|
|
|
|
// Jitter enables randomized delay to prevent thundering herd.
|
|
|
|
|
Jitter bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DefaultRetryConfig returns the default retry configuration.
|
|
|
|
|
func DefaultRetryConfig() RetryConfig {
|
|
|
|
|
return RetryConfig{
|
|
|
|
|
MaxRetries: 3,
|
|
|
|
|
BaseDelay: 1 * time.Second,
|
|
|
|
|
MaxDelay: 30 * time.Second,
|
|
|
|
|
Jitter: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Option is a functional option for configuring the Client.
|
|
|
|
|
type Option func(*Client)
|
|
|
|
|
|
|
|
|
|
// WithHTTPClient sets a custom HTTP client for the Checkvist client.
|
|
|
|
|
func WithHTTPClient(client *http.Client) Option {
|
|
|
|
|
return func(c *Client) {
|
|
|
|
|
c.httpClient = client
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithTimeout sets the timeout for HTTP requests.
|
|
|
|
|
// This creates a new HTTP client with the specified timeout.
|
|
|
|
|
func WithTimeout(timeout time.Duration) Option {
|
|
|
|
|
return func(c *Client) {
|
|
|
|
|
c.httpClient = &http.Client{
|
|
|
|
|
Timeout: timeout,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithRetryConfig sets the retry configuration for failed requests.
|
|
|
|
|
func WithRetryConfig(config RetryConfig) Option {
|
|
|
|
|
return func(c *Client) {
|
|
|
|
|
c.retryConf = config
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithLogger sets a custom logger for the client.
|
|
|
|
|
func WithLogger(logger *slog.Logger) Option {
|
|
|
|
|
return func(c *Client) {
|
|
|
|
|
c.logger = logger
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithBaseURL sets a custom base URL for the API.
|
|
|
|
|
// This is primarily useful for testing.
|
|
|
|
|
func WithBaseURL(url string) Option {
|
|
|
|
|
return func(c *Client) {
|
|
|
|
|
c.baseURL = url
|
|
|
|
|
}
|
|
|
|
|
}
|