From 034c26d8deefae42c68ed037c0e93c79dba630cd Mon Sep 17 00:00:00 2001 From: edmarSoaress Date: Wed, 31 Jan 2024 16:18:05 -0300 Subject: [PATCH] add user client api --- examples/apis/user/get/main.go | 28 ++++++ pkg/user/response.go | 11 ++ pkg/user/user.go | 51 ++++++++++ pkg/user/user_test.go | 134 +++++++++++++++++++++++++ resources/mocks/user/get_response.json | 11 ++ test/integration/user/user_test.go | 29 ++++++ 6 files changed, 264 insertions(+) create mode 100644 examples/apis/user/get/main.go create mode 100644 pkg/user/response.go create mode 100644 pkg/user/user.go create mode 100644 pkg/user/user_test.go create mode 100644 resources/mocks/user/get_response.json create mode 100644 test/integration/user/user_test.go diff --git a/examples/apis/user/get/main.go b/examples/apis/user/get/main.go new file mode 100644 index 00000000..fec0b150 --- /dev/null +++ b/examples/apis/user/get/main.go @@ -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) +} diff --git a/pkg/user/response.go b/pkg/user/response.go new file mode 100644 index 00000000..fd013044 --- /dev/null +++ b/pkg/user/response.go @@ -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"` +} diff --git a/pkg/user/user.go b/pkg/user/user.go new file mode 100644 index 00000000..75e6308e --- /dev/null +++ b/pkg/user/user.go @@ -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 +} diff --git a/pkg/user/user_test.go b/pkg/user/user_test.go new file mode 100644 index 00000000..14aca159 --- /dev/null +++ b/pkg/user/user_test.go @@ -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: "test_user_658045679@testuser.com", + 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) + } + }) + } +} diff --git a/resources/mocks/user/get_response.json b/resources/mocks/user/get_response.json new file mode 100644 index 00000000..856006f0 --- /dev/null +++ b/resources/mocks/user/get_response.json @@ -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": "test_user_658045679@testuser.com", + "site_id": "MLB" +} \ No newline at end of file diff --git a/test/integration/user/user_test.go b/test/integration/user/user_test.go new file mode 100644 index 00000000..8ee2ee38 --- /dev/null +++ b/test/integration/user/user_test.go @@ -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()) + } + }) +}