Skip to content

[FSSDK-11169] Implement Decision Service methods to handle CMAB #403

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

Merged
merged 19 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
54 changes: 27 additions & 27 deletions pkg/decision/cmab_client.go → pkg/cmab/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License. *
***************************************************************************/

// Package decision provides CMAB client implementation
package decision
// Package cmab provides contextual multi-armed bandit functionality
package cmab

import (
"bytes"
Expand Down Expand Up @@ -44,34 +44,34 @@ const (
DefaultBackoffMultiplier = 2.0
)

// CMABAttribute represents an attribute in a CMAB request
type CMABAttribute struct {
// Attribute represents an attribute in a CMAB request
type Attribute struct {
ID string `json:"id"`
Value interface{} `json:"value"`
Type string `json:"type"`
}

// CMABInstance represents an instance in a CMAB request
type CMABInstance struct {
VisitorID string `json:"visitorId"`
ExperimentID string `json:"experimentId"`
Attributes []CMABAttribute `json:"attributes"`
CmabUUID string `json:"cmabUUID"`
// Instance represents an instance in a CMAB request
type Instance struct {
VisitorID string `json:"visitorId"`
ExperimentID string `json:"experimentId"`
Attributes []Attribute `json:"attributes"`
CmabUUID string `json:"cmabUUID"`
}

// CMABRequest represents a request to the CMAB API
type CMABRequest struct {
Instances []CMABInstance `json:"instances"`
// Request represents a request to the CMAB API
type Request struct {
Instances []Instance `json:"instances"`
}

// CMABPrediction represents a prediction in a CMAB response
type CMABPrediction struct {
// Prediction represents a prediction in a CMAB response
type Prediction struct {
VariationID string `json:"variation_id"`
}

// CMABResponse represents a response from the CMAB API
type CMABResponse struct {
Predictions []CMABPrediction `json:"predictions"`
// Response represents a response from the CMAB API
type Response struct {
Predictions []Prediction `json:"predictions"`
}

// RetryConfig defines configuration for retry behavior
Expand All @@ -93,15 +93,15 @@ type DefaultCmabClient struct {
logger logging.OptimizelyLogProducer
}

// CmabClientOptions defines options for creating a CMAB client
type CmabClientOptions struct {
// ClientOptions defines options for creating a CMAB client
type ClientOptions struct {
HTTPClient *http.Client
RetryConfig *RetryConfig
Logger logging.OptimizelyLogProducer
}

// NewDefaultCmabClient creates a new instance of DefaultCmabClient
func NewDefaultCmabClient(options CmabClientOptions) *DefaultCmabClient {
func NewDefaultCmabClient(options ClientOptions) *DefaultCmabClient {
httpClient := options.HTTPClient
if httpClient == nil {
httpClient = &http.Client{
Expand Down Expand Up @@ -142,18 +142,18 @@ func (c *DefaultCmabClient) FetchDecision(
url := fmt.Sprintf(CMABPredictionEndpoint, ruleID)

// Convert attributes to CMAB format
cmabAttributes := make([]CMABAttribute, 0, len(attributes))
cmabAttributes := make([]Attribute, 0, len(attributes))
for key, value := range attributes {
cmabAttributes = append(cmabAttributes, CMABAttribute{
cmabAttributes = append(cmabAttributes, Attribute{
ID: key,
Value: value,
Type: "custom_attribute",
})
}

// Create the request body
requestBody := CMABRequest{
Instances: []CMABInstance{
requestBody := Request{
Instances: []Instance{
{
VisitorID: userID,
ExperimentID: ruleID,
Expand Down Expand Up @@ -248,7 +248,7 @@ func (c *DefaultCmabClient) doFetch(ctx context.Context, url string, bodyBytes [
}

// Parse response
var cmabResponse CMABResponse
var cmabResponse Response
if err := json.Unmarshal(respBody, &cmabResponse); err != nil {
return "", fmt.Errorf("failed to unmarshal CMAB response: %w", err)
}
Expand All @@ -263,6 +263,6 @@ func (c *DefaultCmabClient) doFetch(ctx context.Context, url string, bodyBytes [
}

// validateResponse validates the CMAB response
func (c *DefaultCmabClient) validateResponse(response CMABResponse) bool {
func (c *DefaultCmabClient) validateResponse(response Response) bool {
return len(response.Predictions) > 0 && response.Predictions[0].VariationID != ""
}
48 changes: 24 additions & 24 deletions pkg/decision/cmab_client_test.go → pkg/cmab/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License. *
***************************************************************************/

// Package decision //
package decision
// Package cmab //
package cmab

import (
"context"
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestDefaultCmabClient_FetchDecision(t *testing.T) {
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))

// Parse request body
var requestBody CMABRequest
var requestBody Request
err := json.NewDecoder(r.Body).Decode(&requestBody)
assert.NoError(t, err)

Expand All @@ -80,7 +80,7 @@ func TestDefaultCmabClient_FetchDecision(t *testing.T) {
assert.Len(t, instance.Attributes, 5)

// Create a map for easier attribute checking
attrMap := make(map[string]CMABAttribute)
attrMap := make(map[string]Attribute)
for _, attr := range instance.Attributes {
attrMap[attr.ID] = attr
assert.Equal(t, "custom_attribute", attr.Type)
Expand Down Expand Up @@ -109,8 +109,8 @@ func TestDefaultCmabClient_FetchDecision(t *testing.T) {
// Return response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
response := CMABResponse{
Predictions: []CMABPrediction{
response := Response{
Predictions: []Prediction{
{
VariationID: "var123",
},
Expand All @@ -121,7 +121,7 @@ func TestDefaultCmabClient_FetchDecision(t *testing.T) {
defer server.Close()

// Create client with custom endpoint
client := NewDefaultCmabClient(CmabClientOptions{
client := NewDefaultCmabClient(ClientOptions{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestDefaultCmabClient_FetchDecision_WithRetry(t *testing.T) {
body, err := io.ReadAll(r.Body)
assert.NoError(t, err)

var requestBody CMABRequest
var requestBody Request
err = json.Unmarshal(body, &requestBody)
assert.NoError(t, err)

Expand All @@ -187,8 +187,8 @@ func TestDefaultCmabClient_FetchDecision_WithRetry(t *testing.T) {
// Return success response on third attempt
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
response := CMABResponse{
Predictions: []CMABPrediction{
response := Response{
Predictions: []Prediction{
{
VariationID: "var123",
},
Expand All @@ -199,7 +199,7 @@ func TestDefaultCmabClient_FetchDecision_WithRetry(t *testing.T) {
defer server.Close()

// Create client with custom endpoint and retry config
client := NewDefaultCmabClient(CmabClientOptions{
client := NewDefaultCmabClient(ClientOptions{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestDefaultCmabClient_FetchDecision_ExhaustedRetries(t *testing.T) {
defer server.Close()

// Create client with custom endpoint and retry config
client := NewDefaultCmabClient(CmabClientOptions{
client := NewDefaultCmabClient(ClientOptions{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestDefaultCmabClient_FetchDecision_NoRetryConfig(t *testing.T) {
defer server.Close()

// Create client with custom endpoint but no retry config
client := NewDefaultCmabClient(CmabClientOptions{
client := NewDefaultCmabClient(ClientOptions{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestDefaultCmabClient_FetchDecision_InvalidResponse(t *testing.T) {
defer server.Close()

// Create client with custom endpoint
client := NewDefaultCmabClient(CmabClientOptions{
client := NewDefaultCmabClient(ClientOptions{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
Expand Down Expand Up @@ -393,7 +393,7 @@ func TestDefaultCmabClient_FetchDecision_NetworkErrors(t *testing.T) {
}

// Create client with non-existent server to simulate network errors
client := NewDefaultCmabClient(CmabClientOptions{
client := NewDefaultCmabClient(ClientOptions{
HTTPClient: &http.Client{
Timeout: 100 * time.Millisecond, // Short timeout to fail quickly
},
Expand Down Expand Up @@ -442,8 +442,8 @@ func TestDefaultCmabClient_ExponentialBackoff(t *testing.T) {
// Return success response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
response := CMABResponse{
Predictions: []CMABPrediction{
response := Response{
Predictions: []Prediction{
{
VariationID: "var123",
},
Expand All @@ -454,7 +454,7 @@ func TestDefaultCmabClient_ExponentialBackoff(t *testing.T) {
defer server.Close()

// Create client with custom endpoint and specific retry config
client := NewDefaultCmabClient(CmabClientOptions{
client := NewDefaultCmabClient(ClientOptions{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
Expand Down Expand Up @@ -504,7 +504,7 @@ func TestDefaultCmabClient_ExponentialBackoff(t *testing.T) {

func TestNewDefaultCmabClient_DefaultValues(t *testing.T) {
// Test with empty options
client := NewDefaultCmabClient(CmabClientOptions{})
client := NewDefaultCmabClient(ClientOptions{})

// Verify default values
assert.NotNil(t, client.httpClient)
Expand Down Expand Up @@ -541,7 +541,7 @@ func TestDefaultCmabClient_LoggingBehavior(t *testing.T) {
defer server.Close()

// Create client with custom logger
client := NewDefaultCmabClient(CmabClientOptions{
client := NewDefaultCmabClient(ClientOptions{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
Expand Down Expand Up @@ -610,7 +610,7 @@ func TestDefaultCmabClient_NonSuccessStatusCode(t *testing.T) {
defer server.Close()

// Create client with custom endpoint and no retries
client := NewDefaultCmabClient(CmabClientOptions{
client := NewDefaultCmabClient(ClientOptions{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
Expand Down Expand Up @@ -649,8 +649,8 @@ func TestDefaultCmabClient_FetchDecision_ContextCancellation(t *testing.T) {

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
response := CMABResponse{
Predictions: []CMABPrediction{
response := Response{
Predictions: []Prediction{
{
VariationID: "var123",
},
Expand All @@ -661,7 +661,7 @@ func TestDefaultCmabClient_FetchDecision_ContextCancellation(t *testing.T) {
defer server.Close()

// Create client with custom endpoint
client := NewDefaultCmabClient(CmabClientOptions{
client := NewDefaultCmabClient(ClientOptions{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
Expand Down
Loading