wingify-fme-python-sdk 1.70.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tests/data/__init__.py +13 -0
- tests/data/dummy_test_data_reader.py +63 -0
- tests/data/user_storage.py +48 -0
- tests/e2e/__init__.py +13 -0
- tests/e2e/custom_bucketing_test.py +514 -0
- tests/e2e/force_users_test.py +368 -0
- tests/e2e/get_flag_test.py +195 -0
- tests/unit/__init__.py +13 -0
- tests/unit/packages/__init__.py +13 -0
- tests/unit/packages/decision_maker/__init__.py +13 -0
- tests/unit/packages/decision_maker/decision_maker_test.py +83 -0
- tests/unit/packages/segmentation_evaluator/__init__.py +13 -0
- tests/unit/packages/segmentation_evaluator/segmentation_evaluator_test.py +284 -0
- tests/unit/packages/segmentation_evaluator/segmentation_manager_test.py +215 -0
- tests/unit/packages/segmentation_evaluator/web_testing_segment_test.py +216 -0
- tests/unit/services/__init__.py +0 -0
- tests/unit/services/hooks_manager_test.py +80 -0
- tests/unit/services/internal_events_sampling_service_test.py +160 -0
- tests/unit/settings/__init__.py +13 -0
- tests/unit/settings/settings_schema_validation_test.py +67 -0
- tests/unit/utils/__init__.py +0 -0
- tests/unit/utils/campaign_util_test.py +248 -0
- tests/unit/utils/decision_util_force_users_test.py +790 -0
- tests/unit/utils/log_message_test.py +65 -0
- tests/unit/vwo_wingify_test.py +175 -0
- tests/unit/wingify_client_validation_test.py +72 -0
- vwo/__init__.py +35 -0
- vwo/models/__init__.py +13 -0
- vwo/models/batch_event_data.py +19 -0
- vwo/models/user/__init__.py +13 -0
- vwo/models/user/context_vwo_model.py +21 -0
- vwo/models/user/get_flag.py +19 -0
- vwo/models/vwo_options_model.py +21 -0
- vwo/packages/__init__.py +13 -0
- vwo/packages/logger/__init__.py +13 -0
- vwo/packages/logger/enums/__init__.py +13 -0
- vwo/packages/logger/enums/log_level_enum.py +19 -0
- vwo/packages/storage/__init__.py +13 -0
- vwo/packages/storage/connector.py +19 -0
- vwo/vwo.py +58 -0
- vwo/vwo_builder.py +21 -0
- vwo/vwo_client.py +22 -0
- wingify/__init__.py +36 -0
- wingify/api/__init__.py +13 -0
- wingify/api/get_flag_api.py +727 -0
- wingify/api/set_attribute_api.py +60 -0
- wingify/api/track_api.py +99 -0
- wingify/constants/Constants.py +94 -0
- wingify/constants/__init__.py +13 -0
- wingify/constants/sampled_debug_error_template_keys.py +34 -0
- wingify/decorators/__init__.py +13 -0
- wingify/decorators/storage_decorator.py +92 -0
- wingify/enums/__init__.py +13 -0
- wingify/enums/api_enum.py +26 -0
- wingify/enums/campaign_type_enum.py +22 -0
- wingify/enums/debug_category_enum.py +24 -0
- wingify/enums/event_enum.py +26 -0
- wingify/enums/headers_enum.py +21 -0
- wingify/enums/hooks_enum.py +24 -0
- wingify/enums/status_enum.py +21 -0
- wingify/enums/storage_enum.py +25 -0
- wingify/enums/url_enum.py +29 -0
- wingify/models/Alias.py +60 -0
- wingify/models/AliasSetResponse.py +43 -0
- wingify/models/__init__.py +13 -0
- wingify/models/batch_event_data.py +68 -0
- wingify/models/campaign/__init__.py +13 -0
- wingify/models/campaign/campaign_model.py +140 -0
- wingify/models/campaign/feature_model.py +95 -0
- wingify/models/campaign/holdout_model.py +94 -0
- wingify/models/campaign/impact_campaign_model.py +60 -0
- wingify/models/campaign/metric_model.py +42 -0
- wingify/models/campaign/rule_model.py +42 -0
- wingify/models/campaign/variable_model.py +44 -0
- wingify/models/campaign/variation_model.py +109 -0
- wingify/models/gateway_service_model.py +48 -0
- wingify/models/schemas/__init__.py +13 -0
- wingify/models/schemas/campaign_group_schema.py +19 -0
- wingify/models/schemas/campaign_metric_schema.py +26 -0
- wingify/models/schemas/campaign_schema.py +37 -0
- wingify/models/schemas/campaign_variation_schema.py +34 -0
- wingify/models/schemas/empty_object_schema.py +15 -0
- wingify/models/schemas/feature_schema.py +38 -0
- wingify/models/schemas/group_schema.py +37 -0
- wingify/models/schemas/holdout_schema.py +29 -0
- wingify/models/schemas/rule_schema.py +24 -0
- wingify/models/schemas/settings_schema.py +47 -0
- wingify/models/schemas/variable_schema.py +24 -0
- wingify/models/settings/__init__.py +13 -0
- wingify/models/settings/internal_event_sampling_settings_model.py +53 -0
- wingify/models/settings/settings_model.py +127 -0
- wingify/models/storage/__init__.py +13 -0
- wingify/models/storage/storage_model.py +84 -0
- wingify/models/user/__init__.py +13 -0
- wingify/models/user/context_model.py +113 -0
- wingify/models/user/context_vwo_model.py +34 -0
- wingify/models/user/get_flag.py +47 -0
- wingify/models/vwo_options_model.py +21 -0
- wingify/models/wingify_options_model.py +129 -0
- wingify/packages/__init__.py +13 -0
- wingify/packages/decision_maker/__init__.py +13 -0
- wingify/packages/decision_maker/decision_maker.py +89 -0
- wingify/packages/logger/__init__.py +13 -0
- wingify/packages/logger/core/__init__.py +13 -0
- wingify/packages/logger/core/log_manager.py +123 -0
- wingify/packages/logger/core/log_transport_manager.py +57 -0
- wingify/packages/logger/enums/__init__.py +13 -0
- wingify/packages/logger/enums/log_level_enum.py +24 -0
- wingify/packages/logger/enums/log_level_number_enum.py +21 -0
- wingify/packages/logger/log_message_builder.py +70 -0
- wingify/packages/logger/logger.py +38 -0
- wingify/packages/logger/transports/__init__.py +13 -0
- wingify/packages/logger/transports/console_transport.py +43 -0
- wingify/packages/network_layer/__init__.py +13 -0
- wingify/packages/network_layer/client/__init__.py +13 -0
- wingify/packages/network_layer/client/network_client.py +167 -0
- wingify/packages/network_layer/handlers/__init__.py +13 -0
- wingify/packages/network_layer/handlers/request_handler.py +40 -0
- wingify/packages/network_layer/manager/__init__.py +13 -0
- wingify/packages/network_layer/manager/network_manager.py +79 -0
- wingify/packages/network_layer/models/__init__.py +13 -0
- wingify/packages/network_layer/models/global_request_model.py +70 -0
- wingify/packages/network_layer/models/request_model.py +133 -0
- wingify/packages/network_layer/models/response_model.py +55 -0
- wingify/packages/segmentation_evaluator/__init__.py +13 -0
- wingify/packages/segmentation_evaluator/core/__init__.py +13 -0
- wingify/packages/segmentation_evaluator/core/segmentation_manager.py +140 -0
- wingify/packages/segmentation_evaluator/enums/__init__.py +13 -0
- wingify/packages/segmentation_evaluator/enums/segment_operand_regex_enum.py +31 -0
- wingify/packages/segmentation_evaluator/enums/segment_operand_value_enum.py +29 -0
- wingify/packages/segmentation_evaluator/enums/segment_operator_value_enum.py +37 -0
- wingify/packages/segmentation_evaluator/evaluators/__init__.py +13 -0
- wingify/packages/segmentation_evaluator/evaluators/segment_evaluator.py +237 -0
- wingify/packages/segmentation_evaluator/evaluators/segment_operand_evaluator.py +518 -0
- wingify/packages/segmentation_evaluator/utils/__init__.py +13 -0
- wingify/packages/segmentation_evaluator/utils/segment_util.py +33 -0
- wingify/packages/segmentation_evaluator/utils/web_testing_segment_util.py +295 -0
- wingify/packages/storage/__init__.py +13 -0
- wingify/packages/storage/connector.py +25 -0
- wingify/packages/storage/storage.py +39 -0
- wingify/resources/__init__.py +13 -0
- wingify/resources/debug-messages.json +21 -0
- wingify/resources/error-messages.json +62 -0
- wingify/resources/info-message.json +47 -0
- wingify/resources/trace-messages.json +1 -0
- wingify/resources/warn-messages.json +1 -0
- wingify/services/__init__.py +13 -0
- wingify/services/batch_event_queue.py +140 -0
- wingify/services/campaign_decision_service.py +268 -0
- wingify/services/hooks_manager.py +65 -0
- wingify/services/internal_events_sampling_service.py +62 -0
- wingify/services/settings_manager.py +293 -0
- wingify/services/storage_service.py +58 -0
- wingify/services/url_service.py +51 -0
- wingify/utils/__init__.py +13 -0
- wingify/utils/aliasing_util.py +108 -0
- wingify/utils/brand_context.py +26 -0
- wingify/utils/brand_util.py +37 -0
- wingify/utils/campaign_util.py +592 -0
- wingify/utils/data_type_util.py +125 -0
- wingify/utils/debugger_service_util.py +58 -0
- wingify/utils/decision_util.py +589 -0
- wingify/utils/event_util.py +85 -0
- wingify/utils/function_util.py +188 -0
- wingify/utils/gateway_service_util.py +179 -0
- wingify/utils/holdout_util.py +264 -0
- wingify/utils/impression_util.py +151 -0
- wingify/utils/internal_events_sampling_util.py +106 -0
- wingify/utils/log_message_util.py +95 -0
- wingify/utils/meg_util.py +561 -0
- wingify/utils/model_utils.py +136 -0
- wingify/utils/network_util.py +706 -0
- wingify/utils/rule_evaluation_util.py +102 -0
- wingify/utils/settings_util.py +39 -0
- wingify/utils/usage_stats_util.py +116 -0
- wingify/utils/uuid_util.py +81 -0
- wingify/vwo_client.py +21 -0
- wingify/wingify.py +159 -0
- wingify/wingify_builder.py +282 -0
- wingify/wingify_client.py +520 -0
- wingify_fme_python_sdk-1.70.0.dist-info/METADATA +1415 -0
- wingify_fme_python_sdk-1.70.0.dist-info/RECORD +186 -0
- wingify_fme_python_sdk-1.70.0.dist-info/WHEEL +5 -0
- wingify_fme_python_sdk-1.70.0.dist-info/licenses/LICENSE +202 -0
- wingify_fme_python_sdk-1.70.0.dist-info/licenses/NOTICE +18 -0
- wingify_fme_python_sdk-1.70.0.dist-info/top_level.txt +3 -0
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
# Copyright 2024-2026 Wingify Software Pvt. Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
from wingify.services.batch_event_queue import BatchEventQueue
|
|
17
|
+
from .models.settings.settings_model import SettingsModel
|
|
18
|
+
from .utils.settings_util import set_settings_and_add_campaigns_to_rules
|
|
19
|
+
from .services.url_service import UrlService
|
|
20
|
+
from .packages.logger.core.log_manager import LogManager
|
|
21
|
+
from .utils.log_message_util import info_messages, error_messages, debug_messages
|
|
22
|
+
from .services.hooks_manager import HooksManager
|
|
23
|
+
from .models.user.context_model import ContextModel
|
|
24
|
+
from .models.user.get_flag import GetFlag
|
|
25
|
+
from .api.get_flag_api import GetFlagApi
|
|
26
|
+
from .api.track_api import TrackApi
|
|
27
|
+
from .api.set_attribute_api import SetAttributeApi
|
|
28
|
+
from typing import Dict, Any
|
|
29
|
+
from .utils.data_type_util import is_string, is_object, is_boolean
|
|
30
|
+
from .services.settings_manager import SettingsManager
|
|
31
|
+
from .enums.api_enum import ApiEnum
|
|
32
|
+
from .utils.aliasing_util import get_alias_user_id
|
|
33
|
+
from .utils.aliasing_util import set_alias as set_user_alias_util
|
|
34
|
+
from .utils.uuid_util import is_web_uuid, get_uuid
|
|
35
|
+
from .utils.function_util import get_current_unix_timestamp
|
|
36
|
+
|
|
37
|
+
class WingifyClient:
|
|
38
|
+
"""
|
|
39
|
+
Canonical FME SDK client. All feature evaluation, tracking, and attribute APIs
|
|
40
|
+
are implemented here. Legacy VWO integrations use VWOClient (alias) from vwo_client.py.
|
|
41
|
+
"""
|
|
42
|
+
_settings: SettingsModel = None
|
|
43
|
+
original_settings: Dict = None
|
|
44
|
+
batch_event_queue: BatchEventQueue = None
|
|
45
|
+
_client_instance = None
|
|
46
|
+
|
|
47
|
+
def __init__(self, settings: str, options: Dict):
|
|
48
|
+
self.options = options
|
|
49
|
+
if settings is None or settings == {}:
|
|
50
|
+
return
|
|
51
|
+
set_settings_and_add_campaigns_to_rules(settings, self)
|
|
52
|
+
|
|
53
|
+
# Singleton — one active client per process
|
|
54
|
+
if WingifyClient._client_instance is None:
|
|
55
|
+
WingifyClient._client_instance = self
|
|
56
|
+
|
|
57
|
+
UrlService(self._settings.get_collection_prefix())
|
|
58
|
+
|
|
59
|
+
LogManager.get_instance().info(info_messages.get("CLIENT_INITIALIZED"))
|
|
60
|
+
|
|
61
|
+
@staticmethod
|
|
62
|
+
def get_instance():
|
|
63
|
+
"""Return the singleton WingifyClient instance, if initialized."""
|
|
64
|
+
return WingifyClient._client_instance
|
|
65
|
+
|
|
66
|
+
# Getter for the batch_event_queue
|
|
67
|
+
def get_batch_event_queue(self):
|
|
68
|
+
return self._batch_event_queue
|
|
69
|
+
|
|
70
|
+
# Setter for the batch_event_queue
|
|
71
|
+
def set_batch_event_queue(self, value):
|
|
72
|
+
self._batch_event_queue = value
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def get_flag(self, feature_key: str, context: Dict) -> GetFlag:
|
|
76
|
+
"""
|
|
77
|
+
Retrieves the value of a feature flag for a given feature key and context.
|
|
78
|
+
This method validates the feature key and context, ensures the settings are valid, and then uses the FlagApi to get the flag value.
|
|
79
|
+
|
|
80
|
+
:param feature_key: The key of the feature to retrieve.
|
|
81
|
+
:param context: The context in which the feature flag is being retrieved, must include a valid user ID.
|
|
82
|
+
:return: The feature flag value.
|
|
83
|
+
"""
|
|
84
|
+
api_name = "getFlag"
|
|
85
|
+
uuid = None
|
|
86
|
+
try:
|
|
87
|
+
LogManager.get_instance().debug(
|
|
88
|
+
debug_messages.get("API_CALLED").format(apiName=api_name)
|
|
89
|
+
)
|
|
90
|
+
self._validate_context(context, api_name)
|
|
91
|
+
# get uuid from context
|
|
92
|
+
uuid = self._get_uuid_from_context(context, api_name)
|
|
93
|
+
except Exception as err:
|
|
94
|
+
LogManager.get_instance().error_log("EXECUTION_FAILED", data={"apiName": api_name, "err": str(err)}, debug_data={"an": ApiEnum.GET_FLAG.value})
|
|
95
|
+
return GetFlag(is_enabled=False, variables=[], session_id=context.get("session_id", get_current_unix_timestamp()), uuid=uuid)
|
|
96
|
+
|
|
97
|
+
get_flag_response = GetFlag(is_enabled=False, variables=[], session_id=context.get("session_id", get_current_unix_timestamp()), uuid=uuid)
|
|
98
|
+
|
|
99
|
+
try:
|
|
100
|
+
hook_manager = HooksManager(self.options)
|
|
101
|
+
|
|
102
|
+
# Validate featureKey is a string
|
|
103
|
+
if not isinstance(feature_key, str):
|
|
104
|
+
LogManager.get_instance().error(
|
|
105
|
+
error_messages.get("INVALID_PARAM").format(
|
|
106
|
+
apiName=api_name,
|
|
107
|
+
key=feature_key,
|
|
108
|
+
type=type(feature_key).__name__,
|
|
109
|
+
correctType="string",
|
|
110
|
+
)
|
|
111
|
+
)
|
|
112
|
+
raise TypeError("TypeError: featureKey should be a string")
|
|
113
|
+
|
|
114
|
+
# Validate settings are loaded and valid
|
|
115
|
+
settings_manager = SettingsManager.get_instance()
|
|
116
|
+
if not settings_manager or not settings_manager.is_settings_valid(self.original_settings):
|
|
117
|
+
LogManager.get_instance().error(
|
|
118
|
+
error_messages.get("INVALID_SETTINGS_SCHEMA")
|
|
119
|
+
)
|
|
120
|
+
raise ValueError("Invalid Settings")
|
|
121
|
+
|
|
122
|
+
context_copy = context.copy()
|
|
123
|
+
context_copy["uuid"] = uuid
|
|
124
|
+
if "bucketingSeed" in context:
|
|
125
|
+
bucketing_seed = context.get("bucketingSeed")
|
|
126
|
+
if not isinstance(bucketing_seed, str) or not bucketing_seed.strip():
|
|
127
|
+
LogManager.get_instance().error(
|
|
128
|
+
error_messages.get("INVALID_BUCKETING_SEED")
|
|
129
|
+
)
|
|
130
|
+
del context_copy["bucketingSeed"]
|
|
131
|
+
|
|
132
|
+
context_model = ContextModel(context_copy)
|
|
133
|
+
|
|
134
|
+
if self.options.get("is_aliasing_enabled"):
|
|
135
|
+
context_model.set_id(get_alias_user_id(context_model))
|
|
136
|
+
|
|
137
|
+
# Fetch the feature flag value using FlagApi
|
|
138
|
+
flag_api = GetFlagApi()
|
|
139
|
+
data = flag_api.get(
|
|
140
|
+
feature_key, self._settings, context_model, hook_manager
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
return data
|
|
144
|
+
|
|
145
|
+
except (TypeError, ValueError) as err:
|
|
146
|
+
LogManager.get_instance().error_log("EXECUTION_FAILED", data={"apiName": api_name, "err": str(err)}, debug_data={"an": ApiEnum.GET_FLAG.value})
|
|
147
|
+
return get_flag_response
|
|
148
|
+
|
|
149
|
+
except Exception as err:
|
|
150
|
+
LogManager.get_instance().error_log("EXECUTION_FAILED", data={"apiName": api_name, "err": str(err)}, debug_data={"an": ApiEnum.GET_FLAG.value})
|
|
151
|
+
return get_flag_response
|
|
152
|
+
|
|
153
|
+
def track_event(
|
|
154
|
+
self, event_name: str, context: Dict, event_properties: Dict[str, Any] = {}
|
|
155
|
+
) -> Dict:
|
|
156
|
+
"""
|
|
157
|
+
Tracks an event with specified properties and context.
|
|
158
|
+
This method validates the types of the inputs and ensures the settings and user context are valid before proceeding.
|
|
159
|
+
|
|
160
|
+
:param event_name: The name of the event to track.
|
|
161
|
+
:param context: The context in which the event is being
|
|
162
|
+
:param event_properties: The properties of the event.
|
|
163
|
+
"""
|
|
164
|
+
api_name = "track_event"
|
|
165
|
+
|
|
166
|
+
try:
|
|
167
|
+
hook_manager = HooksManager(self.options)
|
|
168
|
+
|
|
169
|
+
LogManager.get_instance().debug(
|
|
170
|
+
debug_messages.get("API_CALLED").format(apiName=api_name)
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
# Validate featureKey is a string
|
|
174
|
+
if not is_string(event_name):
|
|
175
|
+
LogManager.get_instance().error(
|
|
176
|
+
error_messages.get("INVALID_PARAM").format(
|
|
177
|
+
apiName=api_name,
|
|
178
|
+
key="event_name",
|
|
179
|
+
type=type(event_name).__name__,
|
|
180
|
+
correctType="string",
|
|
181
|
+
)
|
|
182
|
+
)
|
|
183
|
+
raise TypeError("TypeError: event_name should be a string")
|
|
184
|
+
|
|
185
|
+
if not is_object(event_properties):
|
|
186
|
+
LogManager.get_instance().error(
|
|
187
|
+
error_messages.get("INVALID_PARAM").format(
|
|
188
|
+
apiName=api_name,
|
|
189
|
+
key="event_properties",
|
|
190
|
+
type=type(event_properties).__name__,
|
|
191
|
+
correctType="object",
|
|
192
|
+
)
|
|
193
|
+
)
|
|
194
|
+
raise TypeError("TypeError: event_properties should be an object")
|
|
195
|
+
|
|
196
|
+
# Validate settings are loaded and valid
|
|
197
|
+
settings_manager = SettingsManager.get_instance()
|
|
198
|
+
if not settings_manager or not settings_manager.is_settings_valid(self.original_settings):
|
|
199
|
+
LogManager.get_instance().error(
|
|
200
|
+
error_messages.get("INVALID_SETTINGS_SCHEMA")
|
|
201
|
+
)
|
|
202
|
+
raise ValueError("Invalid Settings")
|
|
203
|
+
|
|
204
|
+
self._validate_context(context, api_name)
|
|
205
|
+
|
|
206
|
+
context_copy = context.copy()
|
|
207
|
+
context_copy["uuid"] = self._get_uuid_from_context(context, api_name)
|
|
208
|
+
context_model = ContextModel(context_copy)
|
|
209
|
+
|
|
210
|
+
if self.options.get("is_aliasing_enabled"):
|
|
211
|
+
context_model.set_id(get_alias_user_id(context_model))
|
|
212
|
+
|
|
213
|
+
# Fetch the feature flag value using FlagApi
|
|
214
|
+
trackApi = TrackApi()
|
|
215
|
+
data = trackApi.track(
|
|
216
|
+
self._settings,
|
|
217
|
+
event_name,
|
|
218
|
+
context_model,
|
|
219
|
+
event_properties,
|
|
220
|
+
hook_manager,
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
return data
|
|
224
|
+
|
|
225
|
+
except (TypeError, ValueError) as err:
|
|
226
|
+
LogManager.get_instance().error_log("EXECUTION_FAILED", data={"apiName": api_name, "err": str(err)}, debug_data={"an": ApiEnum.TRACK_EVENT.value})
|
|
227
|
+
return {event_name: False}
|
|
228
|
+
|
|
229
|
+
except Exception as err:
|
|
230
|
+
LogManager.get_instance().error_log("EXECUTION_FAILED", data={"apiName": api_name, "err": str(err)}, debug_data={"an": ApiEnum.TRACK_EVENT.value})
|
|
231
|
+
return {event_name: False}
|
|
232
|
+
|
|
233
|
+
def set_attribute(
|
|
234
|
+
self, key_or_map: Any, value_or_context: Any, context: Dict = None
|
|
235
|
+
):
|
|
236
|
+
"""
|
|
237
|
+
Sets an attribute for a user in the context provided.
|
|
238
|
+
This method can be called in two ways:
|
|
239
|
+
1. set_attribute(key, value, context) - Sets a single attribute
|
|
240
|
+
2. set_attribute(attribute_map, context) - Sets multiple attributes
|
|
241
|
+
|
|
242
|
+
The attribute values must be either strings, integers, or booleans.
|
|
243
|
+
|
|
244
|
+
:param key_or_map: Either the attribute key (str) or a map of attributes (Dict)
|
|
245
|
+
:param value_or_context: Either the attribute value or the context (if key_or_map is a Dict)
|
|
246
|
+
:param context: The context in which the attribute is being set (only used when setting single attribute)
|
|
247
|
+
"""
|
|
248
|
+
api_name = "set_attribute"
|
|
249
|
+
|
|
250
|
+
try:
|
|
251
|
+
LogManager.get_instance().debug(
|
|
252
|
+
debug_messages.get("API_CALLED").format(apiName=api_name)
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
# Determine which calling pattern is being used
|
|
256
|
+
if context is not None:
|
|
257
|
+
# Single attribute pattern: (key, value, context)
|
|
258
|
+
if not is_string(key_or_map) or key_or_map == '':
|
|
259
|
+
LogManager.get_instance().error(
|
|
260
|
+
error_messages.get("INVALID_PARAM").format(
|
|
261
|
+
apiName=api_name,
|
|
262
|
+
key="key",
|
|
263
|
+
type=type(key_or_map).__name__,
|
|
264
|
+
correctType="string",
|
|
265
|
+
)
|
|
266
|
+
)
|
|
267
|
+
raise TypeError("TypeError: key should be a string")
|
|
268
|
+
|
|
269
|
+
if not isinstance(value_or_context, (str, int, bool, float)) or value_or_context == '':
|
|
270
|
+
LogManager.get_instance().error(
|
|
271
|
+
error_messages.get("INVALID_PARAM").format(
|
|
272
|
+
apiName=api_name,
|
|
273
|
+
key="value",
|
|
274
|
+
type=type(value_or_context).__name__,
|
|
275
|
+
correctType="string, integer, float, or boolean",
|
|
276
|
+
)
|
|
277
|
+
)
|
|
278
|
+
raise TypeError(
|
|
279
|
+
"TypeError: value should be a string, integer, float, or boolean"
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
attribute_map = {key_or_map: value_or_context}
|
|
283
|
+
user_context = context
|
|
284
|
+
else:
|
|
285
|
+
# Multiple attributes pattern: (attribute_map, context)
|
|
286
|
+
if not is_object(key_or_map) or not key_or_map:
|
|
287
|
+
LogManager.get_instance().error(
|
|
288
|
+
error_messages.get("INVALID_PARAM").format(
|
|
289
|
+
apiName=api_name,
|
|
290
|
+
key="attribute_map",
|
|
291
|
+
type=type(key_or_map).__name__,
|
|
292
|
+
correctType="object",
|
|
293
|
+
)
|
|
294
|
+
)
|
|
295
|
+
raise TypeError(
|
|
296
|
+
"TypeError: attribute_map should be a non-empty object"
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
# Validate all keys and values in the attribute map
|
|
300
|
+
for key, value in key_or_map.items():
|
|
301
|
+
if not is_string(key):
|
|
302
|
+
LogManager.get_instance().error(
|
|
303
|
+
error_messages.get("INVALID_PARAM").format(
|
|
304
|
+
apiName=api_name,
|
|
305
|
+
key="key",
|
|
306
|
+
type=type(key).__name__,
|
|
307
|
+
correctType="string",
|
|
308
|
+
)
|
|
309
|
+
)
|
|
310
|
+
raise TypeError("TypeError: key should be a string")
|
|
311
|
+
if not isinstance(value, (str, int, bool, float)):
|
|
312
|
+
LogManager.get_instance().error(
|
|
313
|
+
error_messages.get("INVALID_PARAM").format(
|
|
314
|
+
apiName=api_name,
|
|
315
|
+
key=f"value for key '{key}'",
|
|
316
|
+
type=type(value).__name__,
|
|
317
|
+
correctType="string, integer, float, or boolean",
|
|
318
|
+
)
|
|
319
|
+
)
|
|
320
|
+
raise TypeError(
|
|
321
|
+
f"TypeError: value for key '{key}' should be a string, integer, float or boolean"
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
attribute_map = key_or_map
|
|
325
|
+
user_context = value_or_context
|
|
326
|
+
|
|
327
|
+
# Validate settings are loaded and valid
|
|
328
|
+
settings_manager = SettingsManager.get_instance()
|
|
329
|
+
if not settings_manager or not settings_manager.is_settings_valid(self.original_settings):
|
|
330
|
+
LogManager.get_instance().error(
|
|
331
|
+
error_messages.get("INVALID_SETTINGS_SCHEMA")
|
|
332
|
+
)
|
|
333
|
+
raise ValueError("Invalid Settings")
|
|
334
|
+
|
|
335
|
+
self._validate_context(user_context, api_name)
|
|
336
|
+
|
|
337
|
+
context_copy = user_context.copy()
|
|
338
|
+
context_copy["uuid"] = self._get_uuid_from_context(user_context, api_name)
|
|
339
|
+
context_model = ContextModel(context_copy)
|
|
340
|
+
|
|
341
|
+
if self.options.get("is_aliasing_enabled"):
|
|
342
|
+
context_model.set_id(get_alias_user_id(context_model))
|
|
343
|
+
|
|
344
|
+
set_attribute_api = SetAttributeApi()
|
|
345
|
+
set_attribute_api.set_attribute(
|
|
346
|
+
self._settings, attribute_map, context_model
|
|
347
|
+
)
|
|
348
|
+
return
|
|
349
|
+
|
|
350
|
+
except (TypeError, ValueError) as err:
|
|
351
|
+
LogManager.get_instance().error_log("EXECUTION_FAILED", data={"apiName": api_name, "err": str(err)}, debug_data={"an": ApiEnum.SET_ATTRIBUTE.value})
|
|
352
|
+
return
|
|
353
|
+
|
|
354
|
+
except Exception as err:
|
|
355
|
+
LogManager.get_instance().error_log("EXECUTION_FAILED", data={"apiName": api_name, "err": str(err)}, debug_data={"an": ApiEnum.SET_ATTRIBUTE.value})
|
|
356
|
+
return
|
|
357
|
+
|
|
358
|
+
def update_settings(self, settings: Dict = None, is_via_webhook=True):
|
|
359
|
+
"""
|
|
360
|
+
Updates the settings for the client.
|
|
361
|
+
:param settings: The settings to update.
|
|
362
|
+
:param is_via_webhook: Whether the settings are being updated via webhook.
|
|
363
|
+
"""
|
|
364
|
+
|
|
365
|
+
api_name = "update_settings"
|
|
366
|
+
|
|
367
|
+
try:
|
|
368
|
+
LogManager.get_instance().debug(
|
|
369
|
+
debug_messages.get("API_CALLED").format(apiName=api_name)
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
# check if settings are None or empty
|
|
373
|
+
settings_to_update = settings
|
|
374
|
+
if settings_to_update is None or settings_to_update == {}:
|
|
375
|
+
# fetch the latest settings
|
|
376
|
+
settings_to_update = SettingsManager.get_instance().fetch_settings(
|
|
377
|
+
is_via_webhook,
|
|
378
|
+
api_name
|
|
379
|
+
)
|
|
380
|
+
|
|
381
|
+
# validate the settings
|
|
382
|
+
settings_manager = SettingsManager.get_instance()
|
|
383
|
+
if not settings_manager or not settings_manager.is_settings_valid(settings_to_update):
|
|
384
|
+
LogManager.get_instance().error(
|
|
385
|
+
error_messages.get("INVALID_SETTINGS_SCHEMA")
|
|
386
|
+
)
|
|
387
|
+
raise ValueError("TypeError: Invalid Settings schema")
|
|
388
|
+
|
|
389
|
+
# update the settings
|
|
390
|
+
set_settings_and_add_campaigns_to_rules(settings_to_update, self)
|
|
391
|
+
LogManager.get_instance().info(
|
|
392
|
+
info_messages.get("SETTINGS_UPDATED").format(
|
|
393
|
+
apiName=api_name, isViaWebhook=is_via_webhook
|
|
394
|
+
)
|
|
395
|
+
)
|
|
396
|
+
return
|
|
397
|
+
|
|
398
|
+
except Exception as err:
|
|
399
|
+
LogManager.get_instance().error_log("UPDATING_CLIENT_INSTANCE_FAILED_WHEN_WEBHOOK_TRIGGERED", data={"apiName": api_name, "isViaWebhook": is_via_webhook, "err": str(err)}, debug_data={"an": ApiEnum.UPDATE_SETTINGS.value})
|
|
400
|
+
return
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def flush_events(self):
|
|
404
|
+
"""
|
|
405
|
+
Flushes events from the batch event queue and clears the queue.
|
|
406
|
+
This method will also handle calling the flush callback upon completion.
|
|
407
|
+
"""
|
|
408
|
+
api_name = "flush_events"
|
|
409
|
+
try:
|
|
410
|
+
# Log that the API has been called
|
|
411
|
+
LogManager.get_instance().debug(
|
|
412
|
+
debug_messages.get("API_CALLED").format(apiName=api_name)
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
if self.batch_event_queue:
|
|
416
|
+
LogManager.get_instance().debug(
|
|
417
|
+
f"Flushing events for accountId: {self.options.get('account_id')}. Queue size: {len(self.batch_event_queue.batch_queue)}"
|
|
418
|
+
)
|
|
419
|
+
# Trigger the flush and clear the queue
|
|
420
|
+
flush_result = self.batch_event_queue.flush_and_clear_timer()
|
|
421
|
+
return flush_result
|
|
422
|
+
else:
|
|
423
|
+
LogManager.get_instance().debug(
|
|
424
|
+
f"Cannot flush events. Batch event queue is empty for accountId: {self.options.get('account_id')}"
|
|
425
|
+
)
|
|
426
|
+
return False
|
|
427
|
+
|
|
428
|
+
except Exception as err:
|
|
429
|
+
LogManager.get_instance().error_log("EXECUTION_FAILED", data={"apiName": api_name, "err": str(err)}, debug_data={"an": ApiEnum.FLUSH_EVENTS.value})
|
|
430
|
+
return False
|
|
431
|
+
|
|
432
|
+
def set_alias(self, user_id_or_context: Any, alias_id: str) -> bool:
|
|
433
|
+
"""
|
|
434
|
+
Set an alias for a given user id using the gateway service.
|
|
435
|
+
|
|
436
|
+
:param user_id_or_context: User id or context to be aliased
|
|
437
|
+
:param alias_id: Alias id to be set for the user id
|
|
438
|
+
:return: True if the alias was set successfully, else False
|
|
439
|
+
"""
|
|
440
|
+
api_name = "setAlias"
|
|
441
|
+
try:
|
|
442
|
+
LogManager.get_instance().debug(
|
|
443
|
+
debug_messages.get("API_CALLED").format(apiName=api_name)
|
|
444
|
+
)
|
|
445
|
+
|
|
446
|
+
# check if aliasing is enabled
|
|
447
|
+
if not self.options.get("is_aliasing_enabled"):
|
|
448
|
+
raise ValueError("Aliasing is not enabled")
|
|
449
|
+
|
|
450
|
+
# check if gateway service is provided
|
|
451
|
+
settings_manager = SettingsManager.get_instance()
|
|
452
|
+
if not settings_manager or not settings_manager.is_gateway_service_provided:
|
|
453
|
+
raise ValueError("Gateway service is not provided")
|
|
454
|
+
|
|
455
|
+
user_id = user_id_or_context if isinstance(user_id_or_context, str) else user_id_or_context.get("id")
|
|
456
|
+
|
|
457
|
+
# check if user id is provided
|
|
458
|
+
if not isinstance(user_id, str) or not user_id or not user_id.strip():
|
|
459
|
+
raise TypeError("User ID is required")
|
|
460
|
+
|
|
461
|
+
# check if alias id is provided
|
|
462
|
+
if not isinstance(alias_id, str) or not alias_id or not alias_id.strip():
|
|
463
|
+
raise TypeError("Alias ID is required")
|
|
464
|
+
|
|
465
|
+
# remove whitespaces from alias id and trim user id
|
|
466
|
+
alias_id_clean = "".join(alias_id.strip().split())
|
|
467
|
+
user_id_clean = user_id.strip()
|
|
468
|
+
|
|
469
|
+
# check if user id and alias id are the same
|
|
470
|
+
if user_id_clean == alias_id_clean:
|
|
471
|
+
raise ValueError("User ID and Alias ID cannot be the same")
|
|
472
|
+
|
|
473
|
+
# set alias on gateway service
|
|
474
|
+
return set_user_alias_util(user_id_clean, alias_id_clean)
|
|
475
|
+
except Exception as err:
|
|
476
|
+
LogManager.get_instance().error_log("EXECUTION_FAILED", data={"apiName": api_name, "err": str(err)}, debug_data={"an": ApiEnum.SET_ALIAS.value})
|
|
477
|
+
return False
|
|
478
|
+
|
|
479
|
+
def _validate_context(self, context: Dict, api_name: str) -> None:
|
|
480
|
+
"""
|
|
481
|
+
Validates that context contains a string user ID.
|
|
482
|
+
|
|
483
|
+
:param context: The context object passed to an API.
|
|
484
|
+
:param api_name: The name of the API calling this method.
|
|
485
|
+
"""
|
|
486
|
+
if not context or not is_object(context) or "id" not in context:
|
|
487
|
+
LogManager.get_instance().error(
|
|
488
|
+
error_messages.get("INVALID_CONTEXT_PASSED")
|
|
489
|
+
)
|
|
490
|
+
raise ValueError("Invalid context")
|
|
491
|
+
|
|
492
|
+
if not isinstance(context.get("id"), str):
|
|
493
|
+
LogManager.get_instance().error(
|
|
494
|
+
error_messages.get("INVALID_CONTEXT_USER_ID")
|
|
495
|
+
)
|
|
496
|
+
raise ValueError("user_id under context must be a string value")
|
|
497
|
+
|
|
498
|
+
def _get_uuid_from_context(self, context: Dict, api_name: str) -> str:
|
|
499
|
+
"""
|
|
500
|
+
Gets the UUID from the context.
|
|
501
|
+
:param context: The context to get the UUID from.
|
|
502
|
+
:param api_name: The name of the API calling this method.
|
|
503
|
+
:return: The UUID from the context.
|
|
504
|
+
"""
|
|
505
|
+
if self._settings.get_is_web_connectivity_enabled():
|
|
506
|
+
# if web connectivity is enabled, check if context id is a valid web UUID
|
|
507
|
+
if is_web_uuid(context.get("id")):
|
|
508
|
+
# if it is a valid web UUID, return it
|
|
509
|
+
LogManager.get_instance().debug(debug_messages.get("WEB_UUID_FOUND").format(apiName=api_name, uuid=context.get("id")))
|
|
510
|
+
return context.get("id")
|
|
511
|
+
|
|
512
|
+
# if use_id_for_web is true and context id is not a valid web UUID, throw error
|
|
513
|
+
if context.get("use_id_for_web") is True:
|
|
514
|
+
raise ValueError("UUID passed in context is not a valid UUID")
|
|
515
|
+
|
|
516
|
+
# if use_id_for_web is false, fallback to server‑side UUID derivation
|
|
517
|
+
return get_uuid(context.get("id"), str(SettingsManager.get_instance().get_account_id()))
|
|
518
|
+
|
|
519
|
+
# if web connectivity is disabled, fallback to server‑side UUID derivation
|
|
520
|
+
return get_uuid(context.get("id"), str(SettingsManager.get_instance().get_account_id()))
|