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
This commit is contained in:
parent
832364a06f
commit
90c48d9323
3 changed files with 141 additions and 1 deletions
70
client.go
70
client.go
|
|
@ -10,4 +10,74 @@
|
|||
// checklists, err := client.Checklists().List(ctx)
|
||||
package checkvist
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// client.go contains the Client struct, constructor, and authentication logic.
|
||||
|
||||
const (
|
||||
// DefaultBaseURL is the default base URL for the Checkvist API.
|
||||
DefaultBaseURL = "https://checkvist.com"
|
||||
// DefaultTimeout is the default timeout for HTTP requests.
|
||||
DefaultTimeout = 30 * time.Second
|
||||
)
|
||||
|
||||
// Client is the Checkvist API client.
|
||||
type Client struct {
|
||||
// baseURL is the base URL for API requests.
|
||||
baseURL string
|
||||
// username is the user's email address.
|
||||
username string
|
||||
// remoteKey is the API key (remote key) for authentication.
|
||||
remoteKey string
|
||||
// token is the current authentication token.
|
||||
token string
|
||||
// tokenExp is the expiration time of the current token.
|
||||
tokenExp time.Time
|
||||
// httpClient is the HTTP client used for requests.
|
||||
httpClient *http.Client
|
||||
// retryConf is the retry configuration for failed requests.
|
||||
retryConf RetryConfig
|
||||
// logger is the logger for debug and error messages.
|
||||
logger *slog.Logger
|
||||
// mu protects token and tokenExp for concurrent access.
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewClient creates a new Checkvist API client.
|
||||
//
|
||||
// The username should be the user's email address, and remoteKey is the API key
|
||||
// which can be obtained from Checkvist settings.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// client := checkvist.NewClient("user@example.com", "your-api-key")
|
||||
//
|
||||
// With options:
|
||||
//
|
||||
// client := checkvist.NewClient("user@example.com", "your-api-key",
|
||||
// checkvist.WithTimeout(60 * time.Second),
|
||||
// checkvist.WithRetryConfig(checkvist.RetryConfig{MaxRetries: 5}),
|
||||
// )
|
||||
func NewClient(username, remoteKey string, opts ...Option) *Client {
|
||||
c := &Client{
|
||||
baseURL: DefaultBaseURL,
|
||||
username: username,
|
||||
remoteKey: remoteKey,
|
||||
httpClient: &http.Client{
|
||||
Timeout: DefaultTimeout,
|
||||
},
|
||||
retryConf: DefaultRetryConfig(),
|
||||
logger: slog.Default(),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue