-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support transient identities and traits (#93)
* feat: Support transient identities and traits - Support transient identities and traits - Bump requests - Bump mypy - Remove linting from CI in favour of pre-commit.ci
- Loading branch information
Showing
10 changed files
with
166 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import typing | ||
|
||
from flag_engine.identities.traits.types import TraitValue | ||
from typing_extensions import TypeAlias | ||
|
||
_JsonScalarType: TypeAlias = typing.Union[ | ||
int, | ||
str, | ||
float, | ||
bool, | ||
None, | ||
] | ||
JsonType: TypeAlias = typing.Union[ | ||
_JsonScalarType, | ||
typing.Dict[str, "JsonType"], | ||
typing.List["JsonType"], | ||
] | ||
|
||
|
||
class TraitConfig(typing.TypedDict): | ||
value: TraitValue | ||
transient: bool | ||
|
||
|
||
TraitMapping: TypeAlias = typing.Mapping[str, typing.Union[TraitValue, TraitConfig]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import typing | ||
|
||
from flag_engine.identities.traits.types import TraitValue | ||
from flagsmith.types import JsonType, TraitMapping | ||
|
||
Identity = typing.TypedDict( | ||
"Identity", | ||
{"identifier": str, "traits": typing.List[typing.Mapping[str, TraitValue]]}, | ||
) | ||
|
||
|
||
def generate_identities_data( | ||
identifier: str, traits: typing.Optional[typing.Mapping[str, TraitValue]] = None | ||
) -> Identity: | ||
return { | ||
"identifier": identifier, | ||
"traits": ( | ||
[{"trait_key": k, "trait_value": v} for k, v in traits.items()] | ||
if traits | ||
else [] | ||
), | ||
} | ||
def generate_identity_data( | ||
identifier: str, | ||
traits: TraitMapping, | ||
*, | ||
transient: bool, | ||
) -> JsonType: | ||
identity_data: typing.Dict[str, JsonType] = {"identifier": identifier} | ||
traits_data: typing.List[JsonType] = [] | ||
for trait_key, trait_value in traits.items(): | ||
trait_data: typing.Dict[str, JsonType] = {"trait_key": trait_key} | ||
if isinstance(trait_value, dict): | ||
trait_data["trait_value"] = trait_value["value"] | ||
if trait_value.get("transient"): | ||
trait_data["transient"] = True | ||
else: | ||
trait_data["trait_value"] = trait_value | ||
traits_data.append(trait_data) | ||
identity_data["traits"] = traits_data | ||
if transient: | ||
identity_data["transient"] = True | ||
return identity_data |
Oops, something went wrong.