Skip to content

Commit 9b16f94

Browse files
committed
create client with options
1 parent ccc3dc1 commit 9b16f94

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ test:
55
.PHONY: lint
66
lint:
77
go vet ./...
8+
9+
.PHONY: format
10+
format:
11+
go fmt ./...

api_client.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
package flareio
2+
3+
type ApiClient struct {
4+
tenantId int
5+
apiKey string
6+
}
7+
8+
type ApiClientOption func(*ApiClient)
9+
10+
func WithTenantId(tenantId int) ApiClientOption {
11+
return func(client *ApiClient) {
12+
client.tenantId = tenantId
13+
}
14+
}
15+
16+
func NewClient(
17+
apiKey string,
18+
optionFns ...ApiClientOption,
19+
) *ApiClient {
20+
c := &ApiClient{
21+
apiKey: apiKey,
22+
}
23+
for _, optionFn := range optionFns {
24+
optionFn(c)
25+
}
26+
return c
27+
}

api_client_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
package flareio
22

3-
import "testing"
3+
import (
4+
"testing"
45

5-
func TestExample(t *testing.T) {
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCreateClientWithApiKey(t *testing.T) {
10+
c := NewClient("test-key")
11+
assert.Equal(t, "test-key", c.apiKey)
12+
assert.Equal(t, 0, c.tenantId)
13+
}
14+
15+
func TestCreateClientWithTenantId(t *testing.T) {
16+
c := NewClient(
17+
"test-key",
18+
WithTenantId(42),
19+
)
20+
assert.Equal(t, "test-key", c.apiKey)
21+
assert.Equal(t, 42, c.tenantId)
622
}

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
module github.com/Flared/go-flareio
22

33
go 1.23.1
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/pmezard/go-difflib v1.0.0 // indirect
8+
github.com/stretchr/testify v1.9.0 // indirect
9+
gopkg.in/yaml.v3 v3.0.1 // indirect
10+
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
9+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)