Skip to content

Commit

Permalink
add: callback layer between provider and flag store
Browse files Browse the repository at this point in the history
  • Loading branch information
colebaileygit committed Jun 20, 2024
1 parent ff2fa84 commit 41ec424
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import typing

from openfeature.evaluation_context import EvaluationContext
from openfeature.event import ProviderEventDetails
from openfeature.flag_evaluation import FlagResolutionDetails
from openfeature.provider.metadata import Metadata
from openfeature.provider.provider import AbstractProvider
Expand Down Expand Up @@ -92,6 +93,11 @@ def get_metadata(self) -> Metadata:
"""Returns provider metadata"""
return Metadata(name="FlagdProvider")

def flag_store_updated_callback(self, flag_keys: typing.List[str]) -> None:
self.emit_provider_configuration_changed(
ProviderEventDetails(flags_changed=flag_keys)
)

def resolve_boolean_details(
self,
key: str,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,5 @@
import typing

from typing_extensions import Protocol

from openfeature.evaluation_context import EvaluationContext
from openfeature.flag_evaluation import FlagResolutionDetails

from .grpc import GrpcResolver
from .in_process import InProcessResolver


class AbstractResolver(Protocol):
def initialize(self, evaluation_context: EvaluationContext) -> None:
return

def shutdown(self) -> None: ...

def resolve_boolean_details(
self,
key: str,
default_value: bool,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[bool]: ...

def resolve_string_details(
self,
key: str,
default_value: str,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[str]: ...

def resolve_float_details(
self,
key: str,
default_value: float,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[float]: ...

def resolve_integer_details(
self,
key: str,
default_value: int,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[int]: ...

def resolve_object_details(
self,
key: str,
default_value: typing.Union[dict, list],
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[typing.Union[dict, list]]: ...

from .protocol import AbstractResolver

__all__ = ["AbstractResolver", "GrpcResolver", "InProcessResolver"]
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ..config import Config
from ..flag_type import FlagType
from ..proto.schema.v1 import schema_pb2, schema_pb2_grpc
from . import AbstractResolver
from .protocol import AbstractResolver

T = typing.TypeVar("T")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from openfeature.evaluation_context import EvaluationContext
from openfeature.exception import FlagNotFoundError, ParseError
from openfeature.flag_evaluation import FlagResolutionDetails, Reason
from openfeature.provider.provider import AbstractProvider

from ..config import Config
from ..provider import FlagdProvider
from .process.connector import FlagStateConnector
from .process.connector.file_watcher import FileWatcher
from .process.connector.grpc_watcher import GrpcWatcher
Expand All @@ -27,10 +27,10 @@ class InProcessResolver:
"sem_ver": sem_ver,
}

def __init__(self, config: Config, provider: AbstractProvider):
def __init__(self, config: Config, provider: FlagdProvider):
self.config = config
self.provider = provider
self.flag_store = FlagStore(provider)
self.flag_store = FlagStore(provider.flag_store_updated_callback)
self.connector: FlagStateConnector = (
FileWatcher(
self.config.offline_flag_source_path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import typing
from dataclasses import dataclass

from openfeature.event import ProviderEventDetails
from openfeature.exception import ParseError
from openfeature.provider.provider import AbstractProvider


class FlagStore:
def __init__(
self,
provider: AbstractProvider,
updated_notifier_hook: typing.Callable[[typing.List[str]], None],
):
self.provider = provider
self.updated_notifier_hook = updated_notifier_hook
self.flags: typing.Mapping[str, "Flag"] = {}

def get_flag(self, key: str) -> typing.Optional["Flag"]:
Expand All @@ -34,9 +32,7 @@ def update(self, flags_data: dict) -> None:
raise ParseError("`flags` key of configuration must be a dictionary")
self.flags = {key: Flag.from_dict(key, data) for key, data in flags.items()}

self.provider.emit_provider_configuration_changed(
ProviderEventDetails(flags_changed=list(self.flags.keys()))
)
self.updated_notifier_hook(list(self.flags.keys()))


@dataclass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import typing

from typing_extensions import Protocol

from openfeature.evaluation_context import EvaluationContext
from openfeature.flag_evaluation import FlagResolutionDetails


class AbstractResolver(Protocol):
def initialize(self, evaluation_context: EvaluationContext) -> None:
return

def shutdown(self) -> None: ...

def resolve_boolean_details(
self,
key: str,
default_value: bool,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[bool]: ...

def resolve_string_details(
self,
key: str,
default_value: str,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[str]: ...

def resolve_float_details(
self,
key: str,
default_value: float,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[float]: ...

def resolve_integer_details(
self,
key: str,
default_value: int,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[int]: ...

def resolve_object_details(
self,
key: str,
default_value: typing.Union[dict, list],
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[typing.Union[dict, list]]: ...

0 comments on commit 41ec424

Please sign in to comment.