Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion lib/client_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export const getOptimizelyInstance = (config: OptimizelyFactoryConfig): Optimize
retryConfig: {
maxRetries: DEFAULT_CMAB_RETRIES,
backoffProvider: () => new ConstantBackoff(DEFAULT_CMAB_BACKOFF_MS),
}
},
predictionEndpointTemplate: config.cmab?.predictionEndpointTemplate,
});

const cmabCache: CacheWithRemove<CmabCacheValue> = config.cmab?.cache ?
Expand Down
46 changes: 46 additions & 0 deletions lib/core/decision_service/cmab/cmab_client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,50 @@ describe('DefaultCmabClient', () => {

await expect(cmabClient.fetchDecision(ruleId, userId, attributes, cmabUuid)).rejects.toThrow('error');
});

it('should use custom prediction endpoint template when provided', async () => {
const requestHandler = getMockRequestHandler();

const mockMakeRequest: MockInstance<RequestHandler['makeRequest']> = requestHandler.makeRequest;
mockMakeRequest.mockReturnValue(getMockAbortableRequest(mockSuccessResponse('var456')));

const customEndpoint = 'https://custom.example.com/predict/%s';
const cmabClient = new DefaultCmabClient({
requestHandler,
predictionEndpointTemplate: customEndpoint,
});
const ruleId = '789';
const userId = 'user789';
const attributes = {
browser: 'firefox',
};
const cmabUuid = 'uuid789';
const variation = await cmabClient.fetchDecision(ruleId, userId, attributes, cmabUuid);
const [requestUrl] = mockMakeRequest.mock.calls[0];

expect(variation).toBe('var456');
expect(mockMakeRequest.mock.calls.length).toBe(1);
expect(requestUrl).toBe('https://custom.example.com/predict/789');
});

it('should use default prediction endpoint template when not provided', async () => {
const requestHandler = getMockRequestHandler();
const mockMakeRequest: MockInstance<RequestHandler['makeRequest']> = requestHandler.makeRequest;
mockMakeRequest.mockReturnValue(getMockAbortableRequest(mockSuccessResponse('var999')));
const cmabClient = new DefaultCmabClient({
requestHandler,
});
const ruleId = '555';
const userId = 'user555';
const attributes = {
browser: 'safari',
};
const cmabUuid = 'uuid555';
const variation = await cmabClient.fetchDecision(ruleId, userId, attributes, cmabUuid);
const [requestUrl] = mockMakeRequest.mock.calls[0];

expect(variation).toBe('var999');
expect(mockMakeRequest.mock.calls.length).toBe(1);
expect(requestUrl).toBe('https://prediction.cmab.optimizely.com/predict/555');
});
});
11 changes: 8 additions & 3 deletions lib/core/decision_service/cmab/cmab_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,30 @@ export interface CmabClient {
): Promise<string>
}

const CMAB_PREDICTION_ENDPOINT = 'https://prediction.cmab.optimizely.com/predict/%s';
const DEFAULT_CMAB_PREDICTION_ENDPOINT = 'https://prediction.cmab.optimizely.com/predict/%s';

export type RetryConfig = {
maxRetries: number,
backoffProvider?: Producer<BackoffController>;
}

export type CmabClientConfig = {
requestHandler: RequestHandler,
requestHandler: RequestHandler;
retryConfig?: RetryConfig;
predictionEndpointTemplate?: string;
}

export class DefaultCmabClient implements CmabClient {
private requestHandler: RequestHandler;
private retryConfig?: RetryConfig;
private predictionEndpointTemplate: string = DEFAULT_CMAB_PREDICTION_ENDPOINT;

constructor(config: CmabClientConfig) {
this.requestHandler = config.requestHandler;
this.retryConfig = config.retryConfig;
if (config.predictionEndpointTemplate) {
this.predictionEndpointTemplate = config.predictionEndpointTemplate;
}
}

async fetchDecision(
Expand All @@ -60,7 +65,7 @@ export class DefaultCmabClient implements CmabClient {
attributes: UserAttributes,
cmabUuid: string,
): Promise<string> {
const url = sprintf(CMAB_PREDICTION_ENDPOINT, ruleId);
const url = sprintf(this.predictionEndpointTemplate, ruleId);

const cmabAttributes = Object.keys(attributes).map((key) => ({
id: key,
Expand Down
1 change: 1 addition & 0 deletions lib/shared_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ export interface Config {
cacheSize?: number;
cacheTtl?: number;
cache?: CacheWithRemove<string>;
predictionEndpointTemplate?: string;
}
}

Expand Down
Loading