diff --git a/examples/apis/paymentmethod/list/main.go b/examples/apis/paymentmethod/list/main.go index 1d989fea..46530a2f 100644 --- a/examples/apis/paymentmethod/list/main.go +++ b/examples/apis/paymentmethod/list/main.go @@ -9,21 +9,22 @@ import ( ) func main() { - at := "TEST-640110472259637-071923-a761f639c4eb1f0835ff7611f3248628-793910800" - c, err := config.New(at) + accessToken := "{{ACCESS_TOKEN}}" + + cfg, err := config.New(accessToken) if err != nil { fmt.Println(err) return } - client := paymentmethod.NewClient(c) - result, err := client.List(context.Background()) + client := paymentmethod.NewClient(cfg) + paymentMethods, err := client.List(context.Background()) if err != nil { fmt.Println(err) return } - for _, v := range result { + for _, v := range paymentMethods { fmt.Println(v) } } diff --git a/examples/custom_config_options/main.go b/examples/custom_config_options/main.go index f09740a8..568e2b14 100644 --- a/examples/custom_config_options/main.go +++ b/examples/custom_config_options/main.go @@ -5,32 +5,31 @@ import ( "fmt" "github.com/mercadopago/sdk-go/pkg/config" - "github.com/mercadopago/sdk-go/pkg/option" "github.com/mercadopago/sdk-go/pkg/paymentmethod" ) func main() { - at := "TEST-640110472259637-071923-a761f639c4eb1f0835ff7611f3248628-793910800" + accessToken := "{{ACCESS_TOKEN}}" - c, err := config.New( - at, - option.WithCorporationID("abc"), - option.WithIntegratorID("def"), - option.WithPlatformID("ghi"), + cfg, err := config.New( + accessToken, + config.WithCorporationID("1yuy811998tt11199"), + config.WithIntegratorID("6888999999000001"), + config.WithPlatformID("prd_02647ea11edb6888682a831752a"), ) if err != nil { fmt.Println(err) return } - pmc := paymentmethod.NewClient(c) - res, err := pmc.List(context.Background()) + client := paymentmethod.NewClient(cfg) + paymentMethods, err := client.List(context.Background()) if err != nil { fmt.Println(err) return } - for _, v := range res { + for _, v := range paymentMethods { fmt.Println(v) } } diff --git a/examples/custom_http_client/main.go b/examples/custom_http_client/main.go index 3f0efc03..f1ef747a 100644 --- a/examples/custom_http_client/main.go +++ b/examples/custom_http_client/main.go @@ -7,27 +7,27 @@ import ( "time" "github.com/mercadopago/sdk-go/pkg/config" - "github.com/mercadopago/sdk-go/pkg/option" "github.com/mercadopago/sdk-go/pkg/paymentmethod" ) func main() { - at := "TEST-640110472259637-071923-a761f639c4eb1f0835ff7611f3248628-793910800" - customClient := &http.Client{Timeout: time.Second * 5} - c, err := config.New(at, option.WithCustomClient(customClient)) + accessToken := "{{ACCESS_TOKEN}}" + httpClient := &http.Client{Timeout: time.Second * 5} + + cfg, err := config.New(accessToken, config.WithHTTTPClient(httpClient)) if err != nil { fmt.Println(err) return } - pmc := paymentmethod.NewClient(c) - res, err := pmc.List(context.Background()) + client := paymentmethod.NewClient(cfg) + paymentMethods, err := client.List(context.Background()) if err != nil { fmt.Println(err) return } - for _, v := range res { + for _, v := range paymentMethods { fmt.Println(v) } } diff --git a/examples/custom_timeout/main.go b/examples/custom_timeout/main.go index 0d15f0cf..0f30178b 100644 --- a/examples/custom_timeout/main.go +++ b/examples/custom_timeout/main.go @@ -10,24 +10,26 @@ import ( ) func main() { - at := "TEST-640110472259637-071923-a761f639c4eb1f0835ff7611f3248628-793910800" - c, err := config.New(at) + accessToken := "{{ACCESS_TOKEN}}" + + cfg, err := config.New(accessToken) if err != nil { fmt.Println(err) return } - client := paymentmethod.NewClient(c) + client := paymentmethod.NewClient(cfg) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() - result, err := client.List(ctx) + + paymentMethods, err := client.List(ctx) if err != nil { fmt.Println(err) return } - for _, v := range result { + for _, v := range paymentMethods { fmt.Println(v) } } diff --git a/pkg/config/config.go b/pkg/config/config.go index 117bee85..e524d161 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -2,32 +2,28 @@ package config import ( "github.com/mercadopago/sdk-go/pkg/internal/requester" - "github.com/mercadopago/sdk-go/pkg/option" ) +// Config allows you to send custom settings and API authentication type Config struct { AccessToken string CorporationID string IntegratorID string PlatformID string - - HTTPClient option.Requester + Requester requester.Requester } // New returns a new Config. -func New(at string, opts ...option.ConfigOption) (*Config, error) { - options := option.ConfigOptions{ - HTTPClient: requester.Default(), +func New(accessToken string, opts ...Option) (*Config, error) { + cfg := &Config{ + AccessToken: accessToken, + Requester: requester.Default(), } + + // Apply all the functional options to configure the client. for _, opt := range opts { - opt.Apply(&options) + opt(cfg) } - return &Config{ - AccessToken: at, - CorporationID: options.CorporationID, - IntegratorID: options.IntegratorID, - PlatformID: options.PlatformID, - HTTPClient: options.HTTPClient, - }, nil + return cfg, nil } diff --git a/pkg/config/config_options.go b/pkg/config/config_options.go new file mode 100644 index 00000000..66462e16 --- /dev/null +++ b/pkg/config/config_options.go @@ -0,0 +1,37 @@ +package config + +import ( + "github.com/mercadopago/sdk-go/pkg/internal/requester" +) + +type Option func(*Config) + +// 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 + } + } +} + +// WithCorporationID send corporation id to api by headers. +func WithCorporationID(value string) Option { + return func(c *Config) { + c.CorporationID = value + } +} + +// WithIntegratorID send integrator id to api by headers. +func WithIntegratorID(i string) Option { + return func(c *Config) { + c.IntegratorID = i + } +} + +// WithPlatformID send platform id to api by headers. +func WithPlatformID(p string) Option { + return func(c *Config) { + c.PlatformID = p + } +} diff --git a/pkg/internal/httpclient/gateway.go b/pkg/internal/httpclient/gateway.go index 41023649..6d8b8815 100644 --- a/pkg/internal/httpclient/gateway.go +++ b/pkg/internal/httpclient/gateway.go @@ -9,7 +9,7 @@ import ( "github.com/google/uuid" "github.com/mercadopago/sdk-go/pkg/config" - "github.com/mercadopago/sdk-go/pkg/option" + "github.com/mercadopago/sdk-go/pkg/internal/requester" ) const ( @@ -32,40 +32,41 @@ const ( ) var ( - userAgent string = fmt.Sprintf("MercadoPago Go SDK/%s", currentSDKVersion) - trackingID string = fmt.Sprintf("platform:%s,type:SDK%s,so;", runtime.Version(), currentSDKVersion) + userAgent = fmt.Sprintf("MercadoPago Go SDK/%s", currentSDKVersion) + trackingID = fmt.Sprintf("platform:%s,type:SDK%s,so;", runtime.Version(), currentSDKVersion) ) // Send wraps needed options before send api call. -func Send(ctx context.Context, c *config.Config, req *http.Request) ([]byte, error) { +func Send(ctx context.Context, cfg *config.Config, req *http.Request) ([]byte, error) { req.Header.Set(productIDHeader, productID) req.Header.Set(acceptHeader, accept) req.Header.Set(contentTypeHeader, contentType) req.Header.Set(userAgentHeader, userAgent) req.Header.Set(trackingIDHeader, trackingID) - req.Header.Set(authorizationHeader, "Bearer "+c.AccessToken) + req.Header.Set(authorizationHeader, "Bearer "+cfg.AccessToken) req.Header.Set(idempotencyHeader, uuid.New().String()) - if c.CorporationID != "" { - req.Header.Set(corporationIDHeader, c.CorporationID) + if cfg.CorporationID != "" { + req.Header.Set(corporationIDHeader, cfg.CorporationID) } - if c.IntegratorID != "" { - req.Header.Set(integratorIDHeader, c.IntegratorID) + if cfg.IntegratorID != "" { + req.Header.Set(integratorIDHeader, cfg.IntegratorID) } - if c.PlatformID != "" { - req.Header.Set(platformIDHeader, c.PlatformID) + if cfg.PlatformID != "" { + req.Header.Set(platformIDHeader, cfg.PlatformID) } - return send(ctx, c.HTTPClient, req) + return send(ctx, cfg.Requester, req) } -func send(ctx context.Context, requester option.Requester, req *http.Request) ([]byte, error) { +func send(_ context.Context, requester requester.Requester, req *http.Request) ([]byte, error) { res, err := requester.Do(req) if err != nil { return nil, fmt.Errorf("transport level error: %w", err) } - defer res.Body.Close() + defer func() { _ = res.Body.Close() }() + response, err := io.ReadAll(res.Body) if err != nil { return nil, &ResponseError{ diff --git a/pkg/internal/requester/requester.go b/pkg/internal/requester/requester.go index 67a34f8a..4f0f3d69 100644 --- a/pkg/internal/requester/requester.go +++ b/pkg/internal/requester/requester.go @@ -6,8 +6,6 @@ import ( "net/http" "strconv" "time" - - "github.com/mercadopago/sdk-go/pkg/option" ) var ( @@ -24,6 +22,11 @@ var ( defaultBackoffStrategy = constantBackoff(time.Second * 2) ) +// Requester has the minimum required method to send http requests. +type Requester interface { + Do(req *http.Request) (*http.Response, error) +} + // defaultRequester provides an immutable implementation of option.Requester. type defaultRequester struct{} @@ -33,11 +36,11 @@ type defaultRequester struct{} type backoffFunc func(attempt int) time.Duration // Default return the default implementation of Requester interface. -func Default() option.Requester { +func Default() Requester { return &defaultRequester{} } -// Do sends an HTTP request and returns an HTTP response. It is the default +// Do send an HTTP request and returns an HTTP response. It is the default // implementation of Requester interface. func (d *defaultRequester) Do(req *http.Request) (*http.Response, error) { var resp *http.Response @@ -83,7 +86,7 @@ func (d *defaultRequester) Do(req *http.Request) (*http.Response, error) { // If the request context has a deadline, check whether that deadline // happens before the wait period of the backoff strategy. In case - // it does we return the last error without waiting. + // it do we return the last error without waiting. if deadline, ok := req.Context().Deadline(); ok { ctxDeadline := time.Until(deadline) if ctxDeadline <= backoffWait { diff --git a/pkg/option/http_options.go b/pkg/option/http_options.go deleted file mode 100644 index a65bfb6f..00000000 --- a/pkg/option/http_options.go +++ /dev/null @@ -1,57 +0,0 @@ -package option - -import ( - "net/http" -) - -// Requester has the minimum required method to send http requests. -type Requester interface { - Do(req *http.Request) (*http.Response, error) -} - -type ConfigOptions struct { - CorporationID string - IntegratorID string - PlatformID string - - HTTPClient Requester -} - -// ConfigOption signature for client configurable parameters. -type ConfigOption interface { - Apply(opts *ConfigOptions) -} - -type optFunc func(opts *ConfigOptions) - -func (f optFunc) Apply(o *ConfigOptions) { f(o) } - -// WithCustomClient allow do requests using received http client. -func WithCustomClient(r Requester) ConfigOption { - return optFunc(func(options *ConfigOptions) { - if r != nil { - options.HTTPClient = r - } - }) -} - -// WithCorporationID send corporation id to api by headers. -func WithCorporationID(c string) ConfigOption { - return optFunc(func(options *ConfigOptions) { - options.CorporationID = c - }) -} - -// WithIntegratorID send corporation id to api by headers. -func WithIntegratorID(i string) ConfigOption { - return optFunc(func(options *ConfigOptions) { - options.IntegratorID = i - }) -} - -// WithPlatformID send corporation id to api by headers. -func WithPlatformID(p string) ConfigOption { - return optFunc(func(options *ConfigOptions) { - options.PlatformID = p - }) -} diff --git a/pkg/paymentmethod/payment_method.go b/pkg/paymentmethod/client.go similarity index 91% rename from pkg/paymentmethod/payment_method.go rename to pkg/paymentmethod/client.go index 35623fc4..7b3851b2 100644 --- a/pkg/paymentmethod/payment_method.go +++ b/pkg/paymentmethod/client.go @@ -16,7 +16,7 @@ const url = "https://api.mercadopago.com/v1/payment_methods" type Client interface { // List lists all payment methods. // It is a get request to the endpoint: https://api.mercadopago.com/v1/payment_methods - // Reference: https://www.mercadopago.com.br/developers/pt/reference/payment_methods/_payment_methods/get/ + // Reference: https://www.mercadopago.com/developers/en/reference/payment_methods/_payment_methods/get/ List(ctx context.Context) ([]Response, error) }