11import typing
22from json import JSONDecodeError
33
4- from open_feature .hooks .hook import Hook
5- from open_feature .provider import provider
6- from open_feature .evaluation_context .evaluation_context import EvaluationContext
7- from open_feature .exception import exceptions
8- from open_feature .exception .error_code import ErrorCode
9- from open_feature .flag_evaluation .reason import Reason
10- from open_feature .flag_evaluation .flag_evaluation_details import FlagEvaluationDetails
4+ from openfeature .hook import Hook
5+ from openfeature .evaluation_context import EvaluationContext
6+ from openfeature .exception import ErrorCode , GeneralError , ParseError , OpenFeatureError , TargetingKeyMissingError
7+ from openfeature .flag_evaluation import Reason , FlagResolutionDetails
8+ from openfeature .provider import AbstractProvider , Metadata
119from splitio import get_factory
1210from splitio .exceptions import TimeoutException
1311import json
1412
1513
16- class SplitProvider (provider . AbstractProvider ):
14+ class SplitProvider (AbstractProvider ):
1715
1816 def __init__ (self , api_key = "" , client = None ):
1917 if api_key == "" and client is None :
@@ -23,13 +21,13 @@ def __init__(self, api_key="", client=None):
2321 try :
2422 factory .block_until_ready (1 )
2523 except TimeoutException :
26- raise exceptions . GeneralError ("Error occurred initializing the client." )
24+ raise GeneralError ("Error occurred initializing the client." )
2725 self .split_client = factory .client ()
2826 else :
2927 self .split_client = client
3028
31- def get_metadata (self ) -> provider . Metadata :
32- return provider . Metadata ("Split" )
29+ def get_metadata (self ) -> Metadata :
30+ return Metadata ("Split" )
3331
3432 def get_provider_hooks (self ) -> typing .List [Hook ]:
3533 return []
@@ -39,93 +37,93 @@ def resolve_boolean_details(self, flag_key: str, default_value: bool,
3937 try :
4038 evaluated = self .evaluate_treatment (flag_key , evaluation_context )
4139 if SplitProvider .no_treatment (evaluated ):
42- return SplitProvider .construct_provider_evaluation ( flag_key , default_value , evaluated , Reason .DEFAULT ,
43- ErrorCode .FLAG_NOT_FOUND )
40+ return SplitProvider .construct_flag_resolution ( default_value , evaluated , Reason .DEFAULT ,
41+ ErrorCode .FLAG_NOT_FOUND )
4442 evaluated_lower = evaluated .lower ()
4543 if evaluated_lower in ["true" , "on" ]:
4644 value = True
4745 elif evaluated_lower in ["false" , "off" ]:
4846 value = False
4947 else :
50- raise exceptions . ParseError ("Could not convert treatment to boolean" )
51- return SplitProvider .construct_provider_evaluation ( flag_key , value , evaluated )
52- except exceptions . OpenFeatureError :
48+ raise ParseError ("Could not convert treatment to boolean" )
49+ return SplitProvider .construct_flag_resolution ( value , evaluated )
50+ except OpenFeatureError :
5351 raise
5452 except Exception :
55- raise exceptions . GeneralError ("Error getting boolean evaluation" )
53+ raise GeneralError ("Error getting boolean evaluation" )
5654
5755 def resolve_string_details (self , flag_key : str , default_value : str ,
5856 evaluation_context : EvaluationContext = EvaluationContext ()):
5957 try :
6058 evaluated = self .evaluate_treatment (flag_key , evaluation_context )
6159 if SplitProvider .no_treatment (evaluated ):
62- return SplitProvider .construct_provider_evaluation ( flag_key , default_value , evaluated , Reason .DEFAULT ,
63- ErrorCode .FLAG_NOT_FOUND )
64- return SplitProvider .construct_provider_evaluation ( flag_key , evaluated , evaluated )
65- except exceptions . OpenFeatureError :
60+ return SplitProvider .construct_flag_resolution ( default_value , evaluated , Reason .DEFAULT ,
61+ ErrorCode .FLAG_NOT_FOUND )
62+ return SplitProvider .construct_flag_resolution ( evaluated , evaluated )
63+ except OpenFeatureError :
6664 raise
6765 except Exception :
68- raise exceptions . GeneralError ("Error getting boolean evaluation" )
66+ raise GeneralError ("Error getting boolean evaluation" )
6967
7068 def resolve_integer_details (self , flag_key : str , default_value : int ,
7169 evaluation_context : EvaluationContext = EvaluationContext ()):
7270 try :
7371 evaluated = self .evaluate_treatment (flag_key , evaluation_context )
7472 if SplitProvider .no_treatment (evaluated ):
75- return SplitProvider .construct_provider_evaluation ( flag_key , default_value , evaluated , Reason .DEFAULT ,
76- ErrorCode .FLAG_NOT_FOUND )
73+ return SplitProvider .construct_flag_resolution ( default_value , evaluated , Reason .DEFAULT ,
74+ ErrorCode .FLAG_NOT_FOUND )
7775 try :
7876 value = int (evaluated )
7977 except ValueError :
80- raise exceptions . ParseError ("Could not convert treatment to integer" )
81- return SplitProvider .construct_provider_evaluation ( flag_key , value , evaluated )
82- except exceptions . OpenFeatureError :
78+ raise ParseError ("Could not convert treatment to integer" )
79+ return SplitProvider .construct_flag_resolution ( value , evaluated )
80+ except OpenFeatureError :
8381 raise
8482 except Exception :
85- raise exceptions . GeneralError ("Error getting boolean evaluation" )
83+ raise GeneralError ("Error getting boolean evaluation" )
8684
8785 def resolve_float_details (self , flag_key : str , default_value : float ,
8886 evaluation_context : EvaluationContext = EvaluationContext ()):
8987 try :
9088 evaluated = self .evaluate_treatment (flag_key , evaluation_context )
9189 if SplitProvider .no_treatment (evaluated ):
92- return SplitProvider .construct_provider_evaluation ( flag_key , default_value , evaluated , Reason .DEFAULT ,
93- ErrorCode .FLAG_NOT_FOUND )
90+ return SplitProvider .construct_flag_resolution ( default_value , evaluated , Reason .DEFAULT ,
91+ ErrorCode .FLAG_NOT_FOUND )
9492 try :
9593 value = float (evaluated )
9694 except ValueError :
97- raise exceptions . ParseError ("Could not convert treatment to float" )
98- return SplitProvider .construct_provider_evaluation ( flag_key , value , evaluated )
99- except exceptions . OpenFeatureError :
95+ raise ParseError ("Could not convert treatment to float" )
96+ return SplitProvider .construct_flag_resolution ( value , evaluated )
97+ except OpenFeatureError :
10098 raise
10199 except Exception :
102- raise exceptions . GeneralError ("Error getting boolean evaluation" )
100+ raise GeneralError ("Error getting boolean evaluation" )
103101
104102 def resolve_object_details (self , flag_key : str , default_value : dict ,
105103 evaluation_context : EvaluationContext = EvaluationContext ()):
106104 try :
107105 evaluated = self .evaluate_treatment (flag_key , evaluation_context )
108106 if SplitProvider .no_treatment (evaluated ):
109- return SplitProvider .construct_provider_evaluation ( flag_key , default_value , evaluated , Reason .DEFAULT ,
110- ErrorCode .FLAG_NOT_FOUND )
107+ return SplitProvider .construct_flag_resolution ( default_value , evaluated , Reason .DEFAULT ,
108+ ErrorCode .FLAG_NOT_FOUND )
111109 value = json .loads (evaluated )
112- return SplitProvider .construct_provider_evaluation ( flag_key , value , evaluated )
110+ return SplitProvider .construct_flag_resolution ( value , evaluated )
113111 except JSONDecodeError :
114- raise exceptions . ParseError ("Could not convert treatment to dict" )
115- except exceptions . OpenFeatureError :
112+ raise ParseError ("Could not convert treatment to dict" )
113+ except OpenFeatureError :
116114 raise
117115 except Exception :
118- raise exceptions . GeneralError ("Error getting boolean evaluation" )
116+ raise GeneralError ("Error getting boolean evaluation" )
119117
120118 # *** --- Helpers --- ***
121119
122120 def evaluate_treatment (self , key : str , evaluation_context : EvaluationContext ):
123121 if evaluation_context is None :
124- raise exceptions . GeneralError ("Evaluation Context must be provided for the Split Provider" )
122+ raise GeneralError ("Evaluation Context must be provided for the Split Provider" )
125123
126124 targeting_key = evaluation_context .targeting_key
127125 if not targeting_key :
128- raise exceptions . TargetingKeyMissingError ("Missing targeting key" )
126+ raise TargetingKeyMissingError ("Missing targeting key" )
129127
130128 attributes = SplitProvider .transform_context (evaluation_context )
131129 return self .split_client .get_treatment (targeting_key , key , attributes )
@@ -139,7 +137,6 @@ def no_treatment(treatment: str):
139137 return not treatment or treatment == "control"
140138
141139 @staticmethod
142- def construct_provider_evaluation (key : str , value , variant : str , reason : Reason = Reason .TARGETING_MATCH ,
143- error_code : ErrorCode = None ):
144- return FlagEvaluationDetails (flag_key = key , value = value , reason = reason ,
145- error_code = error_code , variant = variant )
140+ def construct_flag_resolution (value , variant : str , reason : Reason = Reason .TARGETING_MATCH ,
141+ error_code : ErrorCode = None ):
142+ return FlagResolutionDetails (value = value , error_code = error_code , reason = reason , variant = variant )
0 commit comments