1- from azure .appconfiguration import AzureAppConfigurationClient
2- from time_tracker_api .security import current_user_email
3- import json
41import os
2+ import json
3+ from time_tracker_api .security import current_user_email
4+ from azure .appconfiguration import AzureAppConfigurationClient
55
66
7- class FeatureToggleManager :
8- AZURE_CONNECTION_STRING = os .environ .get (
7+ class MSConfig :
8+ def check_variables_are_defined ():
9+ auth_variables = ['AZURE_APP_CONFIGURATION_CONNECTION_STRING' ]
10+ for var in auth_variables :
11+ if var not in os .environ :
12+ raise EnvironmentError (
13+ "{} is not defined in the environment" .format (var )
14+ )
15+
16+ check_variables_are_defined ()
17+ AZURE_APP_CONFIGURATION_CONNECTION_STRING = os .environ .get (
918 'AZURE_APP_CONFIGURATION_CONNECTION_STRING'
1019 )
1120
12- def __init__ (self , key : str , label : str = None ):
21+
22+ class FeatureToggleManager :
23+ def __init__ (self , key : str , label : str = None , config = MSConfig ):
1324 self .key = key
1425 self .label = label
26+ self .config = config
27+ self .client = self .get_azure_app_configuration_client ()
1528 self .configuration = {}
1629
17- def get_configuration (self , key : str , label : str ):
18- connection_str = self .AZURE_CONNECTION_STRING
30+ def get_azure_app_configuration_client (self ):
31+ connection_str = self .config . AZURE_APP_CONFIGURATION_CONNECTION_STRING
1932 client = AzureAppConfigurationClient .from_connection_string (
2033 connection_str
2134 )
22- configuration = client .get_configuration_setting (
35+
36+ return client
37+
38+ def get_configuration (self , key : str , label : str ):
39+ configuration = self .client .get_configuration_setting (
2340 key = f".appconfig.featureflag/{ self .key } " , label = self .label
2441 )
2542
@@ -37,15 +54,16 @@ def is_toggle_enabled(self):
3754
3855 return result
3956
40- def is_toggle_enabled_for_user (self ):
57+ def get_list_users (self ):
4158 data = self .get_data_configuration ()
4259 client_filters = data ["conditions" ]["client_filters" ]
4360 first_client = client_filters [0 ]
4461 list_users = first_client ["parameters" ]["Audience" ]["Users" ]
62+
63+ return list_users
64+
65+ def is_toggle_enabled_for_user (self ):
66+ list_users = self .get_list_users ()
4567 current_user = current_user_email ()
4668
47- return (
48- True
49- if current_user in list_users and self .is_toggle_enabled ()
50- else False
51- )
69+ return current_user in list_users and self .is_toggle_enabled ()
0 commit comments