Skip to content

Commit

Permalink
Merge pull request #27 from mercadopago/feature/refactor-request-builder
Browse files Browse the repository at this point in the history
Feature/refactor request builder
  • Loading branch information
gdeandradero authored Mar 7, 2024
2 parents 3251ce4 + bbbf552 commit c76f053
Show file tree
Hide file tree
Showing 45 changed files with 897 additions and 1,274 deletions.
2 changes: 1 addition & 1 deletion examples/apis/customer/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {

req := customer.SearchRequest{
Filters: map[string]string{
"email": "{{EMAIL}}",
"EMAIL": "{{EMAIL}}",
},
}

Expand Down
2 changes: 1 addition & 1 deletion examples/apis/point/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
req := point.CreateRequest{
Amount: 1500,
Description: "your payment intent description",
AdditionalInfo: &point.AdditionalInfo{
AdditionalInfo: &point.AdditionalInfoRequest{
PrintOnTerminal: false,
ExternalReference: "4561ads-das4das4-das4754-das456",
},
Expand Down
2 changes: 1 addition & 1 deletion examples/apis/refund/partial_refund/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
partialAmount := pay.TransactionAmount - 10.0

refundClient := refund.NewClient(cfg)
ref, err := refundClient.CreatePartialRefund(context.Background(), partialAmount, pay.ID)
ref, err := refundClient.CreatePartialRefund(context.Background(), pay.ID, partialAmount)
if err != nil {
fmt.Println(err)
return
Expand Down
11 changes: 1 addition & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,4 @@ module github.com/mercadopago/sdk-go

go 1.21.0

require (
github.com/google/uuid v1.5.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require github.com/google/uuid v1.5.0
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 8 additions & 2 deletions pkg/cardtoken/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cardtoken

import (
"context"
"net/http"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/internal/baseclient"
"github.com/mercadopago/sdk-go/pkg/internal/httpclient"
)

const url = "https://api.mercadopago.com/v1/card_tokens"
Expand All @@ -25,7 +26,12 @@ func NewClient(c *config.Config) Client {
}

func (c *client) Create(ctx context.Context, request Request) (*Response, error) {
result, err := baseclient.Post[*Response](ctx, c.cfg, url, request)
requestData := httpclient.RequestData{
Body: request,
Method: http.MethodPost,
URL: url,
}
result, err := httpclient.DoRequest[*Response](ctx, c.cfg, requestData)
if err != nil {
return nil, err
}
Expand Down
55 changes: 43 additions & 12 deletions pkg/cardtoken/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"strings"
"testing"
"time"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/internal/httpclient"
Expand All @@ -35,42 +36,42 @@ func TestCreate(t *testing.T) {
wantErr string
}{
{
name: "should_create_card_token",
name: "should_fail_create_card_token",
fields: fields{
cfg: &config.Config{
Requester: &httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
stringReader := strings.NewReader(string(cardTokenResponse))
stringReadCloser := io.NopCloser(stringReader)
return &http.Response{
Body: stringReadCloser,
}, nil
return nil, fmt.Errorf("some error")
},
},
},
},
args: args{
ctx: context.Background(),
},
want: mockCardToken(),
wantErr: "",
want: nil,
wantErr: "transport level error: some error",
},
{
name: "should_fail_create_card_token",
name: "should_create_card_token",
fields: fields{
cfg: &config.Config{
Requester: &httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("some error")
stringReader := strings.NewReader(string(cardTokenResponse))
stringReadCloser := io.NopCloser(stringReader)
return &http.Response{
Body: stringReadCloser,
}, nil
},
},
},
},
args: args{
ctx: context.Background(),
},
want: nil,
wantErr: "transport level error: some error",
want: mockCardToken(),
wantErr: "",
},
}
for _, tt := range tests {
Expand All @@ -93,3 +94,33 @@ func TestCreate(t *testing.T) {
})
}
}

func mockCardToken() *Response {
return &Response{
ID: "3d40b34eb41a6d0923e5bc545927c2e9",
FirstSixDigits: "503143",
ExpirationMonth: 11,
ExpirationYear: 2025,
LastFourDigits: "6351",
Cardholder: CardholderResponse{
Identification: IdentificationResponse{
Number: "70383868084",
Type: "CPF",
},
Name: "MASTER TEST",
},
Status: "active",
DateCreated: parseDate("2024-02-08T09:05:42.725-04:00"),
DateLastUpdated: parseDate("2024-02-08T09:05:42.725-04:00"),
DateDue: parseDate("2024-02-16T09:05:42.725-04:00"),
LuhnValidation: true,
LiveMode: false,
CardNumberLength: 16,
SecurityCodeLength: 3,
}
}

