Skip to content

Commit 3ca01d3

Browse files
authored
feat: add scheme configuration (#159)
Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent 2f3f0b0 commit 3ca01d3

10 files changed

Lines changed: 74 additions & 9 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ coverage:
4646
.PHONY: e2e
4747
e2e:
4848
go test -v -race ./test/e2e/... \
49-
-args -api-key=${API_KEY} -api-key-server=${API_KEY_SERVER} -host=${HOST} -port=${PORT}
49+
-args -api-key=${API_KEY} -api-key-server=${API_KEY_SERVER} -host=${HOST} -port=${PORT} -scheme=${SCHEME}

pkg/bucketeer/api/api.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"bytes"
5+
"crypto/tls"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -20,10 +21,13 @@ const (
2021
)
2122

2223
func (c *client) GetEvaluation(req *model.GetEvaluationRequest) (*model.GetEvaluationResponse, int, error) {
23-
url := fmt.Sprintf("https://%s%s",
24+
url := fmt.Sprintf(
25+
"%s://%s%s",
26+
c.scheme,
2427
c.host,
2528
evaluationAPI,
2629
)
30+
2731
resp, size, err := c.sendHTTPRequest(
2832
url,
2933
req,
@@ -39,7 +43,9 @@ func (c *client) GetEvaluation(req *model.GetEvaluationRequest) (*model.GetEvalu
3943
}
4044

4145
func (c *client) RegisterEvents(req *model.RegisterEventsRequest) (*model.RegisterEventsResponse, int, error) {
42-
url := fmt.Sprintf("https://%s%s",
46+
url := fmt.Sprintf(
47+
"%s://%s%s",
48+
c.scheme,
4349
c.host,
4450
registerEventAPI,
4551
)
@@ -58,7 +64,9 @@ func (c *client) RegisterEvents(req *model.RegisterEventsRequest) (*model.Regist
5864
}
5965

6066
func (c *client) GetFeatureFlags(req *model.GetFeatureFlagsRequest) (*model.GetFeatureFlagsResponse, int, error) {
61-
url := fmt.Sprintf("https://%s%s",
67+
url := fmt.Sprintf(
68+
"%s://%s%s",
69+
c.scheme,
6270
c.host,
6371
featureFlagsAPI,
6472
)
@@ -77,7 +85,9 @@ func (c *client) GetFeatureFlags(req *model.GetFeatureFlagsRequest) (*model.GetF
7785
}
7886

7987
func (c *client) GetSegmentUsers(req *model.GetSegmentUsersRequest) (*model.GetSegmentUsersResponse, int, error) {
80-
url := fmt.Sprintf("https://%s%s",
88+
url := fmt.Sprintf(
89+
"%s://%s%s",
90+
c.scheme,
8191
c.host,
8292
segmentUsersAPI,
8393
)
@@ -95,7 +105,7 @@ func (c *client) GetSegmentUsers(req *model.GetSegmentUsersRequest) (*model.GetS
95105
return &gfr, size, nil
96106
}
97107

98-
func (c *client) sendHTTPRequest(url string, body interface{}) ([]byte, int, error) {
108+
func (c *client) sendHTTPRequest(url string, body any) ([]byte, int, error) {
99109
encoded, err := json.Marshal(body)
100110
if err != nil {
101111
return nil, 0, err
@@ -109,6 +119,13 @@ func (c *client) sendHTTPRequest(url string, body interface{}) ([]byte, int, err
109119
client := &http.Client{
110120
Timeout: 60 * time.Second,
111121
}
122+
if c.scheme == "http" {
123+
client.Transport = &http.Transport{
124+
// This setting is for developing on local machines.
125+
// In production, it's not recommended to specify c.scheme as "http".
126+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec
127+
}
128+
}
112129
resp, err := client.Do(req)
113130
if err != nil {
114131
return nil, 0, err

pkg/bucketeer/api/client.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22
package api
33

44
import (
5+
"errors"
6+
57
"github.com/bucketeer-io/go-server-sdk/pkg/bucketeer/model"
68
)
79

10+
var (
11+
ErrEmptyAPIKey = errors.New("api key must not be empty")
12+
ErrInvalidScheme = errors.New("scheme must be http or https")
13+
ErrEmptyHost = errors.New("host must not be empty")
14+
)
15+
816
// Client is the client interface for the Bucketeer APIGateway service.
917
type Client interface {
1018
GetEvaluation(req *model.GetEvaluationRequest) (*model.GetEvaluationResponse, int, error)
@@ -15,6 +23,7 @@ type Client interface {
1523

1624
type client struct {
1725
apiKey string
26+
scheme string
1827
host string
1928
}
2029

@@ -23,17 +32,36 @@ type ClientConfig struct {
2332
// APIKey is the key to use the Bucketeer APIGateway service.
2433
APIKey string
2534

35+
// Scheme is the scheme of the target service. This must be "http" or "https".
36+
Scheme string
37+
2638
// Host is the host name of the target service, e.g. api.example.com.
2739
Host string
2840
}
2941

42+
// Validate validates the ClientConfig.
43+
func (c *ClientConfig) Validate() error {
44+
if c.APIKey == "" {
45+
return ErrEmptyAPIKey
46+
}
47+
if c.Scheme != "http" && c.Scheme != "https" {
48+
return ErrInvalidScheme
49+
}
50+
if c.Host == "" {
51+
return ErrEmptyHost
52+
}
53+
return nil
54+
}
55+
3056
// NewClient creates a new Client.
31-
//
32-
// NewClient returns error if failed to dial gRPC.
3357
func NewClient(conf *ClientConfig) (Client, error) {
3458
client := &client{
59+
scheme: string(conf.Scheme),
3560
apiKey: conf.APIKey,
3661
host: conf.Host,
3762
}
63+
if err := conf.Validate(); err != nil {
64+
return nil, err
65+
}
3866
return client, nil
3967
}

pkg/bucketeer/option.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type options struct {
1414
cachePollingInterval time.Duration
1515
tag string
1616
apiKey string
17+
scheme string
1718
host string
1819
port int
1920
eventQueueCapacity int
@@ -29,6 +30,7 @@ var defaultOptions = options{
2930
cachePollingInterval: 1 * time.Minute,
3031
tag: "",
3132
apiKey: "",
33+
scheme: "https",
3234
host: "",
3335
port: 443,
3436
eventQueueCapacity: 100_000,
@@ -72,6 +74,13 @@ func WithAPIKey(apiKey string) Option {
7274
}
7375
}
7476

77+
// WithScheme sets scheme to use Bucketeer service. (Default: "https")
78+
func WithScheme(scheme string) Option {
79+
return func(opts *options) {
80+
opts.scheme = scheme
81+
}
82+
}
83+
7584
// WithHost sets host name for the Bucketeer service. (Default: "")
7685
func WithHost(host string) Option {
7786
return func(opts *options) {

pkg/bucketeer/option_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func TestWithOptions(t *testing.T) {
1515
cachePollingInterval := 30 * time.Second
1616
tag := "go-server"
1717
apiKey := "apiKey"
18+
scheme := "http"
1819
host := "host"
1920
port := 8443
2021
eventQueueCapacity := 100
@@ -29,6 +30,7 @@ func TestWithOptions(t *testing.T) {
2930
WithCachePollingInterval(cachePollingInterval),
3031
WithTag(tag),
3132
WithAPIKey(apiKey),
33+
WithScheme(scheme),
3234
WithHost(host),
3335
WithPort(port),
3436
WithEventQueueCapacity(eventQueueCapacity),
@@ -47,6 +49,7 @@ func TestWithOptions(t *testing.T) {
4749
assert.Equal(t, cachePollingInterval, dopts.cachePollingInterval)
4850
assert.Equal(t, tag, dopts.tag)
4951
assert.Equal(t, apiKey, dopts.apiKey)
52+
assert.Equal(t, scheme, dopts.scheme)
5053
assert.Equal(t, host, dopts.host)
5154
assert.Equal(t, port, dopts.port)
5255
assert.Equal(t, eventQueueCapacity, dopts.eventQueueCapacity)

pkg/bucketeer/sdk.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ func NewSDK(ctx context.Context, opts ...Option) (SDK, error) {
147147
ErrorLogger: dopts.errorLogger,
148148
}
149149
loggers := log.NewLoggers(loggerConf)
150-
client, err := api.NewClient(&api.ClientConfig{APIKey: dopts.apiKey, Host: dopts.host})
150+
client, err := api.NewClient(&api.ClientConfig{
151+
APIKey: dopts.apiKey,
152+
Scheme: dopts.scheme,
153+
Host: dopts.host,
154+
})
151155
if err != nil {
152156
return nil, fmt.Errorf("bucketeer: failed to new api client: %w", err)
153157
}

test/e2e/api_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func newAPIClient(t *testing.T, apiKey string) api.Client {
222222
conf := &api.ClientConfig{
223223
APIKey: apiKey,
224224
Host: *host,
225+
Scheme: *scheme,
225226
}
226227
client, err := api.NewClient(conf)
227228
assert.NoError(t, err)

test/e2e/main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ var (
4949
apiKey = flag.String("api-key", "", "API key for the Bucketeer service")
5050
apiKeyServer = flag.String("api-key-server", "", "API key for Server SDK")
5151
host = flag.String("host", "", "Host name of the Bucketeer service, e.g. api-dev.bucketeer.jp")
52+
scheme = flag.String("scheme", "https", "Scheme of the Bucketeer service, e.g. https")
5253
port = flag.Int("port", 443, "Port number of the Bucketeer service, e.g. 443")
5354
)

test/e2e/sdk_local_evaluation_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ func newLocalSDK(t *testing.T, ctx context.Context) bucketeer.SDK {
278278
bucketeer.WithTag(tag),
279279
bucketeer.WithAPIKey(*apiKeyServer),
280280
bucketeer.WithHost(*host),
281+
bucketeer.WithScheme(*scheme),
281282
bucketeer.WithPort(*port),
282283
bucketeer.WithEventQueueCapacity(100),
283284
bucketeer.WithNumEventFlushWorkers(3),

test/e2e/sdk_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ func newSDK(t *testing.T, ctx context.Context) bucketeer.SDK {
597597
bucketeer.WithTag(tag),
598598
bucketeer.WithAPIKey(*apiKey),
599599
bucketeer.WithHost(*host),
600+
bucketeer.WithScheme(*scheme),
600601
bucketeer.WithPort(*port),
601602
bucketeer.WithEventQueueCapacity(100),
602603
bucketeer.WithNumEventFlushWorkers(3),

0 commit comments

Comments
 (0)