diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 2f92b19..2536fb1 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,27 +1,21 @@ // For format details, see https://aka.ms/devcontainer.json. For config options, see the // README at: https://github.com/devcontainers/templates/tree/main/src/go { - "name": "clearbank-api-client-go", - // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile - "image": "mcr.microsoft.com/devcontainers/go:0-1.19", - - // Configure tool-specific properties. - "customizations": { - "vscode": { - "extensions": [ - "golang.Go", - "wayou.vscode-todo-highlight" - ] - } - }, - - // Features to add to the dev container. More info: https://containers.dev/features. + "name": "clearbank-api-client-go", + "image": "mcr.microsoft.com/devcontainers/go:1-1.21-bookworm", "features": { - "ghcr.io/brokeyourbike/devcontainer-features/mockery-go:0": { - "version": "2.14.0" - }, "ghcr.io/brokeyourbike/devcontainer-features/staticcheck:0": { - "version": "2023.1" + "version": "2023.1.5" + }, + "ghcr.io/brokeyourbike/devcontainer-features/mockery-go:0": { + "version": "2.33.0" + } + }, + "customizations": { + "vscode": { + "extensions": [ + "golang.Go" + ] } } } diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1ec7ea2..c887f30 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -16,8 +16,12 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-go@v4 with: - go-version-file: "go.mod" + go-version-file: 'go.mod' + - uses: brokeyourbike/go-mockery-action@v0.1 + with: + mockery-version: '2.33.0' + - run: mockery --quiet - uses: dominikh/staticcheck-action@v1 with: - version: "2023.1" + version: '2023.1.5' install-go: false \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8e39cf6..b4582c5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,7 +16,11 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-go@v4 with: - go-version-file: "go.mod" + go-version-file: 'go.mod' + - uses: brokeyourbike/go-mockery-action@v0.1 + with: + mockery-version: '2.33.0' + - run: mockery --quiet - run: go build -v ./... - run: go test -race -covermode=atomic -coverprofile=coverage.out -v ./... diff --git a/.gitignore b/.gitignore index c03571e..0e70c59 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ go.work .DS_Store +/mock_*.go diff --git a/.mockery.yaml b/.mockery.yaml new file mode 100644 index 0000000..3228ebe --- /dev/null +++ b/.mockery.yaml @@ -0,0 +1,2 @@ +inpackage: True +all: True \ No newline at end of file diff --git a/README.md b/README.md index 2a89624..ab6603b 100644 --- a/README.md +++ b/README.md @@ -13,26 +13,13 @@ ClearBank API Client for Go go get github.com/brokeyourbike/clearbank-api-client-go ``` -## Example +## Usage ```go -package main +client := clearbank.NewClient("token", signer) -import ( - "context" - - "github.com/brokeyourbike/clearbank-api-client-go" - "github.com/stretchr/testify/assert" -) - -func main() { - client := clearbank.NewClient("token", signer, clearbank.WithBaseURL("https://api.clear.bank")) - - ctx := context.Background() - - err := client.Test(clearbank.RequestIdContext(ctx, "123"), "hello") - assert.NoError(t, err) -} +err := client.Test(clearbank.RequestIdContext(context.TODO(), "123"), "hello") +require.NoError(t, err) ``` ## Authors diff --git a/client.go b/client.go index 25f233c..50da966 100644 --- a/client.go +++ b/client.go @@ -7,33 +7,34 @@ import ( "fmt" "io" "net/http" - "strings" ) -const ( - defaultBaseURL = "https://institution-api-sim.clearbank.co.uk" -) +const defaultBaseURL = "https://institution-api-sim.clearbank.co.uk" // requestIdCtx is the context key for the request ID. type requestIdCtx struct{} +type HttpClient interface { + Do(req *http.Request) (*http.Response, error) +} + // Client is a ClearBank API client. -type Client struct { +type client struct { + httpClient HttpClient + signer Signer baseURL string token string - httpClient *http.Client - signer Signer } // ClientOption is a function that configures a Client. -type ClientOption func(*Client) +type ClientOption func(*client) -func NewClient(token string, signer Signer, options ...ClientOption) *Client { - c := &Client{ - baseURL: defaultBaseURL, - token: token, +func NewClient(token string, signer Signer, options ...ClientOption) *client { + c := &client{ httpClient: http.DefaultClient, signer: signer, + baseURL: defaultBaseURL, + token: token, } for _, option := range options { @@ -43,21 +44,7 @@ func NewClient(token string, signer Signer, options ...ClientOption) *Client { return c } -// WithHTTPClient sets the HTTP client for the ClearBank API client. -func WithHTTPClient(c *http.Client) ClientOption { - return func(client *Client) { - client.httpClient = c - } -} - -// WithBaseURL sets the base URL for the ClearBank API client. -func WithBaseURL(baseURL string) ClientOption { - return func(client *Client) { - client.baseURL = strings.TrimSuffix(baseURL, "/") - } -} - -func (c *Client) NewRequest(ctx context.Context, method, url string, body interface{}) (*http.Request, error) { +func (c *client) NewRequest(ctx context.Context, method, url string, body interface{}) (*http.Request, error) { req, err := http.NewRequestWithContext(ctx, method, c.baseURL+url, nil) if err != nil { return nil, fmt.Errorf("failed to create request: %w", err) @@ -85,7 +72,7 @@ func (c *Client) NewRequest(ctx context.Context, method, url string, body interf return req, nil } -func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) { +func (c *client) Do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) { resp, err := c.httpClient.Do(req) if err != nil { return resp, fmt.Errorf("failed to send request: %w", err) diff --git a/options.go b/options.go new file mode 100644 index 0000000..f46be1c --- /dev/null +++ b/options.go @@ -0,0 +1,19 @@ +package clearbank + +import ( + "strings" +) + +// WithHTTPClient sets the HTTP client for the ClearBank API client. +func WithHTTPClient(c HttpClient) ClientOption { + return func(target *client) { + target.httpClient = c + } +} + +// WithBaseURL sets the base URL for the ClearBank API client. +func WithBaseURL(baseURL string) ClientOption { + return func(target *client) { + target.baseURL = strings.TrimSuffix(baseURL, "/") + } +}