Skip to content

Commit 75a0148

Browse files
authored
[FSSDK-10090] Refactor ODP integration (#920)
* refactor * refactor: build working * fix segment manager tests * fix identify user * update event api manager * update event man * add sement manager test * fix odp event manager tests * odp manager tests * fix odp event manager tests * odpManager tests * odp manager tests * browserOdpManager tests fixed * fix project config tests * fix tests * fix * config manager tests fix * odpManager test * event man update * copyright * remove console log * fix review * undo clear timeout * remove unnecessary line
1 parent 53a2042 commit 75a0148

29 files changed

+1624
-1081
lines changed

Diff for: lib/core/odp/odp_config.ts

+37-75
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022-2023, Optimizely
2+
* Copyright 2022-2024, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,90 +19,29 @@ import { checkArrayEquality } from '../../utils/fns';
1919
export class OdpConfig {
2020
/**
2121
* Host of ODP audience segments API.
22-
* @private
2322
*/
24-
private _apiHost: string;
25-
26-
/**
27-
* Getter to retrieve the ODP server host
28-
* @public
29-
*/
30-
get apiHost(): string {
31-
return this._apiHost;
32-
}
23+
readonly apiHost: string;
3324

3425
/**
3526
* Public API key for the ODP account from which the audience segments will be fetched (optional).
36-
* @private
3727
*/
38-
private _apiKey: string;
39-
40-
/**
41-
* Getter to retrieve the ODP API key
42-
* @public
43-
*/
44-
get apiKey(): string {
45-
return this._apiKey;
46-
}
28+
readonly apiKey: string;
4729

4830
/**
4931
* Url for sending events via pixel.
50-
* @private
51-
*/
52-
private _pixelUrl: string;
53-
54-
/**
55-
* Getter to retrieve the ODP pixel URL
56-
* @public
5732
*/
58-
get pixelUrl(): string {
59-
return this._pixelUrl;
60-
}
33+
readonly pixelUrl: string;
6134

6235
/**
6336
* All ODP segments used in the current datafile (associated with apiHost/apiKey).
64-
* @private
65-
*/
66-
private _segmentsToCheck: string[];
67-
68-
/**
69-
* Getter for ODP segments to check
70-
* @public
71-
*/
72-
get segmentsToCheck(): string[] {
73-
return this._segmentsToCheck;
74-
}
75-
76-
constructor(apiKey?: string, apiHost?: string, pixelUrl?: string, segmentsToCheck?: string[]) {
77-
this._apiKey = apiKey ?? '';
78-
this._apiHost = apiHost ?? '';
79-
this._pixelUrl = pixelUrl ?? '';
80-
this._segmentsToCheck = segmentsToCheck ?? [];
81-
}
82-
83-
/**
84-
* Update the ODP configuration details
85-
* @param {OdpConfig} config New ODP Config to potentially update self with
86-
* @returns true if configuration was updated successfully
8737
*/
88-
update(config: OdpConfig): boolean {
89-
if (this.equals(config)) {
90-
return false;
91-
} else {
92-
if (config.apiKey) this._apiKey = config.apiKey;
93-
if (config.apiHost) this._apiHost = config.apiHost;
94-
if (config.pixelUrl) this._pixelUrl = config.pixelUrl;
95-
if (config.segmentsToCheck) this._segmentsToCheck = config.segmentsToCheck;
38+
readonly segmentsToCheck: string[];
9639

97-
return true;
98-
}
99-
}
100-
101-
/**
102-
* Determines if ODP configuration has the minimum amount of information
103-
*/
104-
isReady(): boolean {
105-
return !!this._apiKey && !!this._apiHost;
40+
constructor(apiKey: string, apiHost: string, pixelUrl: string, segmentsToCheck: string[]) {
41+
this.apiKey = apiKey;
42+
this.apiHost = apiHost;
43+
this.pixelUrl = pixelUrl;
44+
this.segmentsToCheck = segmentsToCheck;
10645
}
10746

10847
/**
@@ -112,10 +51,33 @@ export class OdpConfig {
11251
*/
11352
equals(configToCompare: OdpConfig): boolean {
11453
return (
115-
this._apiHost === configToCompare._apiHost &&
116-
this._apiKey === configToCompare._apiKey &&
117-
this._pixelUrl === configToCompare._pixelUrl &&
118-
checkArrayEquality(this.segmentsToCheck, configToCompare._segmentsToCheck)
54+
this.apiHost === configToCompare.apiHost &&
55+
this.apiKey === configToCompare.apiKey &&
56+
this.pixelUrl === configToCompare.pixelUrl &&
57+
checkArrayEquality(this.segmentsToCheck, configToCompare.segmentsToCheck)
11958
);
12059
}
12160
}
61+
62+
export type OdpNotIntegratedConfig = {
63+
readonly integrated: false;
64+
}
65+
66+
export type OdpIntegratedConfig = {
67+
readonly integrated: true;
68+
readonly odpConfig: OdpConfig;
69+
}
70+
71+
export const odpIntegrationsAreEqual = (config1: OdpIntegrationConfig, config2: OdpIntegrationConfig): boolean => {
72+
if (config1.integrated !== config2.integrated) {
73+
return false;
74+
}
75+
76+
if (config1.integrated && config2.integrated) {
77+
return config1.odpConfig.equals(config2.odpConfig);
78+
}
79+
80+
return true;
81+
}
82+
83+
export type OdpIntegrationConfig = OdpNotIntegratedConfig | OdpIntegratedConfig;

Diff for: lib/core/odp/odp_event_api_manager.ts

+6-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022-2023, Optimizely
2+
* Copyright 2022-2024, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,16 +18,15 @@ import { LogHandler, LogLevel } from '../../modules/logging';
1818
import { OdpEvent } from './odp_event';
1919
import { RequestHandler } from '../../utils/http_request_handler/http';
2020
import { OdpConfig } from './odp_config';
21+
import { ERROR_MESSAGES } from '../../utils/enums';
2122

2223
const EVENT_SENDING_FAILURE_MESSAGE = 'ODP event send failed';
23-
export const ODP_CONFIG_NOT_READY_MESSAGE = 'ODP config not ready';
2424

2525
/**
2626
* Manager for communicating with the Optimizely Data Platform REST API
2727
*/
2828
export interface IOdpEventApiManager {
29-
sendEvents(events: OdpEvent[]): Promise<boolean>;
30-
updateSettings(odpConfig: OdpConfig): void;
29+
sendEvents(odpConfig: OdpConfig, events: OdpEvent[]): Promise<boolean>;
3130
}
3231

3332
/**
@@ -46,11 +45,6 @@ export abstract class OdpEventApiManager implements IOdpEventApiManager {
4645
*/
4746
private readonly requestHandler: RequestHandler;
4847

49-
/**
50-
* ODP configuration settings for identifying the target API and segments
51-
*/
52-
protected odpConfig?: OdpConfig;
53-
5448
/**
5549
* Creates instance to access Optimizely Data Platform (ODP) REST API
5650
* @param requestHandler Desired request handler for testing
@@ -61,14 +55,6 @@ export abstract class OdpEventApiManager implements IOdpEventApiManager {
6155
this.logger = logger;
6256
}
6357

64-
/**
65-
* Updates odpConfig of the api manager instance
66-
* @param odpConfig
67-
*/
68-
updateSettings(odpConfig: OdpConfig): void {
69-
this.odpConfig = odpConfig;
70-
}
71-
7258
getLogger(): LogHandler {
7359
return this.logger;
7460
}
@@ -78,14 +64,9 @@ export abstract class OdpEventApiManager implements IOdpEventApiManager {
7864
* @param events ODP events to send
7965
* @returns Retry is true - if network or server error (5xx), otherwise false
8066
*/
81-
async sendEvents(events: OdpEvent[]): Promise<boolean> {
67+
async sendEvents(odpConfig: OdpConfig, events: OdpEvent[]): Promise<boolean> {
8268
let shouldRetry = false;
8369

84-
if (!this.odpConfig?.isReady()) {
85-
this.logger.log(LogLevel.ERROR, `${EVENT_SENDING_FAILURE_MESSAGE} (${ODP_CONFIG_NOT_READY_MESSAGE})`);
86-
return shouldRetry;
87-
}
88-
8970
if (events.length === 0) {
9071
this.logger.log(LogLevel.ERROR, `${EVENT_SENDING_FAILURE_MESSAGE} (no events)`);
9172
return shouldRetry;
@@ -95,7 +76,7 @@ export abstract class OdpEventApiManager implements IOdpEventApiManager {
9576
return shouldRetry;
9677
}
9778

98-
const { method, endpoint, headers, data } = this.generateRequestData(events);
79+
const { method, endpoint, headers, data } = this.generateRequestData(odpConfig, events);
9980

10081
let statusCode = 0;
10182
try {
@@ -125,6 +106,7 @@ export abstract class OdpEventApiManager implements IOdpEventApiManager {
125106
protected abstract shouldSendEvents(events: OdpEvent[]): boolean;
126107

127108
protected abstract generateRequestData(
109+
odpConfig: OdpConfig,
128110
events: OdpEvent[]
129111
): {
130112
method: string;

0 commit comments

Comments
 (0)