From 06e80cf895bd3c46da0670f68e49cc24029b66ed Mon Sep 17 00:00:00 2001 From: gabs Date: Mon, 26 Feb 2024 20:14:53 -0300 Subject: [PATCH] Add changes --- examples/custom_http_client/main.go | 2 +- pkg/config/config.go | 6 +++++- pkg/config/config_options.go | 25 ++++++++++++++++--------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/examples/custom_http_client/main.go b/examples/custom_http_client/main.go index f1ef747a..a36257d6 100644 --- a/examples/custom_http_client/main.go +++ b/examples/custom_http_client/main.go @@ -14,7 +14,7 @@ func main() { accessToken := "{{ACCESS_TOKEN}}" httpClient := &http.Client{Timeout: time.Second * 5} - cfg, err := config.New(accessToken, config.WithHTTTPClient(httpClient)) + cfg, err := config.New(accessToken, config.WithHTTPClient(httpClient)) if err != nil { fmt.Println(err) return diff --git a/pkg/config/config.go b/pkg/config/config.go index e524d161..24850e9b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,6 +1,8 @@ package config import ( + "fmt" + "github.com/mercadopago/sdk-go/pkg/internal/requester" ) @@ -22,7 +24,9 @@ func New(accessToken string, opts ...Option) (*Config, error) { // Apply all the functional options to configure the client. for _, opt := range opts { - opt(cfg) + if err := opt(cfg); err != nil { + return nil, fmt.Errorf("fail to build config: %w", err) + } } return cfg, nil diff --git a/pkg/config/config_options.go b/pkg/config/config_options.go index 66462e16..5538a194 100644 --- a/pkg/config/config_options.go +++ b/pkg/config/config_options.go @@ -1,37 +1,44 @@ package config import ( + "fmt" + "github.com/mercadopago/sdk-go/pkg/internal/requester" ) -type Option func(*Config) +type Option func(*Config) error -// WithHTTTPClient allow to do requests using received http client. -func WithHTTTPClient(r requester.Requester) Option { - return func(c *Config) { - if r != nil { - c.Requester = r +// WithHTTPClient allow to do requests using received http client. +func WithHTTPClient(r requester.Requester) Option { + return func(c *Config) error { + if r == nil { + return fmt.Errorf("received http client can't be nil") } + c.Requester = r + return nil } } // WithCorporationID send corporation id to api by headers. func WithCorporationID(value string) Option { - return func(c *Config) { + return func(c *Config) error { c.CorporationID = value + return nil } } // WithIntegratorID send integrator id to api by headers. func WithIntegratorID(i string) Option { - return func(c *Config) { + return func(c *Config) error { c.IntegratorID = i + return nil } } // WithPlatformID send platform id to api by headers. func WithPlatformID(p string) Option { - return func(c *Config) { + return func(c *Config) error { c.PlatformID = p + return nil } }