1111
1212_LOGGER = logging .getLogger (__name__ )
1313
14- class SplitProvider (AbstractProvider ):
15-
16- def __init__ (self , initial_context ):
17- self ._split_client_wrapper = SplitClientWrapper (initial_context )
14+ class SplitProviderBase (AbstractProvider ):
1815
1916 def get_metadata (self ) -> Metadata :
2017 return Metadata ("Split" )
2118
2219 def get_provider_hooks (self ) -> typing .List [Hook ]:
2320 return []
2421
25- def resolve_boolean_details (self , flag_key : str , default_value : bool ,
26- evaluation_context : EvaluationContext = EvaluationContext ()):
27- return self ._evaluate_treatment (flag_key , evaluation_context , default_value )
28-
29- def resolve_string_details (self , flag_key : str , default_value : str ,
30- evaluation_context : EvaluationContext = EvaluationContext ()):
31- return self ._evaluate_treatment (flag_key , evaluation_context , default_value )
32-
33- def resolve_integer_details (self , flag_key : str , default_value : int ,
34- evaluation_context : EvaluationContext = EvaluationContext ()):
35- return self ._evaluate_treatment (flag_key , evaluation_context , default_value )
36-
37- def resolve_float_details (self , flag_key : str , default_value : float ,
38- evaluation_context : EvaluationContext = EvaluationContext ()):
39- return self ._evaluate_treatment (flag_key , evaluation_context , default_value )
40-
41- def resolve_object_details (self , flag_key : str , default_value : dict ,
42- evaluation_context : EvaluationContext = EvaluationContext ()):
43- return self ._evaluate_treatment (flag_key , evaluation_context , default_value )
44-
4522 def _evaluate_treatment (self , key : str , evaluation_context : EvaluationContext , default_value ):
4623 if evaluation_context is None :
4724 raise GeneralError ("Evaluation Context must be provided for the Split Provider" )
@@ -54,9 +31,12 @@ def _evaluate_treatment(self, key: str, evaluation_context: EvaluationContext, d
5431 if not targeting_key :
5532 raise TargetingKeyMissingError ("Missing targeting key" )
5633
34+ attributes = SplitProvider .transform_context (evaluation_context )
35+ evaluated = self ._split_client_wrapper .split_client .get_treatment_with_config (targeting_key , key , attributes )
36+ return self ._process_treatment (evaluated , default_value )
37+
38+ def _process_treatment (self , evaluated , default_value ):
5739 try :
58- attributes = SplitProvider .transform_context (evaluation_context )
59- evaluated = self ._split_client_wrapper .split_client .get_treatment_with_config (targeting_key , key , attributes )
6040 treatment = None
6141 config = None
6242 if evaluated != None :
@@ -116,3 +96,111 @@ def construct_flag_resolution(value, variant, config, reason: Reason = Reason.TA
11696 error_code : ErrorCode = None ):
11797 return FlagResolutionDetails (value = value , error_code = error_code , reason = reason , variant = variant ,
11898 flag_metadata = {"config" : config })
99+
100+ def resolve_boolean_details (self , flag_key : str , default_value : bool ,
101+ evaluation_context : EvaluationContext = EvaluationContext ()):
102+ pass
103+
104+ def resolve_string_details (self , flag_key : str , default_value : str ,
105+ evaluation_context : EvaluationContext = EvaluationContext ()):
106+ pass
107+
108+ def resolve_integer_details (self , flag_key : str , default_value : int ,
109+ evaluation_context : EvaluationContext = EvaluationContext ()):
110+ pass
111+
112+ def resolve_float_details (self , flag_key : str , default_value : float ,
113+ evaluation_context : EvaluationContext = EvaluationContext ()):
114+ pass
115+
116+ def resolve_object_details (self , flag_key : str , default_value : dict ,
117+ evaluation_context : EvaluationContext = EvaluationContext ()):
118+ pass
119+
120+ async def resolve_boolean_details_async (self , flag_key : str , default_value : bool ,
121+ evaluation_context : EvaluationContext = EvaluationContext ()):
122+ pass
123+
124+ async def resolve_string_details_async (self , flag_key : str , default_value : str ,
125+ evaluation_context : EvaluationContext = EvaluationContext ()):
126+ pass
127+ async def resolve_integer_details_async (self , flag_key : str , default_value : int ,
128+ evaluation_context : EvaluationContext = EvaluationContext ()):
129+ pass
130+
131+ async def resolve_float_details_async (self , flag_key : str , default_value : float ,
132+ evaluation_context : EvaluationContext = EvaluationContext ()):
133+ pass
134+
135+ async def resolve_object_details_async (self , flag_key : str , default_value : dict ,
136+ evaluation_context : EvaluationContext = EvaluationContext ()):
137+ pass
138+
139+ class SplitProvider (SplitProviderBase ):
140+ def __init__ (self , initial_context ):
141+ self ._split_client_wrapper = SplitClientWrapper (initial_context )
142+
143+ def resolve_boolean_details (self , flag_key : str , default_value : bool ,
144+ evaluation_context : EvaluationContext = EvaluationContext ()):
145+ return self ._evaluate_treatment (flag_key , evaluation_context , default_value )
146+
147+ def resolve_string_details (self , flag_key : str , default_value : str ,
148+ evaluation_context : EvaluationContext = EvaluationContext ()):
149+ return self ._evaluate_treatment (flag_key , evaluation_context , default_value )
150+
151+ def resolve_integer_details (self , flag_key : str , default_value : int ,
152+ evaluation_context : EvaluationContext = EvaluationContext ()):
153+ return self ._evaluate_treatment (flag_key , evaluation_context , default_value )
154+
155+ def resolve_float_details (self , flag_key : str , default_value : float ,
156+ evaluation_context : EvaluationContext = EvaluationContext ()):
157+ return self ._evaluate_treatment (flag_key , evaluation_context , default_value )
158+
159+ def resolve_object_details (self , flag_key : str , default_value : dict ,
160+ evaluation_context : EvaluationContext = EvaluationContext ()):
161+ return self ._evaluate_treatment (flag_key , evaluation_context , default_value )
162+
163+ class SplitProviderAsync (SplitProviderBase ):
164+ def __init__ (self , initial_context ):
165+ if isinstance (initial_context , dict ):
166+ initial_context ["ThreadingMode" ] = "asyncio"
167+ self ._split_client_wrapper = SplitClientWrapper (initial_context )
168+
169+ async def create (self ):
170+ await self ._split_client_wrapper .create ()
171+
172+ async def resolve_boolean_details_async (self , flag_key : str , default_value : bool ,
173+ evaluation_context : EvaluationContext = EvaluationContext ()):
174+ return await self ._evaluate_treatment_async (flag_key , evaluation_context , default_value )
175+
176+ async def resolve_string_details_async (self , flag_key : str , default_value : str ,
177+ evaluation_context : EvaluationContext = EvaluationContext ()):
178+ return await self ._evaluate_treatment_async (flag_key , evaluation_context , default_value )
179+
180+ async def resolve_integer_details_async (self , flag_key : str , default_value : int ,
181+ evaluation_context : EvaluationContext = EvaluationContext ()):
182+ return await self ._evaluate_treatment_async (flag_key , evaluation_context , default_value )
183+
184+ async def resolve_float_details_async (self , flag_key : str , default_value : float ,
185+ evaluation_context : EvaluationContext = EvaluationContext ()):
186+ return await self ._evaluate_treatment_async (flag_key , evaluation_context , default_value )
187+
188+ async def resolve_object_details_async (self , flag_key : str , default_value : dict ,
189+ evaluation_context : EvaluationContext = EvaluationContext ()):
190+ return await self ._evaluate_treatment_async (flag_key , evaluation_context , default_value )
191+
192+ async def _evaluate_treatment_async (self , key : str , evaluation_context : EvaluationContext , default_value ):
193+ if evaluation_context is None :
194+ raise GeneralError ("Evaluation Context must be provided for the Split Provider" )
195+
196+ if not await self ._split_client_wrapper .is_sdk_ready_async ():
197+ return SplitProvider .construct_flag_resolution (default_value , None , None , Reason .ERROR ,
198+ ErrorCode .PROVIDER_NOT_READY )
199+
200+ targeting_key = evaluation_context .targeting_key
201+ if not targeting_key :
202+ raise TargetingKeyMissingError ("Missing targeting key" )
203+
204+ attributes = SplitProvider .transform_context (evaluation_context )
205+ evaluated = await self ._split_client_wrapper .split_client .get_treatment_with_config (targeting_key , key , attributes )
206+ return self ._process_treatment (evaluated , default_value )
0 commit comments