Skip to content

Commit

Permalink
feat!: Add custom baseURL config for resolve (#62)
Browse files Browse the repository at this point in the history
* feat!: Remove APIRegion add APIResolverUrl config

Signed-off-by: Fabrizio Demaria <[email protected]>

* chore: Demo app uses local sdk dependency

Signed-off-by: Fabrizio Demaria <[email protected]>

* docs: Readme update on side-car

* fix: default url not picked up

Signed-off-by: Fabrizio Demaria <[email protected]>

* fix: format

Signed-off-by: Fabrizio Demaria <[email protected]>

* feat: make sure default url is set

Signed-off-by: Fabrizio Demaria <[email protected]>

* refactor: rename base url config

Signed-off-by: Fabrizio Demaria <[email protected]>

---------

Signed-off-by: Fabrizio Demaria <[email protected]>
  • Loading branch information
fabriziodemaria authored Sep 10, 2024
1 parent 363a21c commit 782cdca
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 38 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ require (
### Creating and using the flag provider

Below is an example for how to create a OpenFeature client using the Confidence flag provider, and then resolve
a flag with a boolean attribute. The provider is configured with an api key and a region, which will determine
where it will send the resolving requests.
a flag with a boolean attribute.

The provider is configured via `NewAPIConfig(...)`, with which you can set the api key for authentication.
Optionally, a custom resolve API url can be configured if, for example, the resolver service is running on a locally deployed side-car (`NewAPIConfigWithUrl(...)`).

The flag will be applied immediately, meaning that Confidence will count the targeted user as having received the treatment.

Expand Down
3 changes: 1 addition & 2 deletions demo/GoDemoApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func main() {
fmt.Println("Fetching the flags...")

confidence := c.NewConfidenceBuilder().SetAPIConfig(c.APIConfig{APIKey: "API_KEY"}).Build()
confidence := c.NewConfidenceBuilder().SetAPIConfig(*c.NewAPIConfig("API_KEY")).Build()
targetingKey := "Random_targeting_key"
confidence.PutContext("targeting_key", targetingKey)

Expand All @@ -34,5 +34,4 @@ func main() {
wg := confidence.Track(context.Background(), "page-viewed", map[string]interface{}{})
wg.Wait()
fmt.Println("Event sent")

}
4 changes: 3 additions & 1 deletion demo/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ module demo

go 1.22.2

require github.com/spotify/confidence-sdk-go v0.2.2-0.20240523155258-4bbc177010cc
require github.com/spotify/confidence-sdk-go v0.2.3

replace github.com/spotify/confidence-sdk-go => ../
2 changes: 2 additions & 0 deletions demo/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spotify/confidence-sdk-go v0.2.2-0.20240523155258-4bbc177010cc h1:CyQdom204c6w4UjbT63Otk3axZff4mEbeVDF/snvfas=
github.com/spotify/confidence-sdk-go v0.2.2-0.20240523155258-4bbc177010cc/go.mod h1:TYBqx3F0AZO7HZF8Nkf2dWnvVHH91ICo0W6e9wmLsk4=
github.com/spotify/confidence-sdk-go v0.2.3 h1:vJJjWJo6qgpgX+Lg/uiWrtzRw+qaJhwIQRkxwrBJUCA=
github.com/spotify/confidence-sdk-go v0.2.3/go.mod h1:3MInYY3UiHaNToPlL0mTgbWjcwMMGV/4OfbWAiJ2JC0=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
Expand Down
3 changes: 3 additions & 0 deletions pkg/confidence/confidence.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type ConfidenceBuilder struct {

func (e ConfidenceBuilder) SetAPIConfig(config APIConfig) ConfidenceBuilder {
e.confidence.Config = config
if config.APIResolveBaseUrl == "" {
e.confidence.Config.APIResolveBaseUrl = DefaultAPIResolveBaseUrl
}
return e
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/confidence/confidence_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func TestChildContextRemoveParentContext(t *testing.T) {
func create_confidence(t *testing.T, response ResolveResponse) *Confidence {
config := APIConfig{
APIKey: "apiKey",
Region: APIRegionGlobal,
}
return &Confidence{
Config: config,
Expand All @@ -109,7 +108,6 @@ func create_confidence(t *testing.T, response ResolveResponse) *Confidence {
func createConfidenceWithUploader(t *testing.T, response ResolveResponse, uploader MockEventUploader) *Confidence {
config := APIConfig{
APIKey: "apiKey",
Region: APIRegionGlobal,
}
return &Confidence{
Config: config,
Expand Down
1 change: 0 additions & 1 deletion pkg/confidence/confidence_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ func emptyResponse() ResolveResponse {
func newConfidence(apiKey string, client ResolveClient) *Confidence {
config := APIConfig{
APIKey: apiKey,
Region: APIRegionGlobal,
}
return &Confidence{
Config: config,
Expand Down
2 changes: 1 addition & 1 deletion pkg/confidence/http_resolve_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (client HttpResolveClient) SendResolveRequest(ctx context.Context,

payload := bytes.NewBuffer(jsonRequest)
req, err := http.NewRequestWithContext(ctx,
http.MethodPost, fmt.Sprintf("%s/flags:resolve", client.Config.Region.apiURL()), payload)
http.MethodPost, fmt.Sprintf("%s/v1/flags:resolve", client.Config.APIResolveBaseUrl), payload)
if err != nil {
return ResolveResponse{}, err
}
Expand Down
39 changes: 10 additions & 29 deletions pkg/confidence/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"errors"
)

type APIRegion int64

func NewFlagNotFoundResolutionError(msg string) ResolutionError {
return ResolutionError{
code: FlagNotFoundCode,
Expand Down Expand Up @@ -58,48 +56,31 @@ func NewGeneralResolutionError(msg string) ResolutionError {
}
}

const DefaultAPIResolveBaseUrl = "https://resolver.confidence.dev"

type APIConfig struct {
APIKey string
Region APIRegion
APIKey string
APIResolveBaseUrl string
}

func NewAPIConfig(apiKey string) *APIConfig {
return &APIConfig{
APIKey: apiKey,
Region: APIRegionGlobal,
APIKey: apiKey,
APIResolveBaseUrl: DefaultAPIResolveBaseUrl,
}
}

const (
APIRegionEU = iota
APIRegionUS = iota
APIRegionGlobal = iota
)

// Private types below

const euAPIURL = "https://resolver.eu.confidence.dev/v1"
const usAPIURL = "https://resolver.us.confidence.dev/v1"
const globalAPIURL = "https://resolver.confidence.dev/v1"

func (r APIRegion) apiURL() string {
if r == APIRegionEU {
return euAPIURL
} else if r == APIRegionUS {
return usAPIURL
} else if r == APIRegionGlobal {
return globalAPIURL
func NewAPIConfigWithUrl(apiKey, APIResolveBaseUrl string) *APIConfig {
return &APIConfig{
APIKey: apiKey,
APIResolveBaseUrl: APIResolveBaseUrl,
}
return ""
}

func (c APIConfig) Validate() error {
if c.APIKey == "" {
return errors.New("api key needs to be set")
}
if c.Region.apiURL() == "" {
return errors.New("api region needs to be set")
}
return nil
}

Expand Down

0 comments on commit 782cdca

Please sign in to comment.