-
Notifications
You must be signed in to change notification settings - Fork 2
feat(schemas): Add SDK API request and response schemas #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+413
−32
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c8dd426
add flasgmith-engine dependency, bump locked version on releases
khvn26 6921024
feat(schemas): Add SDK API request and response schemas
khvn26 1a85068
clarify project docstring
khvn26 1104e66
clarify MultivariateFeatureStateValue docstring
khvn26 492b805
fix typo
khvn26 035f665
clarify nested rules
khvn26 8a56b7a
fix the doc
khvn26 b5cb407
fix `FeatureState.feature_state_value` type
khvn26 d3c90b4
rename `InputTrait` to `TraitInput` for consistency in type naming
khvn26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,217 @@ | ||
| """ | ||
| The types in this module describe Flagsmith SDK API request and response schemas. | ||
| The docstrings here comprise user-facing documentation for these types. | ||
|
|
||
| The types are used by: | ||
| - SDK API OpenAPI schema generation. | ||
| - Flagsmith's API and SDK implementations written in Python. | ||
|
|
||
| These types can be used with for validation and serialization | ||
| with any library that supports TypedDict, such as Pydantic or typeguard. | ||
|
|
||
| When updating this module, ensure that the changes are backwards compatible. | ||
| """ | ||
|
|
||
| from flag_engine.engine import ContextValue | ||
| from flag_engine.segments.types import ConditionOperator, RuleType | ||
| from typing_extensions import NotRequired, TypedDict | ||
|
|
||
| from flagsmith_schemas.types import FeatureType, FeatureValue, UUIDStr | ||
|
|
||
|
|
||
| class Feature(TypedDict): | ||
| """Represents a Flagsmith feature, defined at project level.""" | ||
|
|
||
| id: int | ||
| """Unique identifier for the feature in Core.""" | ||
| name: str | ||
| """Name of the feature. Must be unique within a project.""" | ||
| type: FeatureType | ||
| """Feature type.""" | ||
|
|
||
|
|
||
| class MultivariateFeatureOption(TypedDict): | ||
| """Represents a single multivariate feature option in the Flagsmith UI.""" | ||
|
|
||
| value: str | ||
| """The feature state value that should be served when this option's parent multivariate feature state is selected by the engine.""" | ||
|
|
||
|
|
||
| class MultivariateFeatureStateValue(TypedDict): | ||
| """Represents a multivariate feature state value.""" | ||
emyller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| id: int | None | ||
| """Unique identifier for the multivariate feature state value in Core. Used for multivariate bucketing. If feature state created via `edge-identities` APIs in Core, this can be missing or `None`.""" | ||
| mv_fs_value_uuid: UUIDStr | None | ||
| """The UUID for this multivariate feature state value. Should be used for multivariate bucketing if `id` is null.""" | ||
| percentage_allocation: float | ||
| """The percentage allocation for this multivariate feature state value. Should be between or equal to 0 and 100; total percentage allocation of grouped `MultivariateFeatureStateValue` must not exceed 100.""" | ||
| multivariate_feature_option: MultivariateFeatureOption | ||
| """The multivariate feature option that this value corresponds to.""" | ||
|
|
||
|
|
||
| class FeatureSegment(TypedDict): | ||
| """Represents data specific to a segment feature override.""" | ||
|
|
||
| priority: int | None | ||
Zaimwa9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """The priority of this segment feature override. Lower numbers indicate stronger priority. If null or not set, the weakest priority is assumed.""" | ||
khvn26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class FeatureState(TypedDict): | ||
| """Used to define the state of a feature for an environment, segment overrides, and identity overrides.""" | ||
emyller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| feature: Feature | ||
| """The feature that this feature state is for.""" | ||
| enabled: bool | ||
| """Whether the feature is enabled or disabled.""" | ||
| feature_state_value: FeatureValue | ||
| """The value for this feature state.""" | ||
| featurestate_uuid: UUIDStr | ||
| """The UUID for this feature state.""" | ||
| feature_segment: FeatureSegment | None | ||
| """Segment override data, if this feature state is for a segment override.""" | ||
| multivariate_feature_state_values: list[MultivariateFeatureStateValue] | ||
| """List of multivariate feature state values, if this feature state is for a multivariate feature.""" | ||
|
|
||
|
|
||
| class Trait(TypedDict): | ||
| """Represents a key-value pair associated with an identity.""" | ||
|
|
||
| trait_key: str | ||
| """Key of the trait.""" | ||
| trait_value: ContextValue | ||
| """Value of the trait.""" | ||
|
|
||
|
|
||
| class SegmentCondition(TypedDict): | ||
| """Represents a condition within a segment rule used by Flagsmith engine.""" | ||
|
|
||
| operator: ConditionOperator | ||
| """Operator to be applied for this condition.""" | ||
| value: str | ||
| """Value to be compared against in this condition. May be `None` for `IS_SET` and `IS_NOT_SET` operators.""" | ||
| property_: str | ||
| """The property (context key) this condition applies to. May be `None` for the `PERCENTAGE_SPLIT` operator. | ||
|
|
||
| Named `property_` for legacy reasons. | ||
| """ | ||
emyller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class SegmentRule(TypedDict): | ||
| """Represents a rule within a segment used by Flagsmith engine. Root rules usually contain nested rules.""" | ||
|
|
||
| type: RuleType | ||
| """Type of the rule, defining how conditions are evaluated.""" | ||
| rules: "list[SegmentRule]" | ||
| """Nested rules within this rule.""" | ||
| conditions: list[SegmentCondition] | ||
| """Conditions that must be met for this rule, evaluated based on the rule type.""" | ||
|
|
||
|
|
||
| class Segment(TypedDict): | ||
| """Represents a Flagsmith segment. Carries rules and feature overrides.""" | ||
|
|
||
| id: int | ||
| """Unique identifier for the segment in Core.""" | ||
| name: str | ||
| """Segment name.""" | ||
| rules: list[SegmentRule] | ||
| """List of rules within the segment.""" | ||
| feature_states: NotRequired[list[FeatureState]] | ||
| """List of segment overrides.""" | ||
|
|
||
|
|
||
| class Project(TypedDict): | ||
| """Represents a Flagsmith project. For SDKs, this is mainly used to convey segment data.""" | ||
|
|
||
| segments: list[Segment] | ||
| """List of segments.""" | ||
|
|
||
|
|
||
| class IdentityOverride(TypedDict): | ||
| """Represents an identity override, defining feature states specific to an identity.""" | ||
|
|
||
| identifier: str | ||
| """Unique identifier for the identity.""" | ||
| identity_features: list[FeatureState] | ||
| """List of identity overrides for this identity.""" | ||
|
|
||
|
|
||
| class TraitInput(TypedDict): | ||
| """Represents a key-value pair trait provided as input when creating or updating an identity.""" | ||
|
|
||
| trait_key: str | ||
| """Trait key.""" | ||
| trait_value: ContextValue | ||
| """Trait value. If `null`, the trait will be deleted.""" | ||
| transient: NotRequired[bool | None] | ||
| """Whether this trait is transient (not persisted). Defaults to `false`.""" | ||
|
|
||
|
|
||
| class V1Flag(TypedDict): | ||
| """Represents a single flag (feature state) returned by the Flagsmith SDK.""" | ||
|
|
||
| feature: Feature | ||
| """The feature that this flag represents.""" | ||
| enabled: bool | ||
| """Whether the feature is enabled or disabled.""" | ||
| feature_state_value: FeatureValue | ||
| """The value for this feature state.""" | ||
|
|
||
|
|
||
| ### Root request schemas below. ### | ||
|
|
||
|
|
||
| class V1IdentitiesRequest(TypedDict): | ||
| """`/api/v1/identities/` request. | ||
|
|
||
| Used to retrieve flags for an identity and store its traits. | ||
| """ | ||
|
|
||
| identifier: str | ||
| """Unique identifier for the identity.""" | ||
| traits: NotRequired[list[TraitInput] | None] | ||
| """List of traits to set for the identity. If `null` or not provided, no traits are set or updated.""" | ||
| transient: NotRequired[bool | None] | ||
| """Whether the identity is transient (not persisted). Defaults to `false`.""" | ||
|
|
||
|
|
||
| ### Root response schemas below. ### | ||
|
|
||
|
|
||
| class V1EnvironmentDocumentResponse(TypedDict): | ||
| """`/api/v1/environment-documents/` response. | ||
|
|
||
| Powers Flagsmith SDK's local evaluation mode. | ||
| """ | ||
|
|
||
| api_key: str | ||
| """Public client-side API key for the environment, used to identify it.""" | ||
| feature_states: list[FeatureState] | ||
| """List of feature states representing the environment defaults.""" | ||
| identity_overrides: list[IdentityOverride] | ||
| """List of identity overrides defined for this environment.""" | ||
| name: str | ||
| """Environment name.""" | ||
| project: Project | ||
| """Project-specific data for this environment.""" | ||
|
|
||
|
|
||
| V1FlagsResponse = list[V1Flag] | ||
| """`/api/v1/flags/` response. | ||
|
|
||
| A list of flags for the specified environment.""" | ||
|
|
||
|
|
||
| class V1IdentitiesResponse(TypedDict): | ||
| """`/api/v1/identities/` response. | ||
|
|
||
| Represents the identity created or updated, along with its flags. | ||
| """ | ||
|
|
||
| identifier: str | ||
| """Unique identifier for the identity.""" | ||
| flags: list[V1Flag] | ||
| """List of flags (feature states) for the identity.""" | ||
| traits: list[Trait] | ||
| """List of traits associated with the identity.""" | ||
This file contains hidden or 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 hidden or 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 hidden or 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,18 @@ | ||
| from pydantic import TypeAdapter | ||
|
|
||
| from flagsmith_schemas.api import FeatureState | ||
|
|
||
|
|
||
| def test_feature_state__featurestate_uuid__expected_json_schema() -> None: | ||
| # Given | ||
| type_adapter: TypeAdapter[FeatureState] = TypeAdapter(FeatureState) | ||
|
|
||
| # When | ||
| schema = type_adapter.json_schema()["properties"]["featurestate_uuid"] | ||
|
|
||
| # Then | ||
| assert schema == { | ||
| "format": "uuid", | ||
| "title": "Featurestate Uuid", | ||
| "type": "string", | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.