Skip to content

Commit

Permalink
transient_traits -> transient_trait_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
khvn26 committed Jul 18, 2024
1 parent 69d5769 commit b0e85d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
16 changes: 11 additions & 5 deletions flagsmith/flagsmith.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def get_identity_flags(
traits: typing.Optional[typing.Mapping[str, TraitValue]] = None,
*,
transient: bool = False,
transient_traits: typing.Optional[typing.List[str]] = None,
transient_trait_keys: typing.Optional[typing.List[str]] = None,
) -> Flags:
"""
Get all the flags for the current environment for a given identity. Will also
Expand All @@ -243,15 +243,18 @@ def get_identity_flags(
:param traits: a dictionary of traits to add / update on the identity in
Flagsmith, e.g. `{"num_orders": 10}`
:param transient: if `True`, the identity won't get persisted
:param transient_traits: a list of trait keys that won't get persisted,
:param transient_trait_keys: a list of trait keys that won't get persisted,
e.g. `["num_orders"]`
:return: Flags object holding all the flags for the given identity.
"""
traits = traits or {}
if (self.offline_mode or self.enable_local_evaluation) and self._environment:
return self._get_identity_flags_from_document(identifier, traits)
return self._get_identity_flags_from_api(
identifier, traits, transient=transient, transient_traits=transient_traits
identifier,
traits,
transient=transient,
transient_trait_keys=transient_trait_keys,
)

def get_identity_segments(
Expand Down Expand Up @@ -343,10 +346,13 @@ def _get_identity_flags_from_api(
traits: typing.Mapping[str, typing.Any],
*,
transient: bool = False,
transient_traits: typing.Optional[typing.List[str]] = None,
transient_trait_keys: typing.Optional[typing.List[str]] = None,
) -> Flags:
request_body = generate_identity_data(
identifier, traits, transient=transient, transient_traits=transient_traits
identifier,
traits,
transient=transient,
transient_trait_keys=transient_trait_keys,
)
try:
json_response: typing.Dict[str, typing.List[typing.Dict[str, JsonType]]] = (
Expand Down
8 changes: 5 additions & 3 deletions flagsmith/utils/identities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ def generate_identity_data(
traits: typing.Optional[typing.Mapping[str, TraitValue]],
*,
transient: bool,
transient_traits: typing.Optional[typing.List[str]],
transient_trait_keys: typing.Optional[typing.List[str]],
) -> JsonType:
identity_data: typing.Dict[str, JsonType] = {"identifier": identifier, "traits": []}
if traits:
traits_data: typing.List[JsonType] = []
transient_trait_keys = set(transient_traits) if transient_traits else set()
transient_trait_keys_set = (
set(transient_trait_keys) if transient_trait_keys else set()
)
for trait_key, trait_value in traits.items():
trait_data: typing.Dict[str, JsonType] = {
"trait_key": trait_key,
"trait_value": trait_value,
}
if trait_key in transient_trait_keys:
if trait_key in transient_trait_keys_set:
trait_data["transient"] = True
traits_data.append(trait_data)
identity_data["traits"] = traits_data
Expand Down
4 changes: 2 additions & 2 deletions tests/test_flagsmith.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def test_get_identity_flags__transient_identity__calls_expected(


@responses.activate()
def test_get_identity_flags__transient_traits__calls_expected(
def test_get_identity_flags__transient_trait_keys__calls_expected(
flagsmith: Flagsmith,
identities_json: str,
environment_model: EnvironmentModel,
Expand Down Expand Up @@ -236,7 +236,7 @@ def test_get_identity_flags__transient_traits__calls_expected(
flagsmith.get_identity_flags(
"identifier",
traits={"some_trait": "some_value"},
transient_traits=["some_trait"],
transient_trait_keys=["some_trait"],
)


Expand Down

0 comments on commit b0e85d8

Please sign in to comment.