Skip to content

Commit

Permalink
Merge pull request #19 from mercadopago/feature/CORECH2-2214-return-e…
Browse files Browse the repository at this point in the history
…rror-on-options

Add changes
  • Loading branch information
gdeandradero authored Feb 27, 2024
2 parents 774820a + 06e80cf commit ed5cfbf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/custom_http_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"fmt"

"github.com/mercadopago/sdk-go/pkg/internal/requester"
)

Expand All @@ -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
Expand Down
25 changes: 16 additions & 9 deletions pkg/config/config_options.go
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit ed5cfbf

Please sign in to comment.