-
Notifications
You must be signed in to change notification settings - Fork 26
feat: improve feature flag support #598
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
Open
vivekkhimani
wants to merge
2
commits into
workos:main
Choose a base branch
from
vivekkhimani:vivek/feature-flags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+408
−12
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,251 @@ | ||
| from typing import Optional, Protocol | ||
|
|
||
| from workos.types.feature_flags import FeatureFlag | ||
| from workos.types.feature_flags.list_filters import FeatureFlagListFilters | ||
| from workos.types.list_resource import ListMetadata, ListPage, WorkOSListResource | ||
| from workos.typing.sync_or_async import SyncOrAsync | ||
| from workos.utils.http_client import AsyncHTTPClient, SyncHTTPClient | ||
| from workos.utils.pagination_order import PaginationOrder | ||
| from workos.utils.request_helper import ( | ||
| DEFAULT_LIST_RESPONSE_LIMIT, | ||
| REQUEST_METHOD_DELETE, | ||
| REQUEST_METHOD_GET, | ||
| REQUEST_METHOD_POST, | ||
| REQUEST_METHOD_PUT, | ||
| ) | ||
|
|
||
| FEATURE_FLAGS_PATH = "feature-flags" | ||
|
|
||
| FeatureFlagsListResource = WorkOSListResource[ | ||
| FeatureFlag, FeatureFlagListFilters, ListMetadata | ||
| ] | ||
|
|
||
|
|
||
| class FeatureFlagsModule(Protocol): | ||
| """Offers methods through the WorkOS Feature Flags service.""" | ||
|
|
||
| def list_feature_flags( | ||
| self, | ||
| *, | ||
| limit: int = DEFAULT_LIST_RESPONSE_LIMIT, | ||
| before: Optional[str] = None, | ||
| after: Optional[str] = None, | ||
| order: PaginationOrder = "desc", | ||
| ) -> SyncOrAsync[FeatureFlagsListResource]: | ||
| """Retrieve a list of feature flags. | ||
|
|
||
| Kwargs: | ||
| limit (int): Maximum number of records to return. (Optional) | ||
| before (str): Pagination cursor to receive records before a provided ID. (Optional) | ||
| after (str): Pagination cursor to receive records after a provided ID. (Optional) | ||
| order (Literal["asc","desc"]): Sort records in either ascending or descending order. (Optional) | ||
|
|
||
| Returns: | ||
| FeatureFlagsListResource: Feature flags list response from WorkOS. | ||
| """ | ||
| ... | ||
|
|
||
| def get_feature_flag(self, slug: str) -> SyncOrAsync[FeatureFlag]: | ||
| """Gets details for a single feature flag. | ||
|
|
||
| Args: | ||
| slug (str): The unique slug identifier of the feature flag. | ||
|
|
||
| Returns: | ||
| FeatureFlag: Feature flag response from WorkOS. | ||
| """ | ||
| ... | ||
|
|
||
| def enable_feature_flag(self, slug: str) -> SyncOrAsync[FeatureFlag]: | ||
| """Enable a feature flag. | ||
|
|
||
| Args: | ||
| slug (str): The unique slug identifier of the feature flag. | ||
|
|
||
| Returns: | ||
| FeatureFlag: Updated feature flag response from WorkOS. | ||
| """ | ||
| ... | ||
|
|
||
| def disable_feature_flag(self, slug: str) -> SyncOrAsync[FeatureFlag]: | ||
| """Disable a feature flag. | ||
|
|
||
| Args: | ||
| slug (str): The unique slug identifier of the feature flag. | ||
|
|
||
| Returns: | ||
| FeatureFlag: Updated feature flag response from WorkOS. | ||
| """ | ||
| ... | ||
|
|
||
| def add_feature_flag_target(self, slug: str, resource_id: str) -> SyncOrAsync[None]: | ||
| """Add a target to a feature flag. | ||
|
|
||
| Args: | ||
| slug (str): The unique slug identifier of the feature flag. | ||
| resource_id (str): Resource ID in format user_<id> or org_<id>. | ||
|
|
||
| Returns: | ||
| None | ||
| """ | ||
| ... | ||
|
|
||
| def remove_feature_flag_target( | ||
| self, slug: str, resource_id: str | ||
| ) -> SyncOrAsync[None]: | ||
| """Remove a target from a feature flag. | ||
|
|
||
| Args: | ||
| slug (str): The unique slug identifier of the feature flag. | ||
| resource_id (str): Resource ID in format user_<id> or org_<id>. | ||
|
|
||
| Returns: | ||
| None | ||
| """ | ||
| ... | ||
|
|
||
|
|
||
| class FeatureFlags(FeatureFlagsModule): | ||
| _http_client: SyncHTTPClient | ||
|
|
||
| def __init__(self, http_client: SyncHTTPClient): | ||
| self._http_client = http_client | ||
|
|
||
| def list_feature_flags( | ||
| self, | ||
| *, | ||
| limit: int = DEFAULT_LIST_RESPONSE_LIMIT, | ||
| before: Optional[str] = None, | ||
| after: Optional[str] = None, | ||
| order: PaginationOrder = "desc", | ||
| ) -> FeatureFlagsListResource: | ||
| list_params: FeatureFlagListFilters = { | ||
| "limit": limit, | ||
| "before": before, | ||
| "after": after, | ||
| "order": order, | ||
| } | ||
|
|
||
| response = self._http_client.request( | ||
| FEATURE_FLAGS_PATH, | ||
| method=REQUEST_METHOD_GET, | ||
| params=list_params, | ||
| ) | ||
|
|
||
| return WorkOSListResource[FeatureFlag, FeatureFlagListFilters, ListMetadata]( | ||
| list_method=self.list_feature_flags, | ||
| list_args=list_params, | ||
| **ListPage[FeatureFlag](**response).model_dump(), | ||
| ) | ||
|
|
||
| def get_feature_flag(self, slug: str) -> FeatureFlag: | ||
| response = self._http_client.request( | ||
| f"{FEATURE_FLAGS_PATH}/{slug}", | ||
| method=REQUEST_METHOD_GET, | ||
| ) | ||
|
|
||
| return FeatureFlag.model_validate(response) | ||
|
|
||
| def enable_feature_flag(self, slug: str) -> FeatureFlag: | ||
| response = self._http_client.request( | ||
| f"{FEATURE_FLAGS_PATH}/{slug}/enable", | ||
| method=REQUEST_METHOD_PUT, | ||
| json={}, | ||
| ) | ||
|
|
||
| return FeatureFlag.model_validate(response) | ||
|
|
||
| def disable_feature_flag(self, slug: str) -> FeatureFlag: | ||
| response = self._http_client.request( | ||
| f"{FEATURE_FLAGS_PATH}/{slug}/disable", | ||
| method=REQUEST_METHOD_PUT, | ||
| json={}, | ||
| ) | ||
|
|
||
| return FeatureFlag.model_validate(response) | ||
|
|
||
| def add_feature_flag_target(self, slug: str, resource_id: str) -> None: | ||
| self._http_client.request( | ||
| f"{FEATURE_FLAGS_PATH}/{slug}/targets/{resource_id}", | ||
| method=REQUEST_METHOD_POST, | ||
| json={}, | ||
| ) | ||
|
|
||
| def remove_feature_flag_target(self, slug: str, resource_id: str) -> None: | ||
| self._http_client.request( | ||
| f"{FEATURE_FLAGS_PATH}/{slug}/targets/{resource_id}", | ||
| method=REQUEST_METHOD_DELETE, | ||
| ) | ||
|
|
||
|
|
||
| class AsyncFeatureFlags(FeatureFlagsModule): | ||
| _http_client: AsyncHTTPClient | ||
|
|
||
| def __init__(self, http_client: AsyncHTTPClient): | ||
| self._http_client = http_client | ||
|
|
||
| async def list_feature_flags( | ||
| self, | ||
| *, | ||
| limit: int = DEFAULT_LIST_RESPONSE_LIMIT, | ||
| before: Optional[str] = None, | ||
| after: Optional[str] = None, | ||
| order: PaginationOrder = "desc", | ||
| ) -> FeatureFlagsListResource: | ||
| list_params: FeatureFlagListFilters = { | ||
| "limit": limit, | ||
| "before": before, | ||
| "after": after, | ||
| "order": order, | ||
| } | ||
|
|
||
| response = await self._http_client.request( | ||
| FEATURE_FLAGS_PATH, | ||
| method=REQUEST_METHOD_GET, | ||
| params=list_params, | ||
| ) | ||
|
|
||
| return WorkOSListResource[FeatureFlag, FeatureFlagListFilters, ListMetadata]( | ||
| list_method=self.list_feature_flags, | ||
| list_args=list_params, | ||
| **ListPage[FeatureFlag](**response).model_dump(), | ||
| ) | ||
|
|
||
| async def get_feature_flag(self, slug: str) -> FeatureFlag: | ||
| response = await self._http_client.request( | ||
| f"{FEATURE_FLAGS_PATH}/{slug}", | ||
| method=REQUEST_METHOD_GET, | ||
| ) | ||
|
|
||
| return FeatureFlag.model_validate(response) | ||
|
|
||
| async def enable_feature_flag(self, slug: str) -> FeatureFlag: | ||
| response = await self._http_client.request( | ||
| f"{FEATURE_FLAGS_PATH}/{slug}/enable", | ||
| method=REQUEST_METHOD_PUT, | ||
| json={}, | ||
| ) | ||
|
|
||
| return FeatureFlag.model_validate(response) | ||
|
|
||
| async def disable_feature_flag(self, slug: str) -> FeatureFlag: | ||
| response = await self._http_client.request( | ||
| f"{FEATURE_FLAGS_PATH}/{slug}/disable", | ||
| method=REQUEST_METHOD_PUT, | ||
| json={}, | ||
| ) | ||
|
|
||
| return FeatureFlag.model_validate(response) | ||
|
|
||
| async def add_feature_flag_target(self, slug: str, resource_id: str) -> None: | ||
| await self._http_client.request( | ||
| f"{FEATURE_FLAGS_PATH}/{slug}/targets/{resource_id}", | ||
| method=REQUEST_METHOD_POST, | ||
| json={}, | ||
| ) | ||
|
|
||
| async def remove_feature_flag_target(self, slug: str, resource_id: str) -> None: | ||
| await self._http_client.request( | ||
| f"{FEATURE_FLAGS_PATH}/{slug}/targets/{resource_id}", | ||
| method=REQUEST_METHOD_DELETE, | ||
| ) | ||
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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| from workos.types.feature_flags.feature_flag import FeatureFlag | ||
| from workos.types.feature_flags.feature_flag import FeatureFlag, FeatureFlagOwner | ||
|
|
||
| __all__ = ["FeatureFlag"] | ||
| __all__ = ["FeatureFlag", "FeatureFlagOwner"] |
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 |
|---|---|---|
| @@ -1,12 +1,22 @@ | ||
| from typing import Literal, Optional | ||
| from typing import Literal, Optional, Sequence | ||
| from workos.types.workos_model import WorkOSModel | ||
|
|
||
|
|
||
| class FeatureFlagOwner(WorkOSModel): | ||
| email: str | ||
| first_name: Optional[str] | ||
| last_name: Optional[str] | ||
|
|
||
|
|
||
| class FeatureFlag(WorkOSModel): | ||
| id: str | ||
| object: Literal["feature_flag"] | ||
| slug: str | ||
| name: str | ||
| description: Optional[str] | ||
| tags: Sequence[str] | ||
| owner: Optional[FeatureFlagOwner] | ||
| enabled: bool | ||
| default_value: bool | ||
| created_at: str | ||
| updated_at: str |
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
Oops, something went wrong.
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.
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.
resource_idinterpolated directly into URL pathresource_idis supplied by the caller and is embedded verbatim into the URL path. The docstring documents the expected format asuser_<id>ororg_<id>, but there is no validation or URL-encoding applied. A value containing/,?, or#would silently alter the request URL.The same pattern applies to
slugthroughout the module, and this is consistent with how other modules in the SDK build paths. Since WorkOS-generated IDs won't contain these characters in practice, the risk is low — but a lightweight guard (e.g.,urllib.parse.quote) onresource_id(and similarly forslug) would make the surface more robust against malformed input:This also applies to the same pattern in the
AsyncFeatureFlagscounterpart (line 241–244).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 low-risk and consistent with how some other modules handle it but if you'd like me to do this, I am happy to!