Skip to content

Commit 0dc6b5d

Browse files
erkasahidvelji
andauthored
feat(flipt): add WithHTTPClient option for custom HTTP client configuration (#784)
Signed-off-by: Roman Dmytrenko <[email protected]> Co-authored-by: Sahid Velji <[email protected]>
1 parent c1fa178 commit 0dc6b5d

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

providers/flipt/go.sum

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
1919
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
2020
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2121
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
22-
github.com/open-feature/go-sdk v1.15.1 h1:TC3FtHtOKlGlIbSf3SEpxXVhgTd/bCbuc39XHIyltkw=
23-
github.com/open-feature/go-sdk v1.15.1/go.mod h1:2WAFYzt8rLYavcubpCoiym3iSCXiHdPB6DxtMkv2wyo=
2422
github.com/open-feature/go-sdk v1.16.0 h1:5NCHYv5slvNBIZhYXAzAufo0OI59OACZ5tczVqSE+Tg=
2523
github.com/open-feature/go-sdk v1.16.0/go.mod h1:EIF40QcoYT1VbQkMPy2ZJH4kvZeY+qGUXAorzSWgKSo=
2624
github.com/open-feature/go-sdk v1.17.0 h1:/OUBBw5d9D61JaNZZxb2Nnr5/EJrEpjtKCTY3rspJQk=
@@ -55,16 +53,12 @@ go.opentelemetry.io/otel/sdk/metric v1.33.0 h1:Gs5VK9/WUJhNXZgn8MR6ITatvAmKeIuCt
5553
go.opentelemetry.io/otel/sdk/metric v1.33.0/go.mod h1:dL5ykHZmm1B1nVRk9dDjChwDmt81MjVp3gLkQRwKf/Q=
5654
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
5755
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
58-
go.uber.org/mock v0.5.2 h1:LbtPTcP8A5k9WPXj54PPPbjcI4Y6lhyOZXn+VS7wNko=
59-
go.uber.org/mock v0.5.2/go.mod h1:wLlUxC2vVTPTaE3UD51E0BGOAElKrILxhVSDYQLld5o=
6056
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
6157
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
6258
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
6359
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
6460
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
6561
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
66-
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
67-
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
6862
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
6963
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
7064
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=

providers/flipt/pkg/provider/provider.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"net/http"
78
"strconv"
89

910
"github.com/open-feature/go-sdk-contrib/providers/flipt/pkg/service/transport"
@@ -25,11 +26,19 @@ type Config struct {
2526
TokenProvider sdk.ClientTokenProvider
2627
Namespace string
2728
GRPCDialOptions []grpc.DialOption
29+
httpClient *http.Client
2830
}
2931

3032
// Option is a configuration option for the provider.
3133
type Option func(*Provider)
3234

35+
// WithHTTPClient returns an [Option] that specifies the HTTP client to use as the basis of communications.
36+
func WithHTTPClient(client *http.Client) Option {
37+
return func(p *Provider) {
38+
p.config.httpClient = client
39+
}
40+
}
41+
3342
// WithAddress sets the address for the remote Flipt gRPC or HTTP API.
3443
func WithAddress(address string) Option {
3544
return func(p *Provider) {
@@ -86,14 +95,19 @@ func NewProvider(opts ...Option) *Provider {
8695
Address: "http://localhost:8080",
8796
Namespace: "default",
8897
GRPCDialOptions: []grpc.DialOption{},
98+
httpClient: transport.DefaultClient,
8999
}}
90100

91101
for _, opt := range opts {
92102
opt(p)
93103
}
94104

95105
if p.svc == nil {
96-
topts := []transport.Option{transport.WithAddress(p.config.Address), transport.WithCertificatePath(p.config.CertificatePath)}
106+
topts := []transport.Option{
107+
transport.WithAddress(p.config.Address),
108+
transport.WithHTTPClient(p.config.httpClient),
109+
transport.WithCertificatePath(p.config.CertificatePath),
110+
}
97111
if p.config.TokenProvider != nil {
98112
topts = append(topts, transport.WithClientTokenProvider(p.config.TokenProvider))
99113
}

providers/flipt/pkg/provider/provider_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package flipt
33
import (
44
"encoding/json"
55
"errors"
6+
"net/http"
67
"testing"
78

89
of "github.com/open-feature/go-sdk/openfeature"
@@ -672,3 +673,9 @@ func TestObjectEvaluation(t *testing.T) {
672673
})
673674
}
674675
}
676+
677+
func TestWithHTTPClientOption(t *testing.T) {
678+
client := &http.Client{}
679+
p := NewProvider(WithHTTPClient(client))
680+
assert.Equal(t, client, p.config.httpClient)
681+
}

providers/flipt/pkg/service/transport/service.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/x509"
77
"errors"
88
"fmt"
9+
"net/http"
910
"net/url"
1011
"os"
1112
"strings"
@@ -31,6 +32,8 @@ const (
3132
defaultAddr = "http://localhost:8080"
3233
)
3334

35+
var DefaultClient = &http.Client{Transport: http.DefaultTransport}
36+
3437
// Service is a Transport service.
3538
type Service struct {
3639
client offlipt.Client
@@ -40,11 +43,19 @@ type Service struct {
4043
once sync.Once
4144
tokenProvider sdk.ClientTokenProvider
4245
grpcDialOptions []grpc.DialOption
46+
httpClient *http.Client
4347
}
4448

4549
// Option is a service option.
4650
type Option func(*Service)
4751

52+
// WithHTTPClient returns an [Option] that specifies the HTTP client to use as the basis of communications.
53+
func WithHTTPClient(client *http.Client) Option {
54+
return func(s *Service) {
55+
s.httpClient = client
56+
}
57+
}
58+
4859
// WithAddress sets the address for the remote Flipt gRPC API.
4960
func WithAddress(address string) Option {
5061
return func(s *Service) {
@@ -91,6 +102,7 @@ func New(opts ...Option) *Service {
91102
grpcDialOptions: []grpc.DialOption{
92103
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
93104
},
105+
httpClient: DefaultClient,
94106
}
95107

96108
for _, opt := range opts {
@@ -158,7 +170,7 @@ func (s *Service) instance() (offlipt.Client, error) {
158170
opts = append(opts, sdk.WithClientTokenProvider(s.tokenProvider))
159171
}
160172

161-
hclient := sdk.New(sdkhttp.NewTransport(s.address), opts...)
173+
hclient := sdk.New(sdkhttp.NewTransport(s.address, sdkhttp.WithHTTPClient(s.httpClient)), opts...)
162174
if u.Scheme == "https" || u.Scheme == "http" {
163175
s.client = &fclient{
164176
hclient.Flipt(),

providers/flipt/pkg/service/transport/service_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package transport
22

33
import (
4+
"net/http"
45
"testing"
56

67
of "github.com/open-feature/go-sdk/openfeature"
@@ -288,3 +289,9 @@ func TestGRPCToOpenFeatureError(t *testing.T) {
288289
})
289290
}
290291
}
292+
293+
func TestWithHTTPClientOption(t *testing.T) {
294+
client := &http.Client{}
295+
p := New(WithHTTPClient(client))
296+
assert.Equal(t, client, p.httpClient)
297+
}

0 commit comments

Comments
 (0)