Skip to content

Commit

Permalink
feat: statement and validate err
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Aug 29, 2023
1 parent 65e1dd6 commit 6528561
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 4 deletions.
7 changes: 5 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"slices"

"github.com/brokeyourbike/clearbank-api-client-go/signature"
"github.com/go-playground/validator/v10"
"github.com/sirupsen/logrus"
)

Expand All @@ -25,6 +26,7 @@ type HttpClient interface {
type Client interface {
TestClient
RateClient
StatementClient
}

var _ Client = (*client)(nil)
Expand All @@ -33,6 +35,7 @@ type client struct {
httpClient HttpClient
signer signature.Signer
logger *logrus.Logger
validate *validator.Validate
baseURL string
token string
}
Expand All @@ -44,6 +47,7 @@ func NewClient(token string, signer signature.Signer, options ...ClientOption) *
c := &client{
httpClient: http.DefaultClient,
signer: signer,
validate: validator.New(validator.WithRequiredStructEnabled()),
baseURL: defaultBaseURL,
token: token,
}
Expand Down Expand Up @@ -124,8 +128,7 @@ func (c *client) do(ctx context.Context, req *request) error {
return unexpectedResponse
}

// TODO: maybe we should setup validator to ensure errResponse is not empty
if errResponse.Status == 0 {
if err := c.validate.Struct(errResponse); err != nil {
return unexpectedResponse
}

Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
cloud.google.com/go/kms v1.15.1
github.com/go-playground/validator/v10 v10.15.2
github.com/googleapis/gax-go/v2 v2.12.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
Expand All @@ -12,9 +13,14 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ cloud.google.com/go/kms v1.15.1/go.mod h1:c9J991h5DTl+kg7gi3MYomh12YEENGrf48ee/N
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.15.2 h1:Ra5cll2/eF8X0Ff2+8SMD7euo2nenQ8WEpgqfy4NhHU=
github.com/go-playground/validator/v10 v10.15.2/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
Expand All @@ -11,6 +21,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56etFpas=
github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
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/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand All @@ -26,6 +38,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (r UnexpectedResponse) Error() string {
type ErrResponse struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Status int `json:"status" validate:"required"`
Details string `json:"detail"`
Instance string `json:"instance"`
Errors map[string][]string `json:"errors"`
Expand Down
39 changes: 39 additions & 0 deletions statement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package clearbank

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

type StatementClient interface {
RequestStatement(context.Context, StatementPayload) error
RequestStatementFor(context.Context, string, StatementPayload) error
}

type StatementPayload struct {
Year int `json:"year"`
Month int `json:"month"`
Format string `json:"format"`
Currency string `json:"currency"`
}

func (c *client) RequestStatement(ctx context.Context, payload StatementPayload) error {
req, err := c.newRequest(ctx, http.MethodPost, "/mccy/v1/StatementRequests", payload)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}

req.ExpectStatus(http.StatusAccepted)
return c.do(ctx, req)
}

func (c *client) RequestStatementFor(ctx context.Context, iban string, payload StatementPayload) error {
req, err := c.newRequest(ctx, http.MethodPost, fmt.Sprintf("/mccy/v1/StatementRequests/account/%s", iban), payload)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}

req.ExpectStatus(http.StatusAccepted)
return c.do(ctx, req)
}
43 changes: 43 additions & 0 deletions statement_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package clearbank_test

import (
"bytes"
"context"
_ "embed"
"io"
"net/http"
"testing"

"github.com/brokeyourbike/clearbank-api-client-go"
"github.com/brokeyourbike/clearbank-api-client-go/signature"
"github.com/stretchr/testify/assert"
mock "github.com/stretchr/testify/mock"
)

func TestRequestStatement(t *testing.T) {
mockSigner := signature.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient))

ctx := clearbank.RequestIdContext(context.TODO(), "123")
mockSigner.On("Sign", ctx, mock.Anything).Return([]byte("signed"), nil).Once()

resp := &http.Response{StatusCode: http.StatusAccepted, Body: io.NopCloser(bytes.NewReader(nil))}
mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(resp, nil).Once()

assert.NoError(t, client.RequestStatement(ctx, clearbank.StatementPayload{}))
}

func TestRequestStatementFor(t *testing.T) {
mockSigner := signature.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient))

ctx := clearbank.RequestIdContext(context.TODO(), "123")
mockSigner.On("Sign", ctx, mock.Anything).Return([]byte("signed"), nil).Once()

resp := &http.Response{StatusCode: http.StatusAccepted, Body: io.NopCloser(bytes.NewReader(nil))}
mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(resp, nil).Once()

assert.NoError(t, client.RequestStatementFor(ctx, "IBAN12345", clearbank.StatementPayload{}))
}
26 changes: 25 additions & 1 deletion test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clearbank_test
import (
"bytes"
"context"
_ "embed"
"errors"
"io"
"net/http"
Expand All @@ -17,6 +18,9 @@ import (
"github.com/stretchr/testify/require"
)

//go:embed testdata/bad-request.json
var badRequest []byte

func TestTest(t *testing.T) {
mockSigner := signature.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
Expand Down Expand Up @@ -57,7 +61,7 @@ func TestFailedSign(t *testing.T) {
assert.Error(t, client.Test(ctx, "hello!"))
}

func TestEnexpectedStatus(t *testing.T) {
func TestUnexpectedStatus(t *testing.T) {
mockSigner := signature.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)

Expand All @@ -73,3 +77,23 @@ func TestEnexpectedStatus(t *testing.T) {
require.Error(t, err)
require.ErrorIs(t, err, clearbank.UnexpectedResponse{Status: 500})
}

func TestBadRequest(t *testing.T) {
mockSigner := signature.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)

client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient))

ctx := context.TODO()
mockSigner.On("Sign", ctx, mock.Anything).Return([]byte("signed"), nil).Once()

resp := &http.Response{StatusCode: 400, Body: io.NopCloser(bytes.NewReader(badRequest))}
mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(resp, nil).Once()

err := client.Test(ctx, "hello!")
require.Error(t, err)

got, ok := err.(clearbank.ErrResponse)
require.True(t, ok)
require.Equal(t, 123, got.Status)
}
9 changes: 9 additions & 0 deletions testdata/bad-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "string",
"title": "string",
"status": 123,
"detail": "string",
"instance": "string",
"property1": null,
"property2": null
}

0 comments on commit 6528561

Please sign in to comment.