-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Federico Bond <[email protected]>
- Loading branch information
1 parent
ed6a42f
commit 12a93a6
Showing
7 changed files
with
257 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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,100 @@ | ||
from collections import defaultdict | ||
from dataclasses import dataclass, field | ||
from enum import Enum | ||
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Union | ||
|
||
from openfeature.provider import FeatureProvider | ||
|
||
if TYPE_CHECKING: | ||
from openfeature.client import OpenFeatureClient | ||
|
||
|
||
class ProviderEvent(Enum): | ||
PROVIDER_READY = "PROVIDER_READY" | ||
PROVIDER_CONFIGURATION_CHANGED = "PROVIDER_CONFIGURATION_CHANGED" | ||
PROVIDER_ERROR = "PROVIDER_ERROR" | ||
PROVIDER_STALE = "PROVIDER_STALE" | ||
|
||
|
||
@dataclass | ||
class ProviderEventDetails: | ||
flags_changed: Optional[List[str]] = None | ||
message: Optional[str] = None | ||
metadata: Dict[str, Union[bool, str, int, float]] = field(default_factory=dict) | ||
|
||
|
||
@dataclass | ||
class EventDetails(ProviderEventDetails): | ||
provider_name: str = "" | ||
flags_changed: Optional[List[str]] = None | ||
message: Optional[str] = None | ||
metadata: Dict[str, Union[bool, str, int, float]] = field(default_factory=dict) | ||
|
||
@classmethod | ||
def from_provider_event_details( | ||
cls, provider_name: str, details: ProviderEventDetails | ||
) -> "EventDetails": | ||
return cls( | ||
provider_name=provider_name, | ||
flags_changed=details.flags_changed, | ||
message=details.message, | ||
metadata=details.metadata, | ||
) | ||
|
||
|
||
EventHandler = Callable[[EventDetails], None] | ||
|
||
|
||
class EventSupport: | ||
_global_handlers: Dict[ProviderEvent, List[EventHandler]] | ||
_client_handlers: Dict["OpenFeatureClient", Dict[ProviderEvent, List[EventHandler]]] | ||
|
||
def __init__(self) -> None: | ||
self._global_handlers = defaultdict(list) | ||
self._client_handlers = defaultdict(lambda: defaultdict(list)) | ||
|
||
def run_client_handlers( | ||
self, client: "OpenFeatureClient", event: ProviderEvent, details: EventDetails | ||
) -> None: | ||
for handler in self._client_handlers[client][event]: | ||
handler(details) | ||
|
||
def run_global_handlers(self, event: ProviderEvent, details: EventDetails) -> None: | ||
for handler in self._global_handlers[event]: | ||
handler(details) | ||
|
||
def add_client_handler( | ||
self, client: "OpenFeatureClient", event: ProviderEvent, handler: EventHandler | ||
) -> None: | ||
handlers = self._client_handlers[client][event] | ||
handlers.append(handler) | ||
|
||
def remove_client_handler( | ||
self, client: "OpenFeatureClient", event: ProviderEvent, handler: EventHandler | ||
) -> None: | ||
handlers = self._client_handlers[client][event] | ||
handlers.remove(handler) | ||
|
||
def add_global_handler(self, event: ProviderEvent, handler: EventHandler) -> None: | ||
self._global_handlers[event].append(handler) | ||
|
||
def remove_global_handler( | ||
self, event: ProviderEvent, handler: EventHandler | ||
) -> None: | ||
self._global_handlers[event].remove(handler) | ||
|
||
def run_handlers_for_provider( | ||
self, | ||
provider: FeatureProvider, | ||
event: ProviderEvent, | ||
provider_details: ProviderEventDetails, | ||
) -> None: | ||
details = EventDetails.from_provider_event_details( | ||
provider.get_metadata().name, provider_details | ||
) | ||
# run the global handlers | ||
self.run_global_handlers(event, details) | ||
# run the handlers for clients associated to this provider | ||
for client in self._client_handlers: | ||
if client.provider == provider: | ||
self.run_client_handlers(client, event, details) |
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
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
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