func parseDate(s string) *time.Time {
d, _ := time.Parse(time.RFC3339, s)
return &d
}
52 changes: 0 additions & 52 deletions pkg/cardtoken/mock.go

This file was deleted.

49 changes: 33 additions & 16 deletions pkg/customer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package customer

import (
"context"
"fmt"
"net/url"
"net/http"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/internal/baseclient"
"github.com/mercadopago/sdk-go/pkg/internal/httpclient"
)

const (
urlBase = "https://api.mercadopago.com/v1/customers"
urlSearch = urlBase + "/search"
urlWithID = urlBase + "/:id"
urlWithID = urlBase + "/{id}"
)

// Client contains the methods to interact with the Customers API.
Expand All @@ -21,14 +20,17 @@ type Client interface {
// It is a post request to the endpoint: https://api.mercadopago.com/v1/customers
// Reference: https://www.mercadopago.com/developers/en/reference/customers/_customers/post/
Create(ctx context.Context, request Request) (*Response, error)

// Search find all customer information using specific filters.
// It is a get request to the endpoint: https://api.mercadopago.com/v1/customers/search
// Reference: https://www.mercadopago.com/developers/en/reference/customers/_customers_search/get/
Search(ctx context.Context, request SearchRequest) (*SearchResponse, error)

// Get check all the information of a client created with the client ID of your choice.
// It is a get request to the endpoint: https://api.mercadopago.com/v1/customers/{id}
// Reference: https://www.mercadopago.com/developers/en/reference/customers/_customers_id/get/
Get(ctx context.Context, id string) (*Response, error)

// Update renew the data of a customer.
// It is a put request to the endpoint: https://api.mercadopago.com/v1/customers
// Reference: https://www.mercadopago.com/developers/en/reference/customers/_customers_id/put/
Expand All @@ -48,7 +50,12 @@ func NewClient(c *config.Config) Client {
}

func (c *client) Create(ctx context.Context, request Request) (*Response, error) {
result, err := baseclient.Post[*Response](ctx, c.cfg, urlBase, request)
requestData := httpclient.RequestData{
Body: request,
Method: http.MethodPost,
URL: urlBase,
}
result, err := httpclient.DoRequest[*Response](ctx, c.cfg, requestData)
if err != nil {
return nil, err
}
Expand All @@ -57,15 +64,14 @@ func (c *client) Create(ctx context.Context, request Request) (*Response, error)
}

func (c *client) Search(ctx context.Context, request SearchRequest) (*SearchResponse, error) {
params := request.Parameters()
request.SetDefaults()

parsedURL, err := url.Parse(urlSearch)
if err != nil {
return nil, fmt.Errorf("error parsing url: %w", err)
requestData := httpclient.RequestData{
QueryParams: request.Filters,
Method: http.MethodGet,
URL: urlSearch,
}
parsedURL.RawQuery = params

result, err := baseclient.Get[*SearchResponse](ctx, c.cfg, parsedURL.String())
result, err := httpclient.DoRequest[*SearchResponse](ctx, c.cfg, requestData)
if err != nil {
return nil, err
}
Expand All @@ -74,11 +80,16 @@ func (c *client) Search(ctx context.Context, request SearchRequest) (*SearchResp
}

func (c *client) Get(ctx context.Context, id string) (*Response, error) {
params := map[string]string{
pathParams := map[string]string{
"id": id,
}

result, err := baseclient.Get[*Response](ctx, c.cfg, urlWithID, baseclient.WithPathParams(params))
requestData := httpclient.RequestData{
PathParams: pathParams,
Method: http.MethodGet,
URL: urlWithID,
}
result, err := httpclient.DoRequest[*Response](ctx, c.cfg, requestData)
if err != nil {
return nil, err
}
Expand All @@ -87,11 +98,17 @@ func (c *client) Get(ctx context.Context, id string) (*Response, error) {
}

func (c *client) Update(ctx context.Context, id string, request Request) (*Response, error) {
params := map[string]string{
pathParams := map[string]string{
"id": id,
}

result, err := baseclient.Put[*Response](ctx, c.cfg, urlWithID, request, baseclient.WithPathParams(params))
requestData := httpclient.RequestData{
Body: request,
PathParams: pathParams,
Method: http.MethodPut,
URL: urlWithID,
}
result, err := httpclient.DoRequest[*Response](ctx, c.cfg, requestData)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit c76f053

Please sign in to comment.