Skip to content

Commit 44110b0

Browse files
committed
polishing
1 parent 0cfb0ef commit 44110b0

File tree

13 files changed

+143
-166
lines changed

13 files changed

+143
-166
lines changed

splitio/client/client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ def _get_fallback_eval_results(self, eval_result, feature):
219219
feature, result["treatment"], result["impression"]["label"], _LOGGER)
220220
return result
221221

222+
def _check_impression_label(self, result):
223+
return result['impression']['label'] == None or (result['impression']['label'] != None and result['impression']['label'].find(Label.SPLIT_NOT_FOUND) == -1)
224+
222225
class Client(ClientBase): # pylint: disable=too-many-instance-attributes
223226
"""Entry point for the split sdk."""
224227

@@ -344,7 +347,7 @@ def _get_treatment(self, method, key, feature, attributes=None, evaluation_optio
344347
result = self._get_fallback_eval_results(self._FAILED_EVAL_RESULT, feature)
345348

346349
properties = self._get_properties(evaluation_options)
347-
if result['impression']['label'] == None or (result['impression']['label'] != None and result['impression']['label'].find(Label.SPLIT_NOT_FOUND) == -1):
350+
if self._check_impression_label(result):
348351
impression_decorated = self._build_impression(key, bucketing, feature, result, properties)
349352
self._record_stats([(impression_decorated, attributes)], start, method)
350353

@@ -844,7 +847,7 @@ async def _get_treatment(self, method, key, feature, attributes=None, evaluation
844847
result = self._get_fallback_eval_results(self._FAILED_EVAL_RESULT, feature)
845848

846849
properties = self._get_properties(evaluation_options)
847-
if result['impression']['label'] == None or (result['impression']['label'] != None and result['impression']['label'].find(Label.SPLIT_NOT_FOUND) == -1):
850+
if self._check_impression_label(result):
848851
impression_decorated = self._build_impression(key, bucketing, feature, result, properties)
849852
await self._record_stats([(impression_decorated, attributes)], start, method)
850853
return result['treatment'], result['configurations']

splitio/client/config.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from splitio.engine.impressions import ImpressionsMode
77
from splitio.client.input_validator import validate_flag_sets, validate_fallback_treatment, validate_regex_name
8-
from splitio.models.fallback_config import FallbackConfig, FallbackTreatmentsConfiguration
8+
from splitio.models.fallback_config import FallbackTreatmentsConfiguration
99

1010
_LOGGER = logging.getLogger(__name__)
1111
DEFAULT_DATA_SAMPLING = 1
@@ -71,7 +71,7 @@ class AuthenticateScheme(Enum):
7171
'httpAuthenticateScheme': AuthenticateScheme.NONE,
7272
'kerberosPrincipalUser': None,
7373
'kerberosPrincipalPassword': None,
74-
'fallbackTreatmentsConfiguration': FallbackTreatmentsConfiguration(None)
74+
'fallbackTreatments': FallbackTreatmentsConfiguration(None)
7575
}
7676

7777
def _parse_operation_mode(sdk_key, config):
@@ -175,32 +175,26 @@ def sanitize(sdk_key, config):
175175
return processed
176176

177177
def _sanitize_fallback_config(config, processed):
178-
if config.get('fallbackTreatmentsConfiguration') is not None:
179-
if not isinstance(config['fallbackTreatmentsConfiguration'], FallbackTreatmentsConfiguration):
180-
_LOGGER.warning('Config: fallbackTreatmentsConfiguration parameter should be of `FallbackTreatmentsConfiguration` class.')
181-
processed['fallbackTreatmentsConfiguration'] = FallbackTreatmentsConfiguration(None)
178+
if config.get('fallbackTreatments') is not None:
179+
if not isinstance(config['fallbackTreatments'], FallbackTreatmentsConfiguration):
180+
_LOGGER.warning('Config: fallbackTreatments parameter should be of `FallbackTreatmentsConfiguration` class.')
181+
processed['fallbackTreatments'] = None
182182
return processed
183-
184-
if config['fallbackTreatmentsConfiguration'].fallback_config != None:
185-
if not isinstance(config['fallbackTreatmentsConfiguration'].fallback_config, FallbackConfig):
186-
_LOGGER.warning('Config: fallback_config parameter should be of `FallbackConfig` class.')
187-
processed['fallbackTreatmentsConfiguration'].fallback_config = FallbackConfig(None, None)
188-
return processed
189-
190-
if config['fallbackTreatmentsConfiguration'].fallback_config.global_fallback_treatment is not None and not validate_fallback_treatment(config['fallbackTreatmentsConfiguration'].fallback_config.global_fallback_treatment):
191-
_LOGGER.warning('Config: global fallbacktreatment parameter is discarded.')
192-
processed['fallbackTreatmentsConfiguration'].fallback_config.global_fallback_treatment = None
193-
return processed
194-
195-
if config['fallbackTreatmentsConfiguration'].fallback_config.by_flag_fallback_treatment is not None:
196-
sanitized_flag_fallback_treatments = {}
197-
for feature_name in config['fallbackTreatmentsConfiguration'].fallback_config.by_flag_fallback_treatment.keys():
198-
if not validate_regex_name(feature_name) or not validate_fallback_treatment(config['fallbackTreatmentsConfiguration'].fallback_config.by_flag_fallback_treatment[feature_name]):
199-
_LOGGER.warning('Config: fallback treatment parameter for feature flag %s is discarded.', feature_name)
200-
continue
201-
202-
sanitized_flag_fallback_treatments[feature_name] = config['fallbackTreatmentsConfiguration'].fallback_config.by_flag_fallback_treatment[feature_name]
203-
204-
processed['fallbackTreatmentsConfiguration'].fallback_config = FallbackConfig(config['fallbackTreatmentsConfiguration'].fallback_config.global_fallback_treatment, sanitized_flag_fallback_treatments)
205-
183+
184+
sanitized_global_fallback_treatment = config['fallbackTreatments'].global_fallback_treatment
185+
if config['fallbackTreatments'].global_fallback_treatment is not None and not validate_fallback_treatment(config['fallbackTreatments'].global_fallback_treatment):
186+
_LOGGER.warning('Config: global fallbacktreatment parameter is discarded.')
187+
sanitized_global_fallback_treatment = None
188+
189+
sanitized_flag_fallback_treatments = {}
190+
if config['fallbackTreatments'].by_flag_fallback_treatment is not None:
191+
for feature_name in config['fallbackTreatments'].by_flag_fallback_treatment.keys():
192+
if not validate_regex_name(feature_name) or not validate_fallback_treatment(config['fallbackTreatments'].by_flag_fallback_treatment[feature_name]):
193+
_LOGGER.warning('Config: fallback treatment parameter for feature flag %s is discarded.', feature_name)
194+
continue
195+
196+
sanitized_flag_fallback_treatments[feature_name] = config['fallbackTreatments'].by_flag_fallback_treatment[feature_name]
197+
198+
processed['fallbackTreatments'] = FallbackTreatmentsConfiguration(sanitized_global_fallback_treatment, sanitized_flag_fallback_treatments)
199+
206200
return processed

splitio/client/factory.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,15 @@ def _build_in_memory_factory(api_key, cfg, sdk_url=None, events_url=None, # pyl
628628

629629
return SplitFactory(api_key, storages, cfg['labelsEnabled'],
630630
recorder, manager, None, telemetry_producer, telemetry_init_producer, telemetry_submitter, preforked_initialization=preforked_initialization,
631-
fallback_treatments_configuration=cfg['fallbackTreatmentsConfiguration'])
631+
fallback_treatments_configuration=cfg['fallbackTreatments'])
632632

633633
initialization_thread = threading.Thread(target=manager.start, name="SDKInitializer", daemon=True)
634634
initialization_thread.start()
635635

636636
return SplitFactory(api_key, storages, cfg['labelsEnabled'],
637637
recorder, manager, sdk_ready_flag,
638638
telemetry_producer, telemetry_init_producer,
639-
telemetry_submitter, fallback_treatments_configuration=cfg['fallbackTreatmentsConfiguration'])
639+
telemetry_submitter, fallback_treatments_configuration=cfg['fallbackTreatments'])
640640

