Skip to content

Commit 25024ab

Browse files
authored
Merge pull request #28 from hellohaptik/bug_fixes
Bug fixes, Directory Structure and Refactor the code
2 parents 01f393e + 2213056 commit 25024ab

File tree

10 files changed

+62
-692
lines changed

10 files changed

+62
-692
lines changed

FeatureToggle/__init__.py

Lines changed: 20 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,6 @@
99
from UnleashClient.utils import LOGGER
1010

1111

12-
class FeatureTogglesFromConst:
13-
def __init__(self):
14-
self.feature_toggles_dict = consts.FEATURE_TOGGLES_API_RESPONSE
15-
16-
def is_enabled(self, feature_name,
17-
app_context: Optional[Dict] = {}) -> bool:
18-
"""
19-
Check if certain feature is enabled in const
20-
Args:
21-
feature_name(str): Name of the feature
22-
app_context(dict): App context to check when certain feature is enabled for given entity
23-
eg: {
24-
"partner_names": "<partner_names>"
25-
}
26-
Returns(bool): True if feature is enabled else False
27-
"""
28-
is_feature_enabled = feature_name in self.feature_toggles_dict
29-
30-
# If Feature is not enabled then return is_feature_enabled Value
31-
if not is_feature_enabled:
32-
return is_feature_enabled
33-
34-
if not app_context: # If there's not any app_context then return is_feature_enabled value
35-
return is_feature_enabled
36-
37-
app_context_parameter_key = list(app_context.keys())[0]
38-
app_context_parameter_value = list(app_context.values())[0]
39-
40-
feature_data = self.feature_toggles_dict[feature_name]
41-
return app_context_parameter_value in feature_data.get(app_context_parameter_key, [])
42-
43-
@staticmethod
44-
def fetch_feature_toggles() -> Dict[str, Any]:
45-
"""
46-
Return Feature toggles from const
47-
"""
48-
return consts.FEATURE_TOGGLES_API_RESPONSE
49-
50-
@staticmethod
51-
def is_enabled_for_partner(feature_name: str,
52-
partner_name: Optional[str] = ''):
53-
context = {}
54-
if partner_name:
55-
context['partner_names'] = partner_name
56-
57-
return FeatureTogglesFromConst().is_enabled(feature_name, context)
58-
59-
@staticmethod
60-
def is_enabled_for_expert(feature_name: str,
61-
expert_email: Optional[str] = ''):
62-
context = {}
63-
if expert_email:
64-
context['expert_emails'] = expert_email
65-
66-
return FeatureTogglesFromConst().is_enabled(feature_name, context)
67-
68-
@staticmethod
69-
def is_enabled_for_business(feature_name: str,
70-
business_via_name: Optional[str] = ''):
71-
context = {}
72-
if business_via_name:
73-
context['business_via_names'] = business_via_name
74-
75-
return FeatureTogglesFromConst().is_enabled(feature_name, context)
76-
77-
@staticmethod
78-
def is_enabled_for_domain(feature_name: str,
79-
domain_name: Optional[str] = ''):
80-
context = {}
81-
if domain_name:
82-
context['domain_names'] = domain_name
83-
84-
return FeatureTogglesFromConst().is_enabled(feature_name, context)
85-
86-
8712
class FeatureToggles:
8813
__client = None
8914
__url = None
@@ -167,20 +92,17 @@ def __get_unleash_client():
16792
Initialize the client if client is None Else Return the established client
16893
"""
16994
if FeatureToggles.__enable_toggle_service:
170-
if FeatureToggles.__client is None:
171-
FeatureToggles.__client = UnleashClient(
172-
url=FeatureToggles.__url,
173-
app_name=FeatureToggles.__app_name,
174-
instance_id=FeatureToggles.__instance_id,
175-
cas_name=FeatureToggles.__cas_name,
176-
environment=FeatureToggles.__environment,
177-
redis_host=FeatureToggles.__redis_host,
178-
redis_port=FeatureToggles.__redis_port,
179-
redis_db=FeatureToggles.__redis_db
180-
)
181-
FeatureToggles.__client.initialize_client()
182-
else:
183-
FeatureToggles.__client = FeatureTogglesFromConst()
95+
FeatureToggles.__client = UnleashClient(
96+
url=FeatureToggles.__url,
97+
app_name=FeatureToggles.__app_name,
98+
instance_id=FeatureToggles.__instance_id,
99+
cas_name=FeatureToggles.__cas_name,
100+
environment=FeatureToggles.__environment,
101+
redis_host=FeatureToggles.__redis_host,
102+
redis_port=FeatureToggles.__redis_port,
103+
redis_db=FeatureToggles.__redis_db
104+
)
105+
FeatureToggles.__client.initialize_client()
184106

185107
return FeatureToggles.__client
186108

@@ -300,6 +222,12 @@ def fetch_feature_toggles():
300222
}
301223
"""
302224
# TODO: Remove the cas and environment name from the feature toggles while returning the response
225+
226+
if FeatureToggles.__cache is None:
227+
raise Exception(
228+
'To update cache Feature Toggles class needs to be initialised'
229+
)
230+
303231
feature_toggles = pickle.loads(
304232
FeatureToggles.__cache.get(consts.FEATURES_URL)
305233
)
@@ -321,7 +249,8 @@ def fetch_feature_toggles():
321249

322250
if cas_name == FeatureToggles.__cas_name and environment == FeatureToggles.__environment:
323251
# Strip CAS and ENV name from feature name
324-
active_cas_env_name = f'{cas_name}.{environment}.'
252+
active_cas_env_name = f'{FeatureToggles.__cas_name}.'
253+
f'{FeatureToggles.__environment}.'
325254
full_feature_name = full_feature_name.replace(active_cas_env_name, '')
326255
if full_feature_name not in response:
327256
response[full_feature_name] = {}
@@ -350,4 +279,4 @@ def fetch_feature_toggles():
350279
# Handle this exception from where this util gets called
351280
raise Exception(f'Error occured while parsing the response: {str(err)}')
352281

353-
return response
282+
return response

0 commit comments

Comments
 (0)