From b7012f8c34e0596d96ef7a0805f1f368080baec4 Mon Sep 17 00:00:00 2001 From: Andy Leap Date: Thu, 18 Aug 2022 15:28:39 -0400 Subject: [PATCH 1/7] fix odp event --- optimizely/odp/odp_event.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/optimizely/odp/odp_event.py b/optimizely/odp/odp_event.py index 23015db5..ac3e5d93 100644 --- a/optimizely/odp/odp_event.py +++ b/optimizely/odp/odp_event.py @@ -13,15 +13,15 @@ from __future__ import annotations -from typing import Any, Dict +from typing import Any class OdpEvent: """ Representation of an odp event which can be sent to the Optimizely odp platform. """ def __init__(self, type: str, action: str, - identifiers: Dict[str, str], data: Dict[str, Any]) -> None: - self.type = type, - self.action = action, - self.identifiers = identifiers, + identifiers: dict[str, str], data: dict[str, Any]) -> None: + self.type = type + self.action = action + self.identifiers = identifiers self.data = data From 7ad4e7fbdb9c283757cb1b4041b24e3cac2c288d Mon Sep 17 00:00:00 2001 From: Andy Leap Date: Fri, 19 Aug 2022 09:50:37 -0400 Subject: [PATCH 2/7] add odp_config --- optimizely/odp/odp_config.py | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 optimizely/odp/odp_config.py diff --git a/optimizely/odp/odp_config.py b/optimizely/odp/odp_config.py new file mode 100644 index 00000000..ae4e932e --- /dev/null +++ b/optimizely/odp/odp_config.py @@ -0,0 +1,96 @@ +# Copyright 2022, Optimizely +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import annotations + +from typing import Optional +from threading import Lock + + +class OdpConfig: + """ + Contains configuration used for ODP integration. + + Args: + api_host: The host URL for the ODP audience segments API (optional). + api_key: The public API key for the ODP account from which the audience segments will be fetched (optional). + segments_to_check: An array of all ODP segments used in the current datafile + (associated with api_host/api_key). + """ + def __init__( + self, + api_key: Optional[str] = None, + api_host: Optional[str] = None, + segments_to_check: Optional[list[str]] = None + ) -> None: + self.api_key = api_key + self.api_host = api_host + self.segments_to_check = segments_to_check or [] + self.lock = Lock() + + def update(self, api_key: Optional[str], api_host: Optional[str], segments_to_check: list[str]) -> bool: + """ + Override the ODP configuration. + + Args: + api_host: The host URL for the ODP audience segments API (optional). + api_key: The public API key for the ODP account from which the audience segments will be fetched (optional). + segments_to_check: An array of all ODP segments used in the current datafile + (associated with api_host/api_key). + + Returns: + True if the provided values were different than the existing values. + """ + updated = False + with self.lock: + if self.api_key != api_key or self.api_host != api_host or self.segments_to_check != segments_to_check: + self.api_key = api_key + self.api_host = api_host + self.segments_to_check = segments_to_check + updated = True + + return updated + + def get_api_host(self) -> Optional[str]: + with self.lock: + return self.api_host + + def set_api_host(self, api_host: Optional[str]) -> None: + with self.lock: + self.api_host = api_host + + def get_api_key(self) -> Optional[str]: + with self.lock: + return self.api_key + + def set_api_key(self, api_key: Optional[str]): + with self.lock: + self.api_key = api_key + + def get_segments_to_check(self) -> Optional[list[str]]: + with self.lock: + return self.segments_to_check.copy() + + def set_segments_to_check(self, segments_to_check: list[str]): + """ + Replace qualified segments with provided list of segments + Args: + segments_to_check: A list of segment names + """ + with self.lock: + self.segments_to_check = segments_to_check.copy() + + def odp_integrated(self) -> bool: + """Returns True if ODP is integrated""" + with self.lock: + return self.api_key is not None and self.api_host is not None From 38e76643d1f1f17bdaa24fa2333b006d7a819310 Mon Sep 17 00:00:00 2001 From: Andy Leap Date: Fri, 19 Aug 2022 10:06:08 -0400 Subject: [PATCH 3/7] add tests --- tests/test_odp_config.py | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/test_odp_config.py diff --git a/tests/test_odp_config.py b/tests/test_odp_config.py new file mode 100644 index 00000000..1167d877 --- /dev/null +++ b/tests/test_odp_config.py @@ -0,0 +1,52 @@ +# Copyright 2022, Optimizely +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import annotations +from tests import base +from optimizely.odp.odp_config import OdpConfig + + +class OdpConfigTest(base.BaseTest): + api_host = 'test-host' + api_key = 'test-key' + segments_to_check = ['test-segment'] + + def test_init_config(self): + config = OdpConfig(self.api_key, self.api_host, self.segments_to_check) + + self.assertEqual(config.get_api_key(), self.api_key) + self.assertEqual(config.get_api_host(), self.api_host) + self.assertEqual(config.get_segments_to_check(), self.segments_to_check) + + def test_update_config(self): + config = OdpConfig() + updated = config.update(self.api_key, self.api_host, self.segments_to_check) + + self.assertStrictTrue(updated) + self.assertEqual(config.get_api_key(), self.api_key) + self.assertEqual(config.get_api_host(), self.api_host) + self.assertEqual(config.get_segments_to_check(), self.segments_to_check) + + updated = config.update(self.api_key, self.api_host, self.segments_to_check) + self.assertStrictFalse(updated) + + def test_config_setters(self): + config = OdpConfig() + + config.set_api_host(self.api_host) + config.set_api_key(self.api_key) + config.set_segments_to_check(self.segments_to_check) + + self.assertEqual(config.get_api_key(), self.api_key) + self.assertEqual(config.get_api_host(), self.api_host) + self.assertEqual(config.get_segments_to_check(), self.segments_to_check) From a9dd85a04dda813000f6666b7d86088a63245aa7 Mon Sep 17 00:00:00 2001 From: Andy Leap Date: Fri, 19 Aug 2022 10:20:13 -0400 Subject: [PATCH 4/7] fix types --- optimizely/odp/odp_config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/optimizely/odp/odp_config.py b/optimizely/odp/odp_config.py index ae4e932e..91cd34bb 100644 --- a/optimizely/odp/odp_config.py +++ b/optimizely/odp/odp_config.py @@ -73,7 +73,7 @@ def get_api_key(self) -> Optional[str]: with self.lock: return self.api_key - def set_api_key(self, api_key: Optional[str]): + def set_api_key(self, api_key: Optional[str]) -> None: with self.lock: self.api_key = api_key @@ -81,7 +81,7 @@ def get_segments_to_check(self) -> Optional[list[str]]: with self.lock: return self.segments_to_check.copy() - def set_segments_to_check(self, segments_to_check: list[str]): + def set_segments_to_check(self, segments_to_check: list[str]) -> None: """ Replace qualified segments with provided list of segments Args: From 49c0c7cbf2a98616b31a3bd4cc4ebcc4e0195254 Mon Sep 17 00:00:00 2001 From: Andy Leap Date: Fri, 19 Aug 2022 12:41:24 -0400 Subject: [PATCH 5/7] address comments --- optimizely/odp/odp_config.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/optimizely/odp/odp_config.py b/optimizely/odp/odp_config.py index 91cd34bb..aec19916 100644 --- a/optimizely/odp/odp_config.py +++ b/optimizely/odp/odp_config.py @@ -24,7 +24,7 @@ class OdpConfig: Args: api_host: The host URL for the ODP audience segments API (optional). api_key: The public API key for the ODP account from which the audience segments will be fetched (optional). - segments_to_check: An array of all ODP segments used in the current datafile + segments_to_check: A list of all ODP segments used in the current datafile (associated with api_host/api_key). """ def __init__( @@ -45,7 +45,7 @@ def update(self, api_key: Optional[str], api_host: Optional[str], segments_to_ch Args: api_host: The host URL for the ODP audience segments API (optional). api_key: The public API key for the ODP account from which the audience segments will be fetched (optional). - segments_to_check: An array of all ODP segments used in the current datafile + segments_to_check: A list of all ODP segments used in the current datafile (associated with api_host/api_key). Returns: @@ -83,14 +83,14 @@ def get_segments_to_check(self) -> Optional[list[str]]: def set_segments_to_check(self, segments_to_check: list[str]) -> None: """ - Replace qualified segments with provided list of segments + Replace qualified segments with provided list of segments. Args: - segments_to_check: A list of segment names + segments_to_check: A list of segment names. """ with self.lock: self.segments_to_check = segments_to_check.copy() def odp_integrated(self) -> bool: - """Returns True if ODP is integrated""" + """Returns True if ODP is integrated.""" with self.lock: return self.api_key is not None and self.api_host is not None From 32d12cc287ff8968ed5ef6022dff37a5d98d0f8a Mon Sep 17 00:00:00 2001 From: Andy Leap Date: Mon, 22 Aug 2022 13:30:19 -0400 Subject: [PATCH 6/7] remove setters --- optimizely/odp/odp_config.py | 17 ----------------- tests/test_odp_config.py | 11 ----------- 2 files changed, 28 deletions(-) diff --git a/optimizely/odp/odp_config.py b/optimizely/odp/odp_config.py index aec19916..f571fd14 100644 --- a/optimizely/odp/odp_config.py +++ b/optimizely/odp/odp_config.py @@ -65,31 +65,14 @@ def get_api_host(self) -> Optional[str]: with self.lock: return self.api_host - def set_api_host(self, api_host: Optional[str]) -> None: - with self.lock: - self.api_host = api_host - def get_api_key(self) -> Optional[str]: with self.lock: return self.api_key - def set_api_key(self, api_key: Optional[str]) -> None: - with self.lock: - self.api_key = api_key - def get_segments_to_check(self) -> Optional[list[str]]: with self.lock: return self.segments_to_check.copy() - def set_segments_to_check(self, segments_to_check: list[str]) -> None: - """ - Replace qualified segments with provided list of segments. - Args: - segments_to_check: A list of segment names. - """ - with self.lock: - self.segments_to_check = segments_to_check.copy() - def odp_integrated(self) -> bool: """Returns True if ODP is integrated.""" with self.lock: diff --git a/tests/test_odp_config.py b/tests/test_odp_config.py index 1167d877..d72a7321 100644 --- a/tests/test_odp_config.py +++ b/tests/test_odp_config.py @@ -39,14 +39,3 @@ def test_update_config(self): updated = config.update(self.api_key, self.api_host, self.segments_to_check) self.assertStrictFalse(updated) - - def test_config_setters(self): - config = OdpConfig() - - config.set_api_host(self.api_host) - config.set_api_key(self.api_key) - config.set_segments_to_check(self.segments_to_check) - - self.assertEqual(config.get_api_key(), self.api_key) - self.assertEqual(config.get_api_host(), self.api_host) - self.assertEqual(config.get_segments_to_check(), self.segments_to_check) From 7ca0a66699119068f2c0a556ee531a8e06d6e536 Mon Sep 17 00:00:00 2001 From: Andy Leap Date: Mon, 22 Aug 2022 13:33:34 -0400 Subject: [PATCH 7/7] make attributes private --- optimizely/odp/odp_config.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/optimizely/odp/odp_config.py b/optimizely/odp/odp_config.py index f571fd14..64809626 100644 --- a/optimizely/odp/odp_config.py +++ b/optimizely/odp/odp_config.py @@ -33,9 +33,9 @@ def __init__( api_host: Optional[str] = None, segments_to_check: Optional[list[str]] = None ) -> None: - self.api_key = api_key - self.api_host = api_host - self.segments_to_check = segments_to_check or [] + self._api_key = api_key + self._api_host = api_host + self._segments_to_check = segments_to_check or [] self.lock = Lock() def update(self, api_key: Optional[str], api_host: Optional[str], segments_to_check: list[str]) -> bool: @@ -53,27 +53,27 @@ def update(self, api_key: Optional[str], api_host: Optional[str], segments_to_ch """ updated = False with self.lock: - if self.api_key != api_key or self.api_host != api_host or self.segments_to_check != segments_to_check: - self.api_key = api_key - self.api_host = api_host - self.segments_to_check = segments_to_check + if self._api_key != api_key or self._api_host != api_host or self._segments_to_check != segments_to_check: + self._api_key = api_key + self._api_host = api_host + self._segments_to_check = segments_to_check updated = True return updated def get_api_host(self) -> Optional[str]: with self.lock: - return self.api_host + return self._api_host def get_api_key(self) -> Optional[str]: with self.lock: - return self.api_key + return self._api_key - def get_segments_to_check(self) -> Optional[list[str]]: + def get_segments_to_check(self) -> list[str]: with self.lock: - return self.segments_to_check.copy() + return self._segments_to_check.copy() def odp_integrated(self) -> bool: """Returns True if ODP is integrated.""" with self.lock: - return self.api_key is not None and self.api_host is not None + return self._api_key is not None and self._api_host is not None