Skip to content

Commit 03715ab

Browse files
chore: replacing-deprecated-methods (#143)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b6d2c84 commit 03715ab

File tree

6 files changed

+544
-339
lines changed

6 files changed

+544
-339
lines changed

flagsmith/flagsmith.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import pydantic
77
import requests
88
from flag_engine import engine
9+
from flag_engine.context.mappers import map_environment_identity_to_context
910
from flag_engine.environments.models import EnvironmentModel
1011
from flag_engine.identities.models import IdentityModel
1112
from flag_engine.identities.traits.models import TraitModel
1213
from flag_engine.identities.traits.types import TraitValue
13-
from flag_engine.segments.evaluator import get_identity_segments
1414
from requests.adapters import HTTPAdapter
1515
from requests.utils import default_user_agent
1616
from urllib3 import Retry
@@ -280,10 +280,18 @@ def get_identity_segments(
280280

281281
traits = traits or {}
282282
identity_model = self._get_identity_model(identifier, **traits)
283-
segment_models = get_identity_segments(
284-
environment=self._environment, identity=identity_model
283+
context = map_environment_identity_to_context(
284+
environment=self._environment,
285+
identity=identity_model,
286+
override_traits=None,
285287
)
286-
return [Segment(id=sm.id, name=sm.name) for sm in segment_models]
288+
evaluation_result = engine.get_evaluation_result(
289+
context=context,
290+
)
291+
return [
292+
Segment(id=int(sm["key"]), name=sm["name"])
293+
for sm in evaluation_result.get("segments", [])
294+
]
287295

288296
def update_environment(self) -> None:
289297
try:
@@ -321,8 +329,18 @@ def _get_environment_from_api(self) -> EnvironmentModel:
321329
def _get_environment_flags_from_document(self) -> Flags:
322330
if self._environment is None:
323331
raise TypeError("No environment present")
324-
return Flags.from_feature_state_models(
325-
feature_states=engine.get_environment_feature_states(self._environment),
332+
identity = self._get_identity_model(identifier="", traits=None)
333+
334+
context = map_environment_identity_to_context(
335+
environment=self._environment,
336+
identity=identity,
337+
override_traits=None,
338+
)
339+
340+
evaluation_result = engine.get_evaluation_result(context=context)
341+
342+
return Flags.from_evaluation_result(
343+
evaluation_result=evaluation_result,
326344
analytics_processor=self._analytics_processor,
327345
default_flag_handler=self.default_flag_handler,
328346
)
@@ -333,13 +351,20 @@ def _get_identity_flags_from_document(
333351
identity_model = self._get_identity_model(identifier, **traits)
334352
if self._environment is None:
335353
raise TypeError("No environment present")
336-
feature_states = engine.get_identity_feature_states(
337-
self._environment, identity_model
354+
355+
context = map_environment_identity_to_context(
356+
environment=self._environment,
357+
identity=identity_model,
358+
override_traits=None,
338359
)
339-
return Flags.from_feature_state_models(
340-
feature_states=feature_states,
360+
361+
evaluation_result = engine.get_evaluation_result(
362+
context=context,
363+
)
364+
365+
return Flags.from_evaluation_result(
366+
evaluation_result=evaluation_result,
341367
analytics_processor=self._analytics_processor,
342-
identity_id=identity_model.composite_key,
343368
default_flag_handler=self.default_flag_handler,
344369
)
345370

flagsmith/models.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import typing
44
from dataclasses import dataclass, field
55

6-
from flag_engine.features.models import FeatureStateModel
6+
from flag_engine.result.types import EvaluationResult, FlagResult
77

88
from flagsmith.analytics import AnalyticsProcessor
99
from flagsmith.exceptions import FlagsmithFeatureDoesNotExistError
@@ -27,16 +27,15 @@ class Flag(BaseFlag):
2727
is_default: bool = field(default=False)
2828

2929
@classmethod
30-
def from_feature_state_model(
30+
def from_evaluation_result(
3131
cls,
32-
feature_state_model: FeatureStateModel,
33-
identity_id: typing.Optional[typing.Union[str, int]] = None,
32+
flag: FlagResult,
3433
) -> Flag:
3534
return Flag(
36-
enabled=feature_state_model.enabled,
37-
value=feature_state_model.get_value(identity_id=identity_id),
38-
feature_name=feature_state_model.feature.name,
39-
feature_id=feature_state_model.feature.id,
35+
enabled=flag["enabled"],
36+
value=flag["value"],
37+
feature_name=flag["name"],
38+
feature_id=int(flag["feature_key"]),
4039
)
4140

4241
@classmethod
@@ -56,22 +55,22 @@ class Flags:
5655
_analytics_processor: typing.Optional[AnalyticsProcessor] = None
5756

5857
@classmethod
59-
def from_feature_state_models(
58+
def from_evaluation_result(
6059
cls,
61-
feature_states: typing.Sequence[FeatureStateModel],
60+
evaluation_result: EvaluationResult,
6261
analytics_processor: typing.Optional[AnalyticsProcessor],
6362
default_flag_handler: typing.Optional[typing.Callable[[str], DefaultFlag]],
64-
identity_id: typing.Optional[typing.Union[str, int]] = None,
6563
) -> Flags:
66-
flags = {
67-
feature_state.feature.name: Flag.from_feature_state_model(
68-
feature_state, identity_id=identity_id
69-
)
70-
for feature_state in feature_states
71-
}
72-
7364
return cls(
74-
flags=flags,
65+
flags={
66+
flag["name"]: Flag(
67+
enabled=flag["enabled"],
68+
value=flag["value"],
69+
feature_name=flag["name"],
70+
feature_id=int(flag["feature_key"]),
71+
)
72+
for flag in evaluation_result["flags"]
73+
},
7574
default_flag_handler=default_flag_handler,
7675
_analytics_processor=analytics_processor,
7776
)

0 commit comments

Comments
 (0)