641641
async def _build_in_memory_factory_async(api_key, cfg, sdk_url=None, events_url=None, # pylint:disable=too-many-arguments,too-many-localsa
642642
auth_api_base_url=None, streaming_api_base_url=None, telemetry_api_base_url=None,
@@ -755,7 +755,7 @@ async def _build_in_memory_factory_async(api_key, cfg, sdk_url=None, events_url=
755755
recorder, manager,
756756
telemetry_producer, telemetry_init_producer,
757757
telemetry_submitter, manager_start_task=manager_start_task,
758-
api_client=http_client, fallback_treatments_configuration=cfg['fallbackTreatmentsConfiguration'])
758+
api_client=http_client, fallback_treatments_configuration=cfg['fallbackTreatments'])
759759

760760
def _build_redis_factory(api_key, cfg):
761761
"""Build and return a split factory with redis-based storage."""
@@ -834,7 +834,7 @@ def _build_redis_factory(api_key, cfg):
834834
sdk_ready_flag=None,
835835
telemetry_producer=telemetry_producer,
836836
telemetry_init_producer=telemetry_init_producer,
837-
fallback_treatments_configuration=cfg['fallbackTreatmentsConfiguration']
837+
fallback_treatments_configuration=cfg['fallbackTreatments']
838838
)
839839
redundant_factory_count, active_factory_count = _get_active_and_redundant_count()
840840
storages['telemetry'].record_active_and_redundant_factories(active_factory_count, redundant_factory_count)
@@ -917,7 +917,7 @@ async def _build_redis_factory_async(api_key, cfg):
917917
telemetry_producer=telemetry_producer,
918918
telemetry_init_producer=telemetry_init_producer,
919919
telemetry_submitter=telemetry_submitter,
920-
fallback_treatments_configuration=cfg['fallbackTreatmentsConfiguration']
920+
fallback_treatments_configuration=cfg['fallbackTreatments']
921921
)
922922
redundant_factory_count, active_factory_count = _get_active_and_redundant_count()
923923
await storages['telemetry'].record_active_and_redundant_factories(active_factory_count, redundant_factory_count)
@@ -1000,7 +1000,7 @@ def _build_pluggable_factory(api_key, cfg):
10001000
sdk_ready_flag=None,
10011001
telemetry_producer=telemetry_producer,
10021002
telemetry_init_producer=telemetry_init_producer,
1003-
fallback_treatments_configuration=cfg['fallbackTreatmentsConfiguration']
1003+
fallback_treatments_configuration=cfg['fallbackTreatments']
10041004
)
10051005
redundant_factory_count, active_factory_count = _get_active_and_redundant_count()
10061006
storages['telemetry'].record_active_and_redundant_factories(active_factory_count, redundant_factory_count)
@@ -1081,7 +1081,7 @@ async def _build_pluggable_factory_async(api_key, cfg):
10811081
telemetry_producer=telemetry_producer,
10821082
telemetry_init_producer=telemetry_init_producer,
10831083
telemetry_submitter=telemetry_submitter,
1084-
fallback_treatments_configuration=cfg['fallbackTreatmentsConfiguration']
1084+
fallback_treatments_configuration=cfg['fallbackTreatments']
10851085
)
10861086
redundant_factory_count, active_factory_count = _get_active_and_redundant_count()
10871087
await storages['telemetry'].record_active_and_redundant_factories(active_factory_count, redundant_factory_count)
@@ -1159,7 +1159,7 @@ def _build_localhost_factory(cfg):
11591159
telemetry_producer=telemetry_producer,
11601160
telemetry_init_producer=telemetry_producer.get_telemetry_init_producer(),
11611161
telemetry_submitter=LocalhostTelemetrySubmitter(),
1162-
fallback_treatments_configuration=cfg['fallbackTreatmentsConfiguration']
1162+
fallback_treatments_configuration=cfg['fallbackTreatments']
11631163
)
11641164

