Skip to content

Commit eb3edf9

Browse files
feat: add plan to config (#26)
* add plan to config * update plugin test to cover plan in context plugin
1 parent f50042f commit eb3edf9

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/amplitude/config.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import Optional, Callable
99

1010
from amplitude import constants
11-
from amplitude.event import BaseEvent
11+
from amplitude.event import BaseEvent, Plan
1212
from amplitude.storage import InMemoryStorageProvider, StorageProvider, Storage
1313

1414

@@ -21,6 +21,7 @@ class Config:
2121
Must be positive.
2222
flush_interval_millis (int, optional): The events buffered in memory will wait no longer than
2323
flush_interval_millis. Must be positive.
24+
flush_max_retries (int, optional): The maximum retry attempts for an event when receiving error response.
2425
logger (optional): The logger used by Amplitude client. Default to logging.getLogger(constants.LOGGER_NAME).
2526
min_id_length (int, optional): The minimum length of user_id and device_id for events. Default to 5.
2627
callback (callable, optional): The client level callback function. Triggered on every events sent or failed.
@@ -29,8 +30,9 @@ class Config:
2930
use_batch(bool, optional): True to use batch API endpoint, False to use HTTP V2 API endpoint. Default to False.
3031
server_url (str, optional): API endpoint url. Default to None. Auto selected by configured server_zone
3132
and use_batch if set to None. Support customized url by setting string value.
32-
storage_provider(amplitude.storage.StorageProvider, optional): Default to InMemoryStorageProvider.
33+
storage_provider (amplitude.storage.StorageProvider, optional): Default to InMemoryStorageProvider.
3334
Provide storage instance for events buffer.
35+
plan (amplitude.event.Plan, optional): Tracking plan information. Default to None.
3436
3537
Properties:
3638
options: A dictionary contains minimum id length information. None if min_id_length not set.
@@ -52,9 +54,10 @@ def __init__(self, api_key: str = None,
5254
server_zone: str = constants.DEFAULT_ZONE,
5355
use_batch: bool = False,
5456
server_url: Optional[str] = None,
55-
storage_provider: StorageProvider = InMemoryStorageProvider()):
57+
storage_provider: StorageProvider = InMemoryStorageProvider(),
58+
plan: Plan = None):
5659
"""The constructor of Config class"""
57-
self.api_key = api_key
60+
self.api_key: str = api_key
5861
self._flush_queue_size: int = flush_queue_size
5962
self._flush_size_divider: int = 1
6063
self.flush_interval_millis: int = flush_interval_millis
@@ -66,7 +69,8 @@ def __init__(self, api_key: str = None,
6669
self.use_batch: bool = use_batch
6770
self._url: Optional[str] = server_url
6871
self.storage_provider: StorageProvider = storage_provider
69-
self.opt_out = False
72+
self.opt_out: bool = False
73+
self.plan: Plan = plan
7074

7175
def get_storage(self) -> Storage:
7276
"""Use configured StorageProvider to create a Storage instance then return.

src/amplitude/plugin.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ def __init__(self):
187187
"""The constructor of ContextPlugin class"""
188188
super().__init__(constants.PluginType.BEFORE)
189189
self.context_string = f"{constants.SDK_LIBRARY}/{constants.SDK_VERSION}"
190+
self.configuration = None
191+
192+
def setup(self, client):
193+
self.configuration = client.configuration
190194

191195
def apply_context_data(self, event: BaseEvent):
192196
"""Add SDK name and version to event.library.
@@ -203,9 +207,11 @@ def execute(self, event: BaseEvent) -> BaseEvent:
203207
event (BaseEvent): The event to be processed.
204208
"""
205209
if not event.time:
206-
event.time = utils.current_milliseconds()
210+
event["time"] = utils.current_milliseconds()
207211
if not event.insert_id:
208-
event.insert_id = str(uuid.uuid4())
212+
event["insert_id"] = str(uuid.uuid4())
213+
if self.configuration.plan and (not event.plan):
214+
event["plan"] = self.configuration.plan
209215
self.apply_context_data(event)
210216
return event
211217

src/test/test_plugin.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest.mock import MagicMock
33

44
from amplitude.plugin import AmplitudeDestinationPlugin, ContextPlugin, EventPlugin, DestinationPlugin
5-
from amplitude import Amplitude, PluginType, BaseEvent, RevenueEvent, IdentifyEvent, GroupIdentifyEvent, Config
5+
from amplitude import Amplitude, PluginType, BaseEvent, RevenueEvent, IdentifyEvent, GroupIdentifyEvent, Config, Plan
66

77

88
class AmplitudePluginTestCase(unittest.TestCase):
@@ -25,14 +25,18 @@ def test_plugin_initialize_amplitude_client_context_plugin_creation_success(self
2525

2626
def test_plugin_context_plugin_execute_event_success(self):
2727
context_plugin = ContextPlugin()
28+
context_plugin.setup(Amplitude("test_api_key"))
29+
context_plugin.configuration.plan = Plan(source="test_source")
2830
event = BaseEvent("test_event", user_id="test_user")
2931
self.assertIsNone(event.time)
3032
self.assertIsNone(event.insert_id)
3133
self.assertIsNone(event.library)
34+
self.assertIsNone(event.plan)
3235
context_plugin.execute(event)
3336
self.assertTrue(isinstance(event.time, int))
3437
self.assertTrue(isinstance(event.insert_id, str))
3538
self.assertTrue(isinstance(event.library, str))
39+
self.assertTrue(isinstance(event.plan, Plan))
3640

3741
def test_plugin_event_plugin_process_event_success(self):
3842
plugin = EventPlugin(PluginType.BEFORE)
@@ -50,6 +54,7 @@ def test_plugin_destination_plugin_add_remove_plugin_success(self):
5054
destination_plugin = DestinationPlugin()
5155
destination_plugin.timeline.configuration = Config()
5256
context_plugin = ContextPlugin()
57+
context_plugin.configuration = destination_plugin.timeline.configuration
5358
event = BaseEvent("test_event", user_id="test_user")
5459
destination_plugin.add(context_plugin)
5560
destination_plugin.execute(event)

0 commit comments

Comments
 (0)