Skip to content

Commit 57cd493

Browse files
committed
option to override http.Client for OFREP communication
Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent 0cb921b commit 57cd493

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

providers/ofrep/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ You can configure the provider using following configuration options,
3232

3333
| Configuration option | Details |
3434
|----------------------|-------------------------------------------------------------------------------------------------------------------------|
35-
| WithBearerToken | Set the token to be used with "Bearer" HTTP Authorization schema |
3635
| WithApiKeyAuth | Set the token to be used with "X-API-Key" header |
36+
| WithBearerToken | Set the token to be used with "Bearer" HTTP Authorization schema |
37+
| WithClient | Provider a custom, pre-configured http.Client for OFREP service communication |
3738
| WithHeaderProvider | Register a custom header provider for OFREP calls. You may utilize this for custom authentication/authorization headers |
3839

3940

providers/ofrep/internal/outbound/http.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,29 @@ const ofrepV1 = "/ofrep/v1/evaluate/flags/"
1717
type HeaderCallback func() (name string, value string)
1818

1919
type Configuration struct {
20-
Callbacks []HeaderCallback
2120
BaseURI string
21+
Callbacks []HeaderCallback
22+
Client *http.Client
2223
}
2324

2425
// Outbound client for http communication
2526
type Outbound struct {
26-
headerProvider []HeaderCallback
2727
baseURI string
28-
29-
client http.Client
28+
client *http.Client
29+
headerProvider []HeaderCallback
3030
}
3131

3232
func NewHttp(cfg Configuration) *Outbound {
33-
client := http.Client{
34-
Timeout: 10 * time.Second,
33+
if cfg.Client == nil {
34+
cfg.Client = &http.Client{
35+
Timeout: 10 * time.Second,
36+
}
3537
}
3638

3739
return &Outbound{
3840
headerProvider: cfg.Callbacks,
3941
baseURI: cfg.BaseURI,
40-
client: client,
42+
client: cfg.Client,
4143
}
4244
}
4345

providers/ofrep/provider.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ofrep
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67

78
"github.com/open-feature/go-sdk-contrib/providers/ofrep/internal/evaluate"
89
"github.com/open-feature/go-sdk-contrib/providers/ofrep/internal/outbound"
@@ -90,3 +91,10 @@ func WithApiKeyAuth(token string) func(*outbound.Configuration) {
9091
})
9192
}
9293
}
94+
95+
// WithClient allows to provide a pre-configured http.Client for the communication with the OFREP service
96+
func WithClient(client *http.Client) func(configuration *outbound.Configuration) {
97+
return func(configuration *outbound.Configuration) {
98+
configuration.Client = client
99+
}
100+
}

providers/ofrep/provider_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,15 @@ func TestWiringE2E(t *testing.T) {
8383
}
8484
}()
8585

86+
// time for server to be ready
8687
<-time.After(3 * time.Second)
8788

88-
provider := NewProvider(fmt.Sprintf("http://%s", host))
89+
// custom client with reduced timeout
90+
customClient := &http.Client{
91+
Timeout: 1 * time.Second,
92+
}
93+
94+
provider := NewProvider(fmt.Sprintf("http://%s", host), WithClient(customClient))
8995
booleanEvaluation := provider.BooleanEvaluation(context.Background(), "flag", false, nil)
9096

9197
if booleanEvaluation.Value != true {

0 commit comments

Comments
 (0)