11651165
async def _build_localhost_factory_async(cfg):
@@ -1231,7 +1231,7 @@ async def _build_localhost_factory_async(cfg):
12311231
telemetry_init_producer=telemetry_producer.get_telemetry_init_producer(),
12321232
telemetry_submitter=LocalhostTelemetrySubmitterAsync(),
12331233
manager_start_task=manager_start_task,
1234-
fallback_treatments_configuration=cfg['fallbackTreatmentsConfiguration']
1234+
fallback_treatments_configuration=cfg['fallbackTreatments']
12351235
)
12361236

12371237
def get_factory(api_key, **kwargs):

splitio/client/input_validator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from splitio.client import client
1010
from splitio.client.util import get_fallback_treatment_and_label
1111
from splitio.engine.evaluator import CONTROL
12+
from splitio.models.fallback_treatment import FallbackTreatment
1213

1314

1415
_LOGGER = logging.getLogger(__name__)
@@ -722,6 +723,14 @@ def validate_flag_sets(flag_sets, method_name):
722723
return list(sanitized_flag_sets)
723724

724725
def validate_fallback_treatment(fallback_treatment):
726+
if not isinstance(fallback_treatment, FallbackTreatment):
727+
_LOGGER.warning("Config: Fallback treatment instance should be FallbackTreatment, input is discarded")
728+
return False
729+
730+
if not isinstance(fallback_treatment.treatment, str):
731+
_LOGGER.warning("Config: Fallback treatment value should be str type, input is discarded")
732+
return False
733+
725734
if not validate_regex_name(fallback_treatment.treatment):
726735
_LOGGER.warning("Config: Fallback treatment should match regex %s", _FALLBACK_TREATMENT_REGEX)
727736
return False

