Skip to content

Commit

Permalink
Merge branch 'main' into feature/payment-methods
Browse files Browse the repository at this point in the history
  • Loading branch information
edmarSoaress committed Jan 31, 2024
2 parents 4c05dfb + 643ac44 commit 6eb7e50
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 118 deletions.
11 changes: 6 additions & 5 deletions examples/apis/paymentmethod/list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
19 changes: 9 additions & 10 deletions examples/custom_config_options/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
14 changes: 7 additions & 7 deletions examples/custom_http_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 7 additions & 5 deletions examples/custom_timeout/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
24 changes: 10 additions & 14 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
37 changes: 37 additions & 0 deletions pkg/config/config_options.go
Original file line number Diff line number Diff line change
@@ -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
}
}
29 changes: 15 additions & 14 deletions pkg/internal/httpclient/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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{
Expand Down
13 changes: 8 additions & 5 deletions pkg/internal/requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"net/http"
"strconv"
"time"

"github.com/mercadopago/sdk-go/pkg/option"
)

var (
Expand All @@ -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{}

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
57 changes: 0 additions & 57 deletions pkg/option/http_options.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 6eb7e50

Please sign in to comment.