Skip to content

Commit 81a5bfe

Browse files
feat: add odp config (#401)
* add odp_config * fix odp event
1 parent 9a01056 commit 81a5bfe

File tree

3 files changed

+125
-5
lines changed

3 files changed

+125
-5
lines changed

optimizely/odp/odp_config.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

optimizely/odp/odp_event.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
from __future__ import annotations
1515

16-
from typing import Any, Dict
16+
from typing import Any
1717

1818

1919
class OdpEvent:
2020
""" Representation of an odp event which can be sent to the Optimizely odp platform. """
2121

2222
def __init__(self, type: str, action: str,
23-
identifiers: Dict[str, str], data: Dict[str, Any]) -> None:
24-
self.type = type,
25-
self.action = action,
26-
self.identifiers = identifiers,
23+
identifiers: dict[str, str], data: dict[str, Any]) -> None:
24+
self.type = type
25+
self.action = action
26+
self.identifiers = identifiers
2727
self.data = data

tests/test_odp_config.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
from tests import base
16+
from optimizely.odp.odp_config import OdpConfig
17+
18+
19+
class OdpConfigTest(base.BaseTest):
20+
api_host = 'test-host'
21+
api_key = 'test-key'
22+
segments_to_check = ['test-segment']
23+
24+
def test_init_config(self):
25+
config = OdpConfig(self.api_key, self.api_host, self.segments_to_check)
26+
27+
self.assertEqual(config.get_api_key(), self.api_key)
28+
self.assertEqual(config.get_api_host(), self.api_host)
29+
self.assertEqual(config.get_segments_to_check(), self.segments_to_check)
30+
31+
def test_update_config(self):
32+
config = OdpConfig()
33+
updated = config.update(self.api_key, self.api_host, self.segments_to_check)
34+
35+
self.assertStrictTrue(updated)
36+
self.assertEqual(config.get_api_key(), self.api_key)
37+
self.assertEqual(config.get_api_host(), self.api_host)
38+
self.assertEqual(config.get_segments_to_check(), self.segments_to_check)
39+
40+
updated = config.update(self.api_key, self.api_host, self.segments_to_check)
41+
self.assertStrictFalse(updated)

0 commit comments

Comments
 (0)