Skip to content

Commit

Permalink
feat: client implement all
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Aug 30, 2023
1 parent 8f036b3 commit c621d2b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 44 deletions.
26 changes: 24 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"slices"
"strings"

"github.com/brokeyourbike/clearbank-api-client-go/signature"
"github.com/go-playground/validator/v10"
Expand All @@ -27,9 +28,9 @@ type Client interface {
TestClient
RateClient
StatementClient
// AccountsClient
AccountsClient
TransactionsClient
// MCCYAccountsClient
MCCYAccountsClient
MCCYTransactionsClient
FxClient
}
Expand All @@ -48,6 +49,27 @@ type client struct {
// ClientOption is a function that configures a Client.
type ClientOption func(*client)

// WithHTTPClient sets the HTTP client for the ClearBank API client.
func WithHTTPClient(c HttpClient) ClientOption {
return func(target *client) {
target.httpClient = c
}
}

// WithLogger sets the *logrus.Logger for the ClearBank API client.
func WithLogger(l *logrus.Logger) ClientOption {
return func(target *client) {
target.logger = l
}
}

// WithBaseURL sets the base URL for the ClearBank API client.
func WithBaseURL(baseURL string) ClientOption {
return func(target *client) {
target.baseURL = strings.TrimSuffix(baseURL, "/")
}
}

func NewClient(token string, signer signature.Signer, options ...ClientOption) *client {
c := &client{
httpClient: http.DefaultClient,
Expand Down
28 changes: 0 additions & 28 deletions options.go

This file was deleted.

14 changes: 0 additions & 14 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,6 @@ func TestFailedHttpRequest(t *testing.T) {
assert.Error(t, client.Test(context.TODO(), "hello!"))
}

func TestFailedHttpRequestBodyDecode(t *testing.T) {
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", local.NewNilSigner(), clearbank.WithHTTPClient(mockHttpClient))

body := io.NopCloser(bytes.NewReader(nil))

_, err := io.ReadAll(body)
require.NoError(t, err)

mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{Body: body}, nil).Once()

assert.Error(t, client.Test(context.TODO(), "hello!"))
}

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

0 comments on commit c621d2b

Please sign in to comment.