Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/change search format #31

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/apis/invoice/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/invoice"
)
Expand All @@ -17,8 +18,8 @@ func main() {
client := invoice.NewClient(cfg)

req := invoice.SearchRequest{
Limit: "10",
Offset: "10",
Limit: 10,
Offset: 10,
Filters: map[string]string{
"preapproval_id": "preapproval_id",
},
Expand Down
5 changes: 3 additions & 2 deletions examples/apis/preapprovalplan/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"

"github.com/mercadopago/sdk-go/pkg/preapprovalplan"

"github.com/mercadopago/sdk-go/pkg/config"
Expand All @@ -18,8 +19,8 @@ func main() {
client := preapprovalplan.NewClient(cfg)

filters := preapprovalplan.SearchRequest{
Limit: "10",
Offset: "10",
Limit: 10,
Offset: 10,
Filters: map[string]string{
"status": "active",
},
Expand Down
4 changes: 2 additions & 2 deletions examples/apis/preference/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func main() {
filter := make(map[string]string)
filter["external_reference"] = "wee3rffee23"
filters := preference.SearchRequest{
Limit: "10",
Offset: "10",
Limit: 10,
Offset: 10,
Filters: filter,
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/customer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ func (c *client) Create(ctx context.Context, request Request) (*Response, error)
}

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

requestData := httpclient.RequestData{
QueryParams: request.Filters,
QueryParams: queryParams,
Method: http.MethodGet,
URL: urlSearch,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/customer/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ func TestSearch(t *testing.T) {
Filters: map[string]string{
"EMAIL": "[email protected]",
},
Limit: "10",
Offset: "10",
Limit: 10,
Offset: 10,
},
},
want: &SearchResponse{
Expand Down
45 changes: 19 additions & 26 deletions pkg/customer/search_request.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
package customer

import "strings"
import (
"strconv"
"strings"
)

// SearchRequest is the request to search services.
// SearchRequest is the helper structure to build search request.
// Filters field can receive a lot of parameters. For details, see:
// https://www.mercadopago.com/developers/en/reference/customers/_customers_search/get.
type SearchRequest struct {
Filters map[string]string

Limit string
Offset string
Limit int
Offset int
}

// SetDefaults sets values for limit and offset when not sent.
func (s *SearchRequest) SetDefaults() {
if len(s.Filters) == 0 {
s.Filters = make(map[string]string, 2)
} else {
for k, v := range s.Filters {
delete(s.Filters, k)
s.Filters[strings.ToLower(k)] = v
}
// GetParams creates map to build query parameters. Keys will be changed to lower case.
func (sr *SearchRequest) GetParams() map[string]string {
params := map[string]string{}
for k, v := range sr.Filters {
key := strings.ToLower(k)
params[key] = v
}

if _, ok := s.Filters["limit"]; !ok {
limit := "30"
if s.Limit != "" {
limit = s.Limit
}
s.Filters["limit"] = limit
}
if _, ok := s.Filters["offset"]; !ok {
offset := "0"
if s.Offset != "" {
offset = s.Offset
}
s.Filters["offset"] = offset
if sr.Limit == 0 {
sr.Limit = 30
}
params["limit"] = strconv.Itoa(sr.Limit)
params["offset"] = strconv.Itoa(sr.Offset)

return params
}
4 changes: 2 additions & 2 deletions pkg/invoice/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func (c *client) Get(ctx context.Context, id string) (*Response, error) {
}

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

requestData := httpclient.RequestData{
QueryParams: request.Filters,
QueryParams: queryParameters,
Method: http.MethodGet,
URL: urlSearch,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/invoice/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func TestSearch(t *testing.T) {
args: args{
ctx: context.Background(),
request: SearchRequest{
Limit: "10",
Offset: "10",
Limit: 10,
Offset: 10,
Filters: map[string]string{
"iD": uuid.NewString(),
},
Expand Down
45 changes: 19 additions & 26 deletions pkg/invoice/search_request.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
package invoice

import "strings"
import (
"strconv"
"strings"
)

// SearchRequest contains filters accepted in search.
// SearchRequest is the helper structure to build search request.
type SearchRequest struct {
Filters map[string]string

Limit string
Offset string
Limit int
Offset int
}

// SetDefaults sets values for limit and offset when not sent.
func (s *SearchRequest) SetDefaults() {
if len(s.Filters) == 0 {
s.Filters = make(map[string]string, 2)
} else {
for k, v := range s.Filters {
delete(s.Filters, k)
s.Filters[strings.ToLower(k)] = v
}
// GetParams creates map to build query parameters. Keys will be changed to lower case.
func (sr *SearchRequest) GetParams() map[string]string {
params := map[string]string{}
for k, v := range sr.Filters {
key := strings.ToLower(k)
params[key] = v
}

if _, ok := s.Filters["limit"]; !ok {
limit := "30"
if s.Limit != "" {
limit = s.Limit
}
s.Filters["limit"] = limit
}
if _, ok := s.Filters["offset"]; !ok {
offset := "0"
if s.Offset != "" {
offset = s.Offset
}
s.Filters["offset"] = offset
if sr.Limit == 0 {
sr.Limit = 30
}
params["limit"] = strconv.Itoa(sr.Limit)
params["offset"] = strconv.Itoa(sr.Offset)

return params
}
4 changes: 2 additions & 2 deletions pkg/merchantorder/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ func (c *client) Get(ctx context.Context, id int64) (*Response, error) {
}

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

requestData := httpclient.RequestData{
QueryParams: request.Filters,
QueryParams: queryParams,
Method: http.MethodGet,
URL: urlSearch,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/merchantorder/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ func TestSearch(t *testing.T) {
args: args{
ctx: context.Background(),
request: SearchRequest{
Limit: "1",
Offset: "1",
Limit: 1,
Offset: 1,
Filters: map[string]string{"preference": "134445566"},
},
},
Expand Down
45 changes: 19 additions & 26 deletions pkg/merchantorder/search_request.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
package merchantorder

import "strings"
import (
"strconv"
"strings"
)

// SearchRequest is the request to search services.
// SearchRequest is the helper structure to build search request.
// Filters field can receive a lot of paramaters. For details, see:
// https://www.mercadopago.com/developers/en/reference/merchant_orders/_merchant_orders_search/get.
type SearchRequest struct {
Filters map[string]string

Limit string
Offset string
Limit int
Offset int
}

// SetDefaults sets values for limit and offset when not sent.
func (s *SearchRequest) SetDefaults() {
if len(s.Filters) == 0 {
s.Filters = make(map[string]string, 2)
} else {
for k, v := range s.Filters {
delete(s.Filters, k)
s.Filters[strings.ToLower(k)] = v
}
// GetParams creates map to build query parameters. Keys will be changed to lower case.
func (sr *SearchRequest) GetParams() map[string]string {
params := map[string]string{}
for k, v := range sr.Filters {
key := strings.ToLower(k)
params[key] = v
}

if _, ok := s.Filters["limit"]; !ok {
limit := "30"
if s.Limit != "" {
limit = s.Limit
}
s.Filters["limit"] = limit
}
if _, ok := s.Filters["offset"]; !ok {
offset := "0"
if s.Offset != "" {
offset = s.Offset
}
s.Filters["offset"] = offset
if sr.Limit == 0 {
sr.Limit = 30
}
params["limit"] = strconv.Itoa(sr.Limit)
params["offset"] = strconv.Itoa(sr.Offset)

return params
}
4 changes: 2 additions & 2 deletions pkg/payment/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func (c *client) Create(ctx context.Context, request Request) (*Response, error)
}

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

requestData := httpclient.RequestData{
QueryParams: request.Filters,
QueryParams: queryParams,
Method: http.MethodGet,
URL: urlSearch,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/payment/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func TestSearch(t *testing.T) {
Filters: map[string]string{
"ExTernal_RefeRENCE": uuid.NewString(),
},
Limit: "30",
Offset: "10",
Limit: 30,
Offset: 10,
},
},
want: &SearchResponse{
Expand Down
47 changes: 20 additions & 27 deletions pkg/payment/search_request.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
package payment

import "strings"
import (
"strconv"
"strings"
)

// SearchRequest is the request to search services.
// SearchRequest is the helper structure to build search request.
// Filters field can receive a lot of paramaters. For details, see:
// https://www.mercadopago.com.br/developers/pt/reference/payments/_payments_search/get.
// https://www.mercadopago.com/developers/en/reference/payments/_payments_search/get.
type SearchRequest struct {
Filters map[string]string

Limit string
Offset string
Limit int
Offset int
}

// SetDefaults sets values for limit and offset when not sent.
func (s *SearchRequest) SetDefaults() {
if len(s.Filters) == 0 {
s.Filters = make(map[string]string, 2)
} else {
for k, v := range s.Filters {
delete(s.Filters, k)
s.Filters[strings.ToLower(k)] = v
}
// GetParams creates map to build query parameters. Keys will be changed to lower case.
func (sr *SearchRequest) GetParams() map[string]string {
params := map[string]string{}
for k, v := range sr.Filters {
key := strings.ToLower(k)
params[key] = v
}

if _, ok := s.Filters["limit"]; !ok {
limit := "30"
if s.Limit != "" {
limit = s.Limit
}
s.Filters["limit"] = limit
}
if _, ok := s.Filters["offset"]; !ok {
offset := "0"
if s.Offset != "" {
offset = s.Offset
}
s.Filters["offset"] = offset
if sr.Limit == 0 {
sr.Limit = 30
}
params["limit"] = strconv.Itoa(sr.Limit)
params["offset"] = strconv.Itoa(sr.Offset)

return params
}
4 changes: 2 additions & 2 deletions pkg/preapprovalplan/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ func (c *client) Update(ctx context.Context, request Request, id string) (*Respo
}

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

requestData := httpclient.RequestData{
QueryParams: request.Filters,
QueryParams: queryParams,
Method: http.MethodGet,
URL: urlSearch,
}
Expand Down
Loading
Loading