|
| 1 | +# Copyright 2022, Optimizely |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from typing import Optional |
| 17 | +from threading import Lock |
| 18 | + |
| 19 | + |
| 20 | +class OdpConfig: |
| 21 | + """ |
| 22 | + Contains configuration used for ODP integration. |
| 23 | +
|
| 24 | + Args: |
| 25 | + api_host: The host URL for the ODP audience segments API (optional). |
| 26 | + api_key: The public API key for the ODP account from which the audience segments will be fetched (optional). |
| 27 | + segments_to_check: A list of all ODP segments used in the current datafile |
| 28 | + (associated with api_host/api_key). |
| 29 | + """ |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + api_key: Optional[str] = None, |
| 33 | + api_host: Optional[str] = None, |
| 34 | + segments_to_check: Optional[list[str]] = None |
| 35 | + ) -> None: |
| 36 | + self._api_key = api_key |
| 37 | + self._api_host = api_host |
| 38 | + self._segments_to_check = segments_to_check or [] |
| 39 | + self.lock = Lock() |
| 40 | + |
| 41 | + def update(self, api_key: Optional[str], api_host: Optional[str], segments_to_check: list[str]) -> bool: |
| 42 | + """ |
| 43 | + Override the ODP configuration. |
| 44 | +
|
| 45 | + Args: |
| 46 | + api_host: The host URL for the ODP audience segments API (optional). |
| 47 | + api_key: The public API key for the ODP account from which the audience segments will be fetched (optional). |
| 48 | + segments_to_check: A list of all ODP segments used in the current datafile |
| 49 | + (associated with api_host/api_key). |
| 50 | +
|
| 51 | + Returns: |
| 52 | + True if the provided values were different than the existing values. |
| 53 | + """ |
| 54 | + updated = False |
| 55 | + with self.lock: |
| 56 | + if self._api_key != api_key or self._api_host != api_host or self._segments_to_check != segments_to_check: |
| 57 | + self._api_key = api_key |
| 58 | + self._api_host = api_host |
| 59 | + self._segments_to_check = segments_to_check |
| 60 | + updated = True |
| 61 | + |
| 62 | + return updated |
| 63 | + |
| 64 | + def get_api_host(self) -> Optional[str]: |
| 65 | + with self.lock: |
| 66 | + return self._api_host |
| 67 | + |
| 68 | + def get_api_key(self) -> Optional[str]: |
| 69 | + with self.lock: |
| 70 | + return self._api_key |
| 71 | + |
| 72 | + def get_segments_to_check(self) -> list[str]: |
| 73 | + with self.lock: |
| 74 | + return self._segments_to_check.copy() |
| 75 | + |
| 76 | + def odp_integrated(self) -> bool: |
| 77 | + """Returns True if ODP is integrated.""" |
| 78 | + with self.lock: |
| 79 | + return self._api_key is not None and self._api_host is not None |
0 commit comments