Skip to content

Commit

Permalink
fix!: do not export client
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Aug 28, 2023
1 parent f05f20e commit 4289b36
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 67 deletions.
32 changes: 13 additions & 19 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
8 changes: 6 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
go.work

.DS_Store
/mock_*.go
2 changes: 2 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
inpackage: True
all: True
21 changes: 4 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 15 additions & 28 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -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, "/")
}
}

0 comments on commit 4289b36

Please sign in to comment.