|
| 1 | +/** |
| 2 | + * Copyright 2022, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +export class OdpConfig { |
| 18 | + /** |
| 19 | + * Host of ODP audience segments API. |
| 20 | + * @private |
| 21 | + */ |
| 22 | + private _apiHost: string; |
| 23 | + |
| 24 | + /** |
| 25 | + * Getter to retrieve the ODP server host |
| 26 | + * @public |
| 27 | + */ |
| 28 | + public get apiHost(): string { |
| 29 | + return this._apiHost; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Public API key for the ODP account from which the audience segments will be fetched (optional). |
| 34 | + * @private |
| 35 | + */ |
| 36 | + private _apiKey: string; |
| 37 | + |
| 38 | + /** |
| 39 | + * Getter to retrieve the ODP API key |
| 40 | + * @public |
| 41 | + */ |
| 42 | + public get apiKey(): string { |
| 43 | + return this._apiKey; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * All ODP segments used in the current datafile (associated with apiHost/apiKey). |
| 48 | + * @private |
| 49 | + */ |
| 50 | + private _segmentsToCheck: string[]; |
| 51 | + |
| 52 | + /** |
| 53 | + * Getter for ODP segments to check |
| 54 | + * @public |
| 55 | + */ |
| 56 | + public get segmentsToCheck(): string[] { |
| 57 | + return this._segmentsToCheck; |
| 58 | + } |
| 59 | + |
| 60 | + constructor(apiKey: string, apiHost: string, segmentsToCheck?: string[]) { |
| 61 | + this._apiKey = apiKey; |
| 62 | + this._apiHost = apiHost; |
| 63 | + this._segmentsToCheck = segmentsToCheck ?? []; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Update the ODP configuration details |
| 68 | + * @param apiKey Public API key for the ODP account |
| 69 | + * @param apiHost Host of ODP audience segments API |
| 70 | + * @param segmentsToCheck Audience segments |
| 71 | + * @returns true if configuration was updated successfully |
| 72 | + */ |
| 73 | + public update(apiKey: string, apiHost: string, segmentsToCheck: string[]): boolean { |
| 74 | + if (this._apiKey === apiKey && this._apiHost === apiHost && this._segmentsToCheck === segmentsToCheck) { |
| 75 | + return false; |
| 76 | + } else { |
| 77 | + this._apiKey = apiKey; |
| 78 | + this._apiHost = apiHost; |
| 79 | + this._segmentsToCheck = segmentsToCheck; |
| 80 | + |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Determines if ODP configuration has the minimum amount of information |
| 87 | + */ |
| 88 | + public isReady(): boolean { |
| 89 | + return !!this._apiKey && !!this._apiHost; |
| 90 | + } |
| 91 | +} |
0 commit comments