Skip to content

Commit

Permalink
exposeAllKeys default true for flags route + more info in flag route
Browse files Browse the repository at this point in the history
  • Loading branch information
kjose committed Aug 24, 2022
1 parent 43d9d69 commit 98b4a0c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
6 changes: 6 additions & 0 deletions internal/apilogic/handle_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package apilogic

import (
"net/http"
"strings"

"github.com/flagship-io/decision-api/internal/handle"
"github.com/flagship-io/decision-api/internal/utils"
Expand All @@ -23,6 +24,11 @@ func BuildHandleRequest(req *http.Request) (*handle.Request, error) {
handleRequest.Mode = mode
}

// exposeAllKeys default true for flags route
if strings.Contains(req.URL.String(), "/flags") {
handleRequest.ExposeAllKeys = true
}
// exposeAllKeys url param extend
exposeAllKeys := req.URL.Query().Get("exposeAllKeys")
if exposeAllKeys != "" {
handleRequest.ExposeAllKeys = exposeAllKeys == "true"
Expand Down
39 changes: 39 additions & 0 deletions internal/apilogic/handle_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,42 @@ func TestBuildHandleRequestHasCorrectSendContextEvent(t *testing.T) {
}

}

func TestBuildHandleRequestHasCorrectExposeAllKeys(t *testing.T) {
tests := map[string]struct {
path string
query string
result bool
}{
"FlagRouteUrlParamEmpty": {"/v2/flags", "", true},
"FlagRouteUrlParamTrue": {"/v2/flags", "exposeAllKeys=true", true},
"FlagRouteUrlParamFalse": {"/v2/flags", "exposeAllKeys=false", false},
"CampaignsRouteUrlParamFalse": {"/v2/campaigns", "", false},
"CampaignsRouteUrlParamTrue": {"/v2/campaigns", "exposeAllKeys=true", true},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
body := `{
"visitor_id": "123",
"context": {}
}`

req := &http.Request{
URL: &url.URL{
Path: test.path,
RawQuery: test.query,
},
Body: io.NopCloser(strings.NewReader(body)),
Method: "POST",
}

hr, err := BuildHandleRequest(req)

assert.NotNil(t, hr)
assert.Nil(t, err)
assert.Equal(t, test.result, hr.ExposeAllKeys)
})
}

}
27 changes: 18 additions & 9 deletions pkg/handlers/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import (

// FlagMetadata represents the metadata informations about a flag key
type FlagMetadata struct {
CampaignID string `json:"campaignId"`
VariationGroupID string `json:"variationGroupId"`
VariationID string `json:"variationId"`
CampaignID string `json:"campaignId"`
Slug *string `json:"slug"`
Type string `json:"type"`
VariationGroupID string `json:"variationGroupId"`
VariationID string `json:"variationId"`
Reference bool `json:"reference"`
}

// FlagInfo represents the informations about a flag key
Expand Down Expand Up @@ -56,13 +59,19 @@ func sendFlagsResponse(w http.ResponseWriter, decisionResponse *decision_respons
for _, c := range decisionResponse.Campaigns {
if c.GetVariation() != nil && c.GetVariation().GetModifications() != nil && c.GetVariation().GetModifications().GetValue() != nil && c.GetVariation().GetModifications().GetValue().GetFields() != nil {
for k, v := range c.GetVariation().GetModifications().GetValue().GetFields() {
mdata := FlagMetadata{
CampaignID: c.GetId().Value,
Type: c.GetType().Value,
VariationGroupID: c.GetVariationGroupId().Value,
VariationID: c.GetVariation().GetId().Value,
Reference: c.GetVariation().GetReference(),
}
if c.GetSlug() != nil {
mdata.Slug = &c.GetSlug().Value
}
flagInfos[k] = &FlagInfo{
Value: v,
Metadata: FlagMetadata{
CampaignID: c.GetId().Value,
VariationGroupID: c.GetVariationGroupId().Value,
VariationID: c.GetVariation().GetId().Value,
},
Value: v,
Metadata: mdata,
}
}
}
Expand Down

0 comments on commit 98b4a0c

Please sign in to comment.