-
Notifications
You must be signed in to change notification settings - Fork 36
feat: add odp config #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add odp config #401
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b7012f8
fix odp event
andrewleap-optimizely 7ad4e7f
add odp_config
andrewleap-optimizely 38e7664
add tests
andrewleap-optimizely a9dd85a
fix types
andrewleap-optimizely 49c0c7c
address comments
andrewleap-optimizely 32d12cc
remove setters
andrewleap-optimizely 7ca0a66
make attributes private
andrewleap-optimizely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# 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: A list 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: A list 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 get_api_key(self) -> Optional[str]: | ||
with self.lock: | ||
return self._api_key | ||
|
||
def get_segments_to_check(self) -> list[str]: | ||
with self.lock: | ||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.