Skip to content

Commit de00f07

Browse files
committed
feat(flipt): add WithHTTPClient option for custom HTTP client configuration
Add WithHTTPClient option to both provider and transport layers to allow users to configure custom HTTP clients. This enables use cases like custom timeouts, proxies, or other HTTP client customizations. The option defaults to http.DefaultClient when not specified. Signed-off-by: Roman Dmytrenko <[email protected]>
1 parent 5f29be0 commit de00f07

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-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/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
@@ -51,16 +49,12 @@ go.opentelemetry.io/otel/sdk/metric v1.33.0 h1:Gs5VK9/WUJhNXZgn8MR6ITatvAmKeIuCt
5149
go.opentelemetry.io/otel/sdk/metric v1.33.0/go.mod h1:dL5ykHZmm1B1nVRk9dDjChwDmt81MjVp3gLkQRwKf/Q=
5250
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
5351
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
54-
go.uber.org/mock v0.5.2 h1:LbtPTcP8A5k9WPXj54PPPbjcI4Y6lhyOZXn+VS7wNko=
55-
go.uber.org/mock v0.5.2/go.mod h1:wLlUxC2vVTPTaE3UD51E0BGOAElKrILxhVSDYQLld5o=
5652
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
5753
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
5854
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
5955
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
6056
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
6157
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
62-
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
63-
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
6458
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
6559
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
6660
google.golang.org/genproto/googleapis/api v0.0.0-20241230172942-26aa7a208def h1:0Km0hi+g2KXbXL0+riZzSCKz23f4MmwicuEb00JeonI=

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: http.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: 11 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"
@@ -40,11 +41,19 @@ type Service struct {
4041
once sync.Once
4142
tokenProvider sdk.ClientTokenProvider
4243
grpcDialOptions []grpc.DialOption
44+
httpClient *http.Client
4345
}
4446

4547
// Option is a service option.
4648
type Option func(*Service)
4749

50+
// WithHTTPClient returns an [Option] that specifies the HTTP client to use as the basis of communications.
51+
func WithHTTPClient(client *http.Client) Option {
52+
return func(s *Service) {
53+
s.httpClient = client
54+
}
55+
}
56+
4857
// WithAddress sets the address for the remote Flipt gRPC API.
4958
func WithAddress(address string) Option {
5059
return func(s *Service) {
@@ -91,6 +100,7 @@ func New(opts ...Option) *Service {
91100
grpcDialOptions: []grpc.DialOption{
92101
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
93102
},
103+
httpClient: http.DefaultClient,
94104
}
95105

96106
for _, opt := range opts {
@@ -158,7 +168,7 @@ func (s *Service) instance() (offlipt.Client, error) {
158168
opts = append(opts, sdk.WithClientTokenProvider(s.tokenProvider))
159169
}
160170

161-
hclient := sdk.New(sdkhttp.NewTransport(s.address), opts...)
171+
hclient := sdk.New(sdkhttp.NewTransport(s.address, sdkhttp.WithHTTPClient(s.httpClient)), opts...)
162172
if u.Scheme == "https" || u.Scheme == "http" {
163173
s.client = &fclient{
164174
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)