Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Add custom baseURL config for resolve #62

Merged
merged 7 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.APIResolveUrl), payload)
if err != nil {
return ResolveResponse{}, err
}
Expand Down
33 changes: 6 additions & 27 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 @@ -60,46 +58,27 @@ func NewGeneralResolutionError(msg string) ResolutionError {

type APIConfig struct {
APIKey string
Region APIRegion
APIResolveUrl string
}

func NewAPIConfig(apiKey string) *APIConfig {
return &APIConfig{
APIKey: apiKey,
Region: APIRegionGlobal,
APIResolveUrl: "https://resolver.confidence.dev",
}
}

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, apiResolveUrl string) *APIConfig {
fabriziodemaria marked this conversation as resolved.
Show resolved Hide resolved
return &APIConfig{
APIKey: apiKey,
APIResolveUrl: apiResolveUrl,
}
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
Loading