Skip to content

Commit

Permalink
add user client api
Browse files Browse the repository at this point in the history
  • Loading branch information
edmarSoaress authored and eltinMeli committed Feb 9, 2024
1 parent 159ca45 commit 034c26d
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/apis/user/get/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"context"
"fmt"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/user"
)

func main() {
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}

client := user.NewClient(cfg)
user, err := client.Get(context.Background())
if err != nil {
fmt.Println(err)
return
}

fmt.Println(user)
}
11 changes: 11 additions & 0 deletions pkg/user/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package user

type Response struct {
ID int64 `json:"id"`
Nickname string `json:"nickname"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
CountryID string `json:"country_id"`
Email string `json:"email"`
SiteID string `json:"site_id"`
}
51 changes: 51 additions & 0 deletions pkg/user/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package user

import (
"context"
"encoding/json"
"fmt"
"net/http"

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

const url = "https://api.mercadopago.com/users/me"

// Client contains the method to interact with the User API.
type Client interface {
// Get get user information.
// It is a get request to the endpoint: https://api.mercadopago.com/users/me
Get(ctx context.Context) (*Response, error)
}

// client is the implementation of Client.
type client struct {
config *config.Config
}

// NewClient returns a new User API Client.
func NewClient(c *config.Config) Client {
return &client{
config: c,
}
}

func (c *client) Get(ctx context.Context) (*Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}

var formatted Response
if err := json.Unmarshal(res, &formatted); err != nil {
return nil, err
}

return &formatted, nil
}
134 changes: 134 additions & 0 deletions pkg/user/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package user

import (
"context"
"fmt"
"io"
"net/http"
"os"
"reflect"
"strings"
"testing"

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

var (
userResponseJSON, _ = os.Open("../../resources/mocks/user/get_response.json")
userResponse, _ = io.ReadAll(userResponseJSON)
)

func TestGet(t *testing.T) {
type fields struct {
config *config.Config
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
want *Response
wantErr string
}{
{
name: "should_return_error_when_creating_request",
fields: fields{
config: nil,
},
args: args{
ctx: nil,
},
want: nil,
wantErr: "error creating request: net/http: nil Context",
},
{
name: "should_return_error_when_send_request",
fields: fields{
config: &config.Config{
HTTPClient: &httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("some error")
},
},
},
},
args: args{
ctx: context.Background(),
},
want: nil,
wantErr: "transport level error: some error",
},
{
name: "should_return_error_unmarshal_response",
fields: fields{
config: &config.Config{
HTTPClient: &httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
stringReader := strings.NewReader("invalid json")
stringReadCloser := io.NopCloser(stringReader)
return &http.Response{
Body: stringReadCloser,
}, nil
},
},
},
},
args: args{
ctx: context.Background(),
},
want: nil,
wantErr: "invalid character 'i' looking for beginning of value",
},
{
name: "should_return_formatted_response",
fields: fields{
config: &config.Config{
HTTPClient: &httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
stringReader := strings.NewReader(string(userResponse))
stringReadCloser := io.NopCloser(stringReader)
return &http.Response{
Body: stringReadCloser,
}, nil
},
},
},
},
args: args{
ctx: context.Background(),
},
want: &Response{
ID: 1340175910,
Nickname: "TEST_USER_658045679",
FirstName: "Test",
LastName: "Test",
CountryID: "BR",
Email: "[email protected]",
SiteID: "MLB",
},
wantErr: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &client{
config: tt.fields.config,
}
got, err := c.Get(tt.args.ctx)
gotErr := ""
if err != nil {
gotErr = err.Error()
}

if gotErr != tt.wantErr {
t.Errorf("client.Get() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("client.Get() = %v, want %v", got, tt.want)
}
})
}
}
11 changes: 11 additions & 0 deletions resources/mocks/user/get_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": 1340175910,
"nickname": "TEST_USER_658045679",
"registration_date": "2023-03-27T20:26:10.298-04:00",
"first_name": "Test",
"last_name": "Test",
"gender": "",
"country_id": "BR",
"email": "[email protected]",
"site_id": "MLB"
}
29 changes: 29 additions & 0 deletions test/integration/user/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package integration

import (
"context"
"os"
"testing"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/user"
)

func TestUser(t *testing.T) {
t.Run("should_get_user_information", func(t *testing.T) {
c, err := config.New(os.Getenv("at"))
if err != nil {
t.Fatal(err)
}

pmc := user.NewClient(c)
res, err := pmc.Get(context.Background())

if res == nil {
t.Error("res can't be nil")
}
if err != nil {
t.Errorf(err.Error())
}
})
}

0 comments on commit 034c26d

Please sign in to comment.