-
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
base: main
Are you sure you want to change the base?
Changes from all commits
c8dd426
6921024
1a85068
1104e66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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.""" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as my other reply. |
||||||
|
|
||||||
| 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 | ||||||
| """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.""" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe we use the "feature override" term in relation to environment feature states anywhere else in the product, or the documentation. |
||||||
|
|
||||||
| feature: Feature | ||||||
| """The feature that this feature state is for.""" | ||||||
| enabled: bool | ||||||
| """Whether the feature is enabled or disabled.""" | ||||||
| feature_state_value: object | ||||||
| """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. | ||||||
| """ | ||||||
|
Comment on lines
+93
to
+97
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe worth introducing a new attribute
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The proposal does sound good to me, but it's out of the scope of this PR; we're documenting the status quo here. |
||||||
|
|
||||||
|
|
||||||
| class SegmentRule(TypedDict): | ||||||
| """Represents a rule within a segment used by Flagsmith engine.""" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my other comment. I appreciate the addition concerning recursion, however; I believe it's good to include this. |
||||||
|
|
||||||
| 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, feature overrides, and segment rules.""" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tend to disagree with this edit. We're not explaining the product here, rather, documenting the SDK API. Using Flagsmith terminology clearly documented/explained elsewhere is okay. |
||||||
|
|
||||||
| 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 InputTrait(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[InputTrait] | 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/environments-document/` 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.""" | ||||||
| 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", | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is out of scope, but is worth a note: I think this entity makes no sense in the API. I understand this represents the data model, but it also makes the API schema slightly harder to grasp as it adds hierarchy with no value.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its sole purpose is to inform the UI of which option is selected by an override. I guess it's for cases when multiple options bear the same value.
Definitely no value in the SDK context, I agree.