Files
bridge/internal/metal/providers/emgql/options.go

63 lines
1.1 KiB
Go

package emgql
import (
"net/http"
"net/url"
"go.uber.org/zap"
)
// Option is a Client configuration Option definition.
type Option func(c *Client) error
// WithLogger sets the logger for the client.
func WithLogger(logger *zap.SugaredLogger) Option {
return func(c *Client) error {
c.logger = logger
return nil
}
}
// WithBaseURL updates the baseurl used by the client.
func WithBaseURL(baseURL string) Option {
return func(c *Client) error {
u, err := url.Parse(baseURL)
if err != nil {
return err
}
c.baseURL = u
return nil
}
}
// WithHTTPClient sets the http client to be used by the client.
func WithHTTPClient(httpClient *http.Client) Option {
return func(c *Client) error {
c.httpClient = httpClient
return nil
}
}
// WithConfig applies all configurations defined in the config.
func WithConfig(config Config) Option {
return func(c *Client) error {
var options []Option
if config.BaseURL != "" {
options = append(options, WithBaseURL(config.BaseURL))
}
for _, opt := range options {
if err := opt(c); err != nil {
return err
}
}
return nil
}
}