-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,67 @@ | ||
from abc import ABC | ||
from logging import Logger | ||
from typing import Dict | ||
|
||
|
||
class FeatureFlagRouter(ABC): | ||
""" | ||
The base feature flag router. | ||
All feature flag routers should extend this class. | ||
""" | ||
|
||
_logger: Logger | ||
_feature_flags: Dict[str, bool] | ||
|
||
def __init__(self, logger: Logger) -> None: | ||
self._logger = logger | ||
self._feature_flags = {} | ||
|
||
def set_feature_is_enabled(self, name: str, is_enabled: bool) -> None: | ||
""" | ||
Enables or disables a feature flag in the in-memory dictionary of feature flags. | ||
Subclasses should call this method to validate parameters and cache values. | ||
name: The feature flag to check. | ||
is_enabled: Whether the feature flag is to be enabled or disabled. | ||
""" | ||
if name in self._feature_flags: | ||
self._logger.warn( | ||
f"Overridding feature flag value for '{name}'. Toggling from {self._feature_flags[name]} to {self._feature_flags[name]}" | ||
) | ||
if type(name) != str: | ||
raise TypeError("`name` must be a string.") | ||
|
||
if type(is_enabled) != bool: | ||
raise TypeError("`is_enabled` must be a boolean.") | ||
|
||
if not name: | ||
raise ValueError("`name` parameter is required and cannot be empty.") | ||
|
||
self._feature_flags[name] = is_enabled | ||
|
||
def feature_is_enabled( | ||
self, name: str, default: bool | None = False | ||
) -> bool | None: | ||
""" | ||
Determine whether a feature flag is enabled or disabled. | ||
Subclasses should call this method to validate parameters and use cached values. | ||
name: The feature flag to check. | ||
default: If the feature flag is not in the in-memory dictionary of flags, | ||
this is the default value to return. The default parameter value | ||
when not specified is `False`. | ||
""" | ||
if type(name) != str: | ||
raise TypeError("`name` must be a string.") | ||
|
||
if not name: | ||
raise ValueError("`name` parameter is required and cannot be empty.") | ||
|
||
if name in self._feature_flags: | ||
return self._feature_flags[name] | ||
|
||
return default |