|
6 | 6 | from openfeature.exception import ErrorCode, GeneralError, ParseError, OpenFeatureError, TargetingKeyMissingError |
7 | 7 | from openfeature.flag_evaluation import Reason, FlagResolutionDetails |
8 | 8 | from openfeature.provider import AbstractProvider, Metadata |
9 | | -from splitio import get_factory |
10 | | -from splitio.exceptions import TimeoutException |
| 9 | +from split_openfeature.split_client_wrapper import SplitClientWrapper |
11 | 10 | import json |
12 | 11 |
|
13 | 12 | _LOGGER = logging.getLogger(__name__) |
14 | 13 |
|
15 | 14 | class SplitProvider(AbstractProvider): |
16 | 15 |
|
17 | | - def __init__(self, api_key="", client=None): |
18 | | - if api_key == "" and client is None: |
19 | | - raise Exception("Must provide apiKey or Split Client") |
20 | | - |
21 | | - if api_key != "": |
22 | | - factory = get_factory(api_key) |
23 | | - try: |
24 | | - factory.block_until_ready(1) |
25 | | - except TimeoutException: |
26 | | - raise GeneralError("Error occurred initializing the client.") |
27 | | - |
28 | | - self.split_client = factory.client() |
29 | | - else: |
30 | | - self.split_client = client |
| 16 | + def __init__(self, initial_context): |
| 17 | + self._split_client_wrapper = SplitClientWrapper(initial_context) |
31 | 18 |
|
32 | 19 | def get_metadata(self) -> Metadata: |
33 | 20 | return Metadata("Split") |
@@ -59,13 +46,17 @@ def _evaluate_treatment(self, key: str, evaluation_context: EvaluationContext, d |
59 | 46 | if evaluation_context is None: |
60 | 47 | raise GeneralError("Evaluation Context must be provided for the Split Provider") |
61 | 48 |
|
| 49 | + if not self._split_client_wrapper.is_sdk_ready(): |
| 50 | + return SplitProvider.construct_flag_resolution(default_value, None, None, Reason.ERROR, |
| 51 | + ErrorCode.PROVIDER_NOT_READY) |
| 52 | + |
62 | 53 | targeting_key = evaluation_context.targeting_key |
63 | 54 | if not targeting_key: |
64 | 55 | raise TargetingKeyMissingError("Missing targeting key") |
65 | 56 |
|
66 | 57 | try: |
67 | 58 | attributes = SplitProvider.transform_context(evaluation_context) |
68 | | - evaluated = self.split_client.get_treatment_with_config(targeting_key, key, attributes) |
| 59 | + evaluated = self._split_client_wrapper.split_client.get_treatment_with_config(targeting_key, key, attributes) |
69 | 60 | treatment = None |
70 | 61 | config = None |
71 | 62 | if evaluated != None: |
|
0 commit comments