@@ -4,12 +4,17 @@ import (
44 "context"
55 "fmt"
66 "net/http"
7+ "os"
8+ "strings"
9+ "time"
710
811 "github.com/open-feature/go-sdk-contrib/providers/ofrep/internal/evaluate"
912 "github.com/open-feature/go-sdk-contrib/providers/ofrep/internal/outbound"
1013 "github.com/open-feature/go-sdk/openfeature"
1114)
1215
16+ var _ openfeature.FeatureProvider = (* Provider )(nil )
17+
1318// Provider implementation for OFREP
1419type Provider struct {
1520 evaluator Evaluator
@@ -22,6 +27,7 @@ type Option func(*outbound.Configuration)
2227func NewProvider (baseUri string , options ... Option ) * Provider {
2328 cfg := outbound.Configuration {
2429 BaseURI : baseUri ,
30+ Timeout : 10 * time .Second ,
2531 }
2632
2733 for _ , option := range options {
@@ -67,14 +73,21 @@ func (p Provider) Hooks() []openfeature.Hook {
6773
6874// options of the OFREP provider
6975
70- // WithHeaderProvider allows to configure a custom header callback to set a custom authorization header
76+ // WithHeader allows to set a custom header to be used for authorization.
77+ func WithHeader (name , value string ) func (* outbound.Configuration ) {
78+ return WithHeaderProvider (func () (string , string ) {
79+ return name , value
80+ })
81+ }
82+
83+ // WithHeaderProvider allows to configure a custom header callback to set a custom authorization header.
7184func WithHeaderProvider (callback outbound.HeaderCallback ) func (* outbound.Configuration ) {
7285 return func (c * outbound.Configuration ) {
7386 c .Callbacks = append (c .Callbacks , callback )
7487 }
7588}
7689
77- // WithBearerToken allows to set token to be used for bearer token authorization
90+ // WithBearerToken allows to set token to be used for bearer token authorization.
7891func WithBearerToken (token string ) func (* outbound.Configuration ) {
7992 return func (c * outbound.Configuration ) {
8093 c .Callbacks = append (c .Callbacks , func () (string , string ) {
@@ -83,7 +96,7 @@ func WithBearerToken(token string) func(*outbound.Configuration) {
8396 }
8497}
8598
86- // WithApiKeyAuth allows to set token to be used for api key authorization
99+ // WithApiKeyAuth allows to set token to be used for api key authorization.
87100func WithApiKeyAuth (token string ) func (* outbound.Configuration ) {
88101 return func (c * outbound.Configuration ) {
89102 c .Callbacks = append (c .Callbacks , func () (string , string ) {
@@ -92,9 +105,67 @@ func WithApiKeyAuth(token string) func(*outbound.Configuration) {
92105 }
93106}
94107
95- // WithClient allows to provide a pre-configured http.Client for the communication with the OFREP service
108+ // WithClient allows to provide a pre-configured http.Client for the communication with the OFREP service.
96109func WithClient (client * http.Client ) func (configuration * outbound.Configuration ) {
97110 return func (configuration * outbound.Configuration ) {
98111 configuration .Client = client
99112 }
100113}
114+
115+ // WithBaseURI allows to set the base URI of the OFREP service.
116+ func WithBaseURI (baseURI string ) func (configuration * outbound.Configuration ) {
117+ return func (configuration * outbound.Configuration ) {
118+ configuration .BaseURI = baseURI
119+ }
120+ }
121+
122+ // WithTimeout allows to set the timeout for the http client used for communication with the OFREP service.
123+ func WithTimeout (timeout time.Duration ) func (configuration * outbound.Configuration ) {
124+ return func (configuration * outbound.Configuration ) {
125+ configuration .Timeout = timeout
126+ }
127+ }
128+
129+ // WithFromEnv uses environment variables to configure the provider.
130+ //
131+ // Experimental: This feature is experimental and may change in future versions.
132+ //
133+ // Supported environment variables:
134+ // - OFREP_ENDPOINT: base URI for the OFREP service
135+ // - OFREP_TIMEOUT: timeout duration (e.g., "30s", "500ms")
136+ // - OFREP_API_KEY: API key for X-API-Key authentication
137+ // - OFREP_BEARER_TOKEN: token for Bearer authentication
138+ // - OFREP_HEADERS: comma-separated custom headers (e.g., "Key1=Value1,Key2=Value2")
139+ func WithFromEnv () func (* outbound.Configuration ) {
140+ envHandlers := map [string ]func (* outbound.Configuration , string ){
141+ "OFREP_ENDPOINT" : func (c * outbound.Configuration , v string ) {
142+ WithBaseURI (v )(c )
143+ },
144+ "OFREP_TIMEOUT" : func (c * outbound.Configuration , v string ) {
145+ if t , err := time .ParseDuration (v ); err == nil {
146+ WithTimeout (t )(c )
147+ }
148+ },
149+ "OFREP_API_KEY" : func (c * outbound.Configuration , v string ) {
150+ WithApiKeyAuth (v )(c )
151+ },
152+ "OFREP_BEARER_TOKEN" : func (c * outbound.Configuration , v string ) {
153+ WithBearerToken (v )(c )
154+ },
155+ "OFREP_HEADERS" : func (c * outbound.Configuration , v string ) {
156+ for pair := range strings .SplitSeq (v , "," ) {
157+ kv := strings .SplitN (strings .TrimSpace (pair ), "=" , 2 )
158+ if len (kv ) == 2 {
159+ WithHeader (kv [0 ], kv [1 ])(c )
160+ }
161+ }
162+ },
163+ }
164+ return func (c * outbound.Configuration ) {
165+ for key , handler := range envHandlers {
166+ if v := os .Getenv (key ); v != "" {
167+ handler (c , v )
168+ }
169+ }
170+ }
171+ }
0 commit comments