Skip to content

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 7 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions optimizely/odp/odp_config.py
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
10 changes: 5 additions & 5 deletions optimizely/odp/odp_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 41 additions & 0 deletions tests/test_odp_config.py
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)