sendamatic/options.go

55 lines
1.5 KiB
Go
Raw Permalink Normal View History

2025-11-18 17:54:34 +01:00
package sendamatic
import (
"net/http"
"time"
)
// Option is a function type that modifies a Client during initialization.
// Options follow the functional options pattern for configuring client behavior.
2025-11-18 17:54:34 +01:00
type Option func(*Client)
// WithBaseURL returns an Option that sets a custom API base URL for the client.
// Use this to point to a different Sendamatic API endpoint or a testing environment.
//
// Example:
//
// client := sendamatic.NewClient("user", "pass",
// sendamatic.WithBaseURL("https://custom.api.url"))
2025-11-18 17:54:34 +01:00
func WithBaseURL(baseURL string) Option {
return func(c *Client) {
c.baseURL = baseURL
}
}
// WithHTTPClient returns an Option that replaces the default HTTP client with a custom one.
// This allows full control over HTTP behavior such as transport settings, connection pooling,
// and custom middleware.
//
// Example:
//
// customClient := &http.Client{
// Timeout: 60 * time.Second,
// Transport: customTransport,
// }
// client := sendamatic.NewClient("user", "pass",
// sendamatic.WithHTTPClient(customClient))
2025-11-18 17:54:34 +01:00
func WithHTTPClient(client *http.Client) Option {
return func(c *Client) {
c.httpClient = client
}
}
// WithTimeout returns an Option that sets the HTTP client timeout duration.
// This determines how long the client will wait for a response before timing out.
// The default timeout is 30 seconds.
//
// Example:
//
// client := sendamatic.NewClient("user", "pass",
// sendamatic.WithTimeout(60*time.Second))
2025-11-18 17:54:34 +01:00
func WithTimeout(timeout time.Duration) Option {
return func(c *Client) {
c.httpClient.Timeout = timeout
}
}