splitio/client/util.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@ def get_metadata(config):
5353
return SdkMetadata(version, hostname, ip_address)
5454

5555
def get_fallback_treatment_and_label(fallback_treatments_configuration, feature_name, treatment, label, _logger):
56-
if fallback_treatments_configuration == None or fallback_treatments_configuration.fallback_config == None:
56+
if fallback_treatments_configuration == None:
5757
return label, treatment, None
5858

59-
if fallback_treatments_configuration.fallback_config.by_flag_fallback_treatment != None and \
60-
fallback_treatments_configuration.fallback_config.by_flag_fallback_treatment.get(feature_name) != None:
59+
if fallback_treatments_configuration.by_flag_fallback_treatment != None and \
60+
fallback_treatments_configuration.by_flag_fallback_treatment.get(feature_name) != None:
6161
_logger.debug('Using Fallback Treatment for feature: %s', feature_name)
62-
return fallback_treatments_configuration.fallback_config.by_flag_fallback_treatment.get(feature_name).label_prefix + label, \
63-
fallback_treatments_configuration.fallback_config.by_flag_fallback_treatment.get(feature_name).treatment, \
64-
fallback_treatments_configuration.fallback_config.by_flag_fallback_treatment.get(feature_name).config
62+
return fallback_treatments_configuration.by_flag_fallback_treatment.get(feature_name).label_prefix + label, \
63+
fallback_treatments_configuration.by_flag_fallback_treatment.get(feature_name).treatment, \
64+
fallback_treatments_configuration.by_flag_fallback_treatment.get(feature_name).config
6565

66-
if fallback_treatments_configuration.fallback_config.global_fallback_treatment != None:
66+
if fallback_treatments_configuration.global_fallback_treatment != None:
6767
_logger.debug('Using Global Fallback Treatment.')
68-
return fallback_treatments_configuration.fallback_config.global_fallback_treatment.label_prefix + label, \
69-
fallback_treatments_configuration.fallback_config.global_fallback_treatment.treatment, \
70-
fallback_treatments_configuration.fallback_config.global_fallback_treatment.config
68+
return fallback_treatments_configuration.global_fallback_treatment.label_prefix + label, \
69+
fallback_treatments_configuration.global_fallback_treatment.treatment, \
70+
fallback_treatments_configuration.global_fallback_treatment.config
7171

7272
return label, treatment, None

splitio/models/fallback_config.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,7 @@
11
"""Segment module."""
22

33
class FallbackTreatmentsConfiguration(object):
4-
"""FallbackConfiguration object class."""
5-
6-
def __init__(self, fallback_config):
7-
"""
8-
Class constructor.
9-
10-
:param fallback_config: fallback config object.
11-
:type fallback_config: FallbackConfig
12-
13-
:param by_flag_fallback_treatment: Dict of flags and their fallback treatment
14-
:type by_flag_fallback_treatment: {str: FallbackTreatment}
15-
"""
16-
self._fallback_config = fallback_config
17-
18-
@property
19-
def fallback_config(self):
20-
"""Return fallback config."""
21-
return self._fallback_config
22-
23-
@fallback_config.setter
24-
def fallback_config(self, new_value):
25-
"""Set fallback config."""
26-
self._fallback_config = new_value
27-
28-
class FallbackConfig(object):
29-
"""FallbackConfig object class."""
4+
"""FallbackTreatmentsConfiguration object class."""
305

316
def __init__(self, global_fallback_treatment=None, by_flag_fallback_treatment=None):
327
"""

0 commit comments

Comments
 (0)