Skip to content

Commit 7a39376

Browse files
committed
revert: refactor of hook package
Signed-off-by: Danju Visvanathan <danju.visvanathan@gmail.com>
1 parent dafe7b6 commit 7a39376

File tree

4 files changed

+140
-142
lines changed

4 files changed

+140
-142
lines changed

openfeature/hook/__init__.py

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
from openfeature.hook.hook import Hook, HookContext, HookData, HookHints, HookType
2-
from openfeature.hook.logging_hook import LoggingHook
1+
from __future__ import annotations
2+
3+
import typing
4+
from collections.abc import Mapping, MutableMapping, Sequence
5+
from datetime import datetime
6+
from enum import Enum
7+
8+
from openfeature.evaluation_context import EvaluationContext
9+
from openfeature.flag_evaluation import FlagEvaluationDetails, FlagType, FlagValueType
10+
11+
if typing.TYPE_CHECKING:
12+
from openfeature.client import ClientMetadata
13+
from openfeature.provider.metadata import Metadata
314

415
__all__ = [
516
"Hook",
617
"HookContext",
718
"HookData",
819
"HookHints",
920
"HookType",
10-
"LoggingHook",
1121
"add_hooks",
1222
"clear_hooks",
1323
"get_hooks",
@@ -16,6 +26,131 @@
1626
_hooks: list[Hook] = []
1727

1828

29+
# https://openfeature.dev/specification/sections/hooks/#requirement-461
30+
HookData = MutableMapping[str, typing.Any]
31+
32+
33+
class HookType(Enum):
34+
BEFORE = "before"
35+
AFTER = "after"
36+
FINALLY_AFTER = "finally_after"
37+
ERROR = "error"
38+
39+
40+
class HookContext:
41+
def __init__( # noqa: PLR0913
42+
self,
43+
flag_key: str,
44+
flag_type: FlagType,
45+
default_value: FlagValueType,
46+
evaluation_context: EvaluationContext,
47+
client_metadata: ClientMetadata | None = None,
48+
provider_metadata: Metadata | None = None,
49+
hook_data: HookData | None = None,
50+
):
51+
self.flag_key = flag_key
52+
self.flag_type = flag_type
53+
self.default_value = default_value
54+
self.evaluation_context = evaluation_context
55+
self.client_metadata = client_metadata
56+
self.provider_metadata = provider_metadata
57+
self.hook_data = hook_data or {}
58+
59+
def __setattr__(self, key: str, value: typing.Any) -> None:
60+
if hasattr(self, key) and key in (
61+
"flag_key",
62+
"flag_type",
63+
"default_value",
64+
"client_metadata",
65+
"provider_metadata",
66+
):
67+
raise AttributeError(f"Attribute {key!r} is immutable")
68+
super().__setattr__(key, value)
69+
70+
71+
# https://openfeature.dev/specification/sections/hooks/#requirement-421
72+
HookHintValue: typing.TypeAlias = (
73+
bool
74+
| int
75+
| float
76+
| str
77+
| datetime
78+
| Sequence["HookHintValue"]
79+
| Mapping[str, "HookHintValue"]
80+
)
81+
82+
HookHints = Mapping[str, HookHintValue]
83+
84+
85+
class Hook:
86+
def before(
87+
self, hook_context: HookContext, hints: HookHints
88+
) -> EvaluationContext | None:
89+
"""
90+
Runs before flag is resolved.
91+
92+
:param hook_context: Information about the particular flag evaluation
93+
:param hints: An immutable mapping of data for users to
94+
communicate to the hooks.
95+
:return: An EvaluationContext. It will be merged with the
96+
EvaluationContext instances from other hooks, the client and API.
97+
"""
98+
return None
99+
100+
def after(
101+
self,
102+
hook_context: HookContext,
103+
details: FlagEvaluationDetails[FlagValueType],
104+
hints: HookHints,
105+
) -> None:
106+
"""
107+
Runs after a flag is resolved.
108+
109+
:param hook_context: Information about the particular flag evaluation
110+
:param details: Information about how the flag was resolved,
111+
including any resolved values.
112+
:param hints: A mapping of data for users to communicate to the hooks.
113+
"""
114+
pass
115+
116+
def error(
117+
self, hook_context: HookContext, exception: Exception, hints: HookHints
118+
) -> None:
119+
"""
120+
Run when evaluation encounters an error. Errors thrown will be swallowed.
121+
122+
:param hook_context: Information about the particular flag evaluation
123+
:param exception: The exception that was thrown
124+
:param hints: A mapping of data for users to communicate to the hooks.
125+
"""
126+
pass
127+
128+
def finally_after(
129+
self,
130+
hook_context: HookContext,
131+
details: FlagEvaluationDetails[FlagValueType],
132+
hints: HookHints,
133+
) -> None:
134+
"""
135+
Run after flag evaluation, including any error processing.
136+
This will always run. Errors will be swallowed.
137+
138+
:param hook_context: Information about the particular flag evaluation
139+
:param hints: A mapping of data for users to communicate to the hooks.
140+
"""
141+
pass
142+
143+
def supports_flag_value_type(self, flag_type: FlagType) -> bool:
144+
"""
145+
Check to see if the hook supports the particular flag type.
146+
147+
:param flag_type: particular type of the flag
148+
:return: a boolean containing whether the flag type is supported (True)
149+
or not (False)
150+
"""
151+
return True
152+
153+
19154
def add_hooks(hooks: list[Hook]) -> None:
20155
global _hooks
21156
_hooks = _hooks + hooks

openfeature/hook/hook.py

Lines changed: 0 additions & 137 deletions
This file was deleted.

openfeature/hook/logging_hook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from openfeature.evaluation_context import EvaluationContext
55
from openfeature.exception import ErrorCode, OpenFeatureError
66
from openfeature.flag_evaluation import FlagEvaluationDetails, FlagValueType
7-
from openfeature.hook.hook import Hook, HookContext, HookHints
7+
from openfeature.hook import Hook, HookContext, HookHints
88

99

1010
class LoggingHook(Hook):

tests/hook/test_logging_hook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from openfeature.evaluation_context import EvaluationContext
77
from openfeature.exception import ErrorCode, FlagNotFoundError
88
from openfeature.flag_evaluation import FlagEvaluationDetails, FlagType
9-
from openfeature.hook import HookContext, LoggingHook
9+
from openfeature.hook.logging_hook import HookContext, LoggingHook
1010
from openfeature.provider.metadata import Metadata
1111

1212

0 commit comments

Comments
